C++ Tutorials

C++ Tutorials

You’re given a tree with NN vertices numbered from 11 to NN. Your goal is to handle QQ queries. For each query you are given KK nodes v1,v2,…,vKv1,v2,…,vK. Find if there exists a simple path in the tree covering all the given vertices.

Input

  • The first line contains a single integer TT – the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • Each of the following N−1N−1 lines contains two integers uu and vv denoting an edge between nodes uu and vv.
  • The next line contains a single integer QQ – the number of queries.
  • The next QQ lines describe queries. The ii-th line describes the ii-th query and starts with the integer KiKi — the number of vertices in the current query. Then KiKi integers follow: v1,v2,…,vKiv1,v2,…,vKi.

Output

For each query print "YES" (without quotes) if there exists a simple path in the tree covering all the given nodes, otherwise print "NO" (without quotes).

You may print each character of the string in uppercase or lowercase (for example, the strings "yEs""yes""Yes" and "YES" will all be treated as identical).

Constraints

  • 1≤T≤101≤T≤10
  • 1≤N≤1051≤N≤105
  • 1≤u,v,vj≤N1≤u,v,vj≤N
  • 1≤Q≤1051≤Q≤105
  • 1≤Ki≤N1≤Ki≤N for each valid ii.
  • The edges form a valid tree.
  • All vertices in a single query are distinct.
  • the sum of NN over all test cases does not exceed 2⋅1052⋅105.
  • For each test case, the sum of KiKi over all queries does not exceed 105105.

Subtasks

Subtask #1 (100 points): Original constraints

Sample Input

1
6
1 2
1 3
2 4
2 5
3 6
2
3 6 2 4
4 2 3 4 5

Sample Output

YES
NO

Explanation

  • For the first query the path will be 4−2−1−3−64−2−1−3−6.
  • For the second query there is no simple path that covers all the given vertices.

CodeChef Solution K Path Query Problem Code: KPATHQRY JULY21B July Long Challenge 2021

Due to copyright issues we won't be able to update the solution here immediately. Keep tracking, the solution will be updated on this website soon.

Full Solution Available Before 11th July Here

BookMark This Page