codeforces E. Tree (lca 求最小公共祖先)

E. Tree Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a rooted tree consisting of 𝑛n vertices numbered from 11 to 𝑛n. The root of the tree is a vertex number 11.

A tree is a connected undirected graph with 𝑛−1n−1 edges.

You are given 𝑚m queries. The 𝑖i-th query consists of the set of 𝑘𝑖ki distinct vertices 𝑣𝑖[1],𝑣𝑖[2],…,𝑣𝑖[𝑘𝑖]vi[1],vi[2],…,vi[ki]. Your task is to say if there is a path from the root to some vertex 𝑢u such that each of the given 𝑘k vertices is either belongs to this path or has the distance 11 to some vertex of this path.

Input

The first line of the input contains two integers 𝑛n and 𝑚m (2≤𝑛≤2⋅1052≤n≤2⋅105, 1≤𝑚≤2⋅1051≤m≤2⋅105) — the number of vertices in the tree and the number of queries.

Each of the next 𝑛−1n−1 lines describes an edge of the tree. Edge 𝑖i is denoted by two integers 𝑢𝑖ui and 𝑣𝑖vi, the labels of vertices it connects (1≤𝑢𝑖,𝑣𝑖≤𝑛,𝑢𝑖≠𝑣𝑖(1≤ui,vi≤n,ui≠vi).

It is guaranteed that the given edges form a tree.

The next 𝑚m lines describe queries. The 𝑖i-th line describes the 𝑖i-th query and starts with the integer 𝑘𝑖ki (1≤𝑘𝑖≤𝑛1≤ki≤n) — the number of vertices in the current query. Then 𝑘𝑖ki integers follow: 𝑣𝑖[1],𝑣𝑖[2],…,𝑣𝑖[𝑘𝑖]vi[1],vi[2],…,vi[ki] (1≤𝑣𝑖[𝑗]≤𝑛1≤vi[j]≤n), where 𝑣𝑖[𝑗]vi[j] is the 𝑗j-th vertex of the 𝑖i-th query.

It is guaranteed that all vertices in a single query are distinct.

It is guaranteed that the sum of 𝑘𝑖ki does not exceed 2⋅1052⋅105 (∑𝑖=1𝑚𝑘𝑖≤2⋅105∑i=1mki≤2⋅105).

Output

For each query, print the answer — "YES", if there is a path from the root to some vertex 𝑢u such that each of the given 𝑘k vertices is either belongs to this path or has the distance 11 to some vertex of this path and "NO" otherwise.

Example

input

Copy

10 6
1 2
1 3
1 4
2 5
2 6
3 7
7 8
7 9
9 10
4 3 8 9 10
3 2 4 6
3 2 1 5
3 4 8 2
2 6 10
3 5 4 7

output

Copy

YES
YES
YES
YES
NO
NO

题意:n个点,一棵树,m次询问,每次询问,先给q,然后q个点,问是否有一条从根出发的路径,使得这些点到该路径的距离小于等于1。

显然,该路径是到q个点中到根最远的点v。然后判断其他点与v的最小祖先的距离是否大于1,那么直接高度判断即可。求最小公共祖先用lca。

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
vector<int> e[200010];
int f[200010][20],a[200010],dep[200010];
void dfs(int x, int fa)
{
    f(i,0,e[x].size()){
        if(e[x][i] == fa) continue;
        dep[e[x][i]] = dep[x]+1;
        f[e[x][i]][0] = x;
        int vv = e[x][i];
        for(int j = 1;f[f[vv][j-1]][j-1];j++)
          f[vv][j] = f[f[vv][j-1]][j-1];
        dfs(e[x][i],x);
    }
}

int lca(int x, int y)
{
    if(dep[x]<dep[y]) swap(x,y);
    int t = dep[x] -dep[y];
    ff(i,0,17) if((1<<i)&t) x = f[x][i];
    for(int i = 17; i >= 0; i--)
    {
        if(f[x][i]!=f[y][i]) 
          x = f[x][i],y = f[y][i];
    }
    if(x == y) return x;
    return f[x][0];
}

int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    f(i,0,n - 1)
    {
        int x,y;
        cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    dep[1] = 1;
    dfs(1,0);
    while(m--)
    {
        int q;
        cin >> q;
        ff(i,1,q) cin >> a[i];
        int minn = dep[a[1]],mini = 1;
        ff(i,2,q) if(dep[a[i]] > minn){ minn = dep[a[i]];mini=i;}
        int flag = 1;
        ff(i,1,q)
        {
            int rt = lca(a[i],a[mini]);
            if(dep[a[i]] - dep[rt] > 1) {flag = 0;break;}
        }
        if(!flag) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值