310853: CF1899G. Unusual Entertainment

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:2 Solved:0

Description

G. Unusual Entertainmenttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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 $p_l, p_{l+1}, \ldots, p_r$.

A vertex $u$ is a descendant of vertex $v$ if and only if $\mathrm{dist}(1, v) + \mathrm{dist}(v, u) = \mathrm{dist}(1, u)$, where $\mathrm{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.

Input

The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains two integers $n, q$ ($1 \le n, q \le 10^5$) — the number of vertices in the tree and the number of questions, respectively.

Each of the next $n - 1$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$), indicating that there is an edge between vertices $u_i$ and $v_i$ (it is guaranteed that the resulting graph is a tree).

The next line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le 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 \le l \le r \le n$, $1 \le x \le 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 $10^5$.

Output

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).

ExampleInput
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
Output
YES
NO
YES
NO
YES

NO
YES
YES
YES
NO
YES
YES
NO
NO
NO

YES

Output

题目大意:
一个树是一个没有环的连通图。
一个排列是一个由 n 个不同的从 1 到 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。对于每个序列,他试图确定顶点 p_l, p_{l+1}, …, p_r 中是否至少有一个顶点 x 的后代。
一个顶点 u 是顶点 v 的后代当且仅当 dist(1, v) + dist(v, u) = dist(1, u),其中 dist(a, b) 是顶点 a 和 b 之间的距离。换句话说,顶点 v 必须在从根到顶点 u 的路径上。
Alex 把这种娱乐方式告诉了 Zakhar。现在 Alex 把他的朋友描述的上述 q 个三元素序列告诉 Zakhar,希望 Zakhar 能检查是否存在一个后代。Zakhar 非常困,所以他向你求助。帮助 Zakhar 回答 Alex 的问题,然后去睡觉。

输入输出数据格式:
输入:
第一行包含一个整数 t (1 ≤ t ≤ 10^4) —— 测试用例的数量。
每个测试用例的第一行包含两个整数 n, q (1 ≤ n, q ≤ 10^5) —— 树中的顶点数和问题数。
接下来 n - 1 行,每行包含两个整数 u_i 和 v_i (1 ≤ u_i, v_i ≤ n),表示顶点 u_i 和 v_i 之间有一条边(保证生成的图是一棵树)。
下一行包含 n 个整数 p_1, p_2, …, p_n (1 ≤ p_i ≤ n) —— 排列 p(保证从 1 到 n 的每个整数恰好出现一次)。
然后是 q 行,描述 Alex 的问题。第 i 行包含三个整数 l, r, x (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ n),如题目描述所示。
保证所有测试用例的 n 和 q 之和不超过 10^5。

输出:
对于 Alex 的每个问题,如果描述的后代存在,则打印 "Yes"(不带引号),否则打印 "No"(不带引号)。
你可以以任何大小写输出答案(例如,"yEs"、"yes"、"Yes" 和 "YES" 将被视为肯定答案)。题目大意: 一个树是一个没有环的连通图。 一个排列是一个由 n 个不同的从 1 到 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。对于每个序列,他试图确定顶点 p_l, p_{l+1}, …, p_r 中是否至少有一个顶点 x 的后代。 一个顶点 u 是顶点 v 的后代当且仅当 dist(1, v) + dist(v, u) = dist(1, u),其中 dist(a, b) 是顶点 a 和 b 之间的距离。换句话说,顶点 v 必须在从根到顶点 u 的路径上。 Alex 把这种娱乐方式告诉了 Zakhar。现在 Alex 把他的朋友描述的上述 q 个三元素序列告诉 Zakhar,希望 Zakhar 能检查是否存在一个后代。Zakhar 非常困,所以他向你求助。帮助 Zakhar 回答 Alex 的问题,然后去睡觉。 输入输出数据格式: 输入: 第一行包含一个整数 t (1 ≤ t ≤ 10^4) —— 测试用例的数量。 每个测试用例的第一行包含两个整数 n, q (1 ≤ n, q ≤ 10^5) —— 树中的顶点数和问题数。 接下来 n - 1 行,每行包含两个整数 u_i 和 v_i (1 ≤ u_i, v_i ≤ n),表示顶点 u_i 和 v_i 之间有一条边(保证生成的图是一棵树)。 下一行包含 n 个整数 p_1, p_2, …, p_n (1 ≤ p_i ≤ n) —— 排列 p(保证从 1 到 n 的每个整数恰好出现一次)。 然后是 q 行,描述 Alex 的问题。第 i 行包含三个整数 l, r, x (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ n),如题目描述所示。 保证所有测试用例的 n 和 q 之和不超过 10^5。 输出: 对于 Alex 的每个问题,如果描述的后代存在,则打印 "Yes"(不带引号),否则打印 "No"(不带引号)。 你可以以任何大小写输出答案(例如,"yEs"、"yes"、"Yes" 和 "YES" 将被视为肯定答案)。

加入题单

上一题 下一题 算法标签: