CF1899G.Unusual Entertainment
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A tree is a connected graph without cycles.
A permutation is an array consisting of n distinct integers from 1 to n in any order. For example, [5,1,3,2,4] is a permutation, but [2,1,1] is not a permutation (as 1 appears twice in the array) and [1,3,2,5] is also not a permutation (as n=4, but 5 is present in the array).
After a failed shoot in the BrMeast video, Alex fell into depression. Even his birthday did not make him happy. However, after receiving a gift from Timofey, Alex's mood suddenly improved. Now he spent days playing with the gifted constructor. Recently, he came up with an unusual entertainment.
Alex builds a tree from his constructor, consisting of n vertices numbered from 1 to n, with the root at vertex 1. Then he writes down each integer from 1 to n in some order, obtaining a permutation p. After that, Alex comes up with q triples of integers l,r,x. For each triple, he tries to determine if there is at least one descendant of vertex x among the vertices pl,pl+1,…,pr.
A vertex u is a descendant of vertex v if and only if dist(1,v)+dist(v,u)=dist(1,u), where dist(a,b) is the distance between vertices a and b. In other words, vertex v must be on the path from the root to vertex u.
Alex told Zakhar about this entertainment. Now Alex tells his friend q triples as described above, hoping that Zakhar can check for the presence of a descendant. Zakhar is very sleepy, so he turned to you for help. Help Zakhar answer all of Alex's questions and finally go to sleep.
树是无环的连通图。
排列是指由 1 到 n 的 n 个互不相同的整数以任意顺序组成的数组。例如,[5,1,3,2,4] 是一个排列,但 [2,1,1] 不是排列(因为数字 1 在数组中出现了两次),[1,3,2,5] 也不是排列(因为 n=4,但数组中却出现了 5)。
在 BrMeast 视频拍摄失败后,Alex 沮丧不已,甚至连自己的生日都没能让他开心起来。然而,在收到 Timofey 送的礼物后,Alex 的心情突然好转。他随后数日都在摆弄这份礼物——一套构造器玩具。最近,他想出了一个别出心裁的游戏。
Alex 用构造器搭建一棵包含 n 个顶点(编号为 1 到 n)的树,并将顶点 1 设为根节点。接着,他将 1 到 n 的每个整数按某种顺序写下,得到一个排列 p。之后,Alex 构造出 q 个整数三元组 l,r,x。对每个三元组,他试图判断:在子数组 pl,pl+1,…,pr 所对应的顶点中,是否至少存在一个顶点是顶点 x 的后代。
当且仅当 dist(1,v)+dist(v,u)=dist(1,u) 时,顶点 u 是顶点 v 的后代,其中 dist(a,b) 表示顶点 a 与 b 之间的距离。换言之,顶点 v 必须位于从根节点到顶点 u 的路径上。
Alex 把这个游戏告诉了 Zakhar。现在 Alex 向朋友依次给出上述 q 个三元组,希望 Zakhar 能够快速判断对应区间内是否存在 x 的后代。但 Zakhar 困得不行,于是向你求助。请帮 Zakhar 回答 Alex 提出的所有问题,好让他终于可以去睡觉了。
输入格式
The first line of the input contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n,q (1≤n,q≤105) — the number of vertices in the tree and the number of questions, respectively.
Each of the next n−1 lines contains two integers ui and vi (1≤ui,vi≤n), indicating that there is an edge between vertices ui and vi (it is guaranteed that the resulting graph is a tree).
The next line contains n integers p1,p2,…,pn (1≤pi≤n) — the permutation p (it is guaranteed that each integer from 1 to n appears exactly once).
Then follow q lines describing Alex's questions. The i-th line contains three integers l,r,x (1≤l≤r≤n, 1≤x≤n), as described in the statement.
It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
每个测试用例的第一行包含两个整数 n,q(1≤n,q≤105)—— 分别表示树中顶点的数量和询问的数量。
接下来的 n−1 行,每行包含两个整数 ui 和 vi(1≤ui,vi≤n),表示顶点 ui 与 vi 之间存在一条边(保证所给图构成一棵树)。
下一行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)—— 表示排列 p(保证 1 到 n 中的每个整数恰好出现一次)。
随后是 q 行,描述 Alex 提出的询问。第 i 行包含三个整数 l,r,x(1≤l≤r≤n, 1≤x≤n),含义如题面所述。
保证所有测试用例中 n 的总和与 q 的总和均不超过 105。
输出格式
For each of Alex's questions, print "Yes" (without quotes) if the described descendant exists, otherwise print "No" (without quotes).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
对于 Alex 的每个问题,如果所描述的后代存在,则输出 “Yes”(不带引号),否则输出 “No”(不带引号)。
您可以以任意大小写形式输出答案(例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答)。
输入输出样例
输入#1
3 3 5 1 2 2 3 1 2 3 1 2 2 1 2 3 2 3 1 1 2 3 2 3 3 10 10 2 6 2 7 2 4 1 7 2 8 10 6 8 5 9 4 3 4 10 2 5 9 1 7 6 4 3 8 8 9 8 7 8 1 7 10 6 4 8 9 5 5 10 7 10 1 9 9 2 9 10 6 6 6 2 10 10 6 1 1 1 1 1 1
输出#1
YES NO YES NO YES NO YES YES YES NO YES YES NO NO NO YES
输入解题思路,AI测评打分。不知道怎么写?