CF1702G2.Passable Paths (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is a hard version of the problem. The only difference between an easy and a hard version is in the number of queries.

Polycarp grew a tree from nn vertices. We remind you that a tree of nn vertices is an undirected connected graph of nn vertices and n1n-1 edges that does not contain cycles.

He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).

In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).

For example, for a tree below sets {3,2,5}\{3, 2, 5\} , {1,5,4}\{1, 5, 4\} , {1,4}\{1, 4\} are passable, and {1,3,5}\{1, 3, 5\} , {1,2,3,4,5}\{1, 2, 3, 4, 5\} are not.

Polycarp asks you to answer qq queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.

输入格式

The first line of input contains a single integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — number of vertices.

Following n1n - 1 lines a description of the tree..

Each line contains two integers uu and vv ( 1u,vn1 \le u, v \le n , uvu \ne v ) — indices of vertices connected by an edge.

Following line contains single integer qq ( 1q1051 \le q \le 10^5 ) — number of queries.

The following 2q2 \cdot q lines contain descriptions of sets.

The first line of the description contains an integer kk ( 1kn1 \le k \le n ) — the size of the set.

The second line of the description contains kk of distinct integers p1,p2,,pkp_1, p_2, \dots, p_k ( 1pin1 \le p_i \le n ) — indices of the vertices of the set.

It is guaranteed that the sum of kk values for all queries does not exceed 21052 \cdot 10^5 .

输出格式

Output qq lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO
    YES
    NO
    YES
  • 输入#2

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

    输出#2

    YES
    NO
    YES
    YES
首页