Codeforces Round #629 (Div. 3) E— Tree Queries(裸最近公共祖先)

整理的算法模板ACM算法模板总结(分类详细版)

题目链接:https://codeforces.ml/contest/1328/problem/E

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 nn vertices numbered from 11 to nn. The root of the tree is a vertex number 11.

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

You are given mm queries. The ii-th query consists of the set of kiki distinct vertices vi[1],vi[2],…,vi[ki]vi[1],vi[2],…,vi[ki]. Your task is to say if there is a path from the root to some vertex uu such that each of the given kk 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 nn and mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤2⋅1051≤m≤2⋅105) — the number of vertices in the tree and the number of queries.

Each of the next n−1n−1 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1≤ui,vi≤n,ui≠vi(1≤ui,vi≤n,ui≠vi).

It is guaranteed that the given edges form a tree.

The next mm lines describe queries. The ii-th line describes the ii-th query and starts with the integer kiki (1≤ki≤n1≤ki≤n) — the number of vertices in the current query. Then kiki integers follow: vi[1],vi[2],…,vi[ki]vi[1],vi[2],…,vi[ki] (1≤vi[j]≤n1≤vi[j]≤n), where vi[j]vi[j] is the jj-th vertex of the ii-th query.

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

It is guaranteed that the sum of kiki does not exceed 2⋅1052⋅105 (∑i=1mki≤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 uu such that each of the given kk 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

Note

The picture corresponding to the example:

  • 题意:n个点,一棵树,m次询问,每次询问,先给q,然后q个点,问是否有一条从根出发的路径,使得这些点到该路径的距离小于等于1。
  • 思路:显然,该路径是到q个点中到根最远的点v。然后判断其他点与v的最小祖先的距离是否大于1,那么直接高度判断即可。求最小公共祖先用lca

很裸的lca

#include <bits/stdc++.h>

using namespace std;

const int N=2e5+7;
vector<int> G[N];//用来存图;
long long bit[30];//存放倍增2次方
int f[N][30];//存放结点u往上倍增2的i次方时的祖先结点
int depth[N];//记录点的深度
int qq[N];
void init()//预处理倍增2次方
{
    bit[0]=1;
    for(int i=1;i<=29;i++) bit[i]=bit[i-1]*2;
}
void dfs(int u,int par)
{
    depth[u]=depth[par]+1;//当前点u的深度是父亲结点par的+1
    f[u][0]=par;
    for(int i=1;i<=29;i++) f[u][i]=f[f[u][i-1]][i-1];//倍增算法
    for(int i=0;i<(int)G[u].size();i++)//依次访问与之相连的儿子结点
    {
        int v=G[u][i];
        if(v==par) continue;//跳过父亲结点
        dfs(v,u);//当前点做父亲结点,相邻的点为儿子结点进行dfs
    }
}
int lca(int a,int b)
{
    if(depth[a]<depth[b]) swap(a,b);
    int dif=depth[a]-depth[b];//算出两个点的深度差
    for(int i=29;i>=0;i--)//从深度大的开始往上倍增,直到两者深度相同
    {
        if(dif>=bit[i])   //倍增时每次往上跳跃最大值;
        {
            a=f[a][i];
            dif-=bit[i];//深度差减去跳跃的长度
        }
    }
    if(a==b) return a;//如果往上倍增后就等于了另一个点,说明这个点就是lca
    for(int i=29;i>=0;i--)
    {
        if(depth[a]>=bit[i]&&f[a][i]!=f[b][i])//两个点同时从同一深度往上倍增,
                                //如果倍增后他们的祖先结点不相等,继续倍增,如果相等,就停止倍增;
        {
            a=f[a][i],b=f[b][i];
        }
    }
    return f[a][0];//(当倍增到祖先结点相等时就输出此时他们的祖先结点)
}
int main()
{
    init();
    int u,v,n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<n;i++)
    {
        scanf("%d %d",&u,&v);//存图
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0);//从根节点开始,深度为0,进行dfs
    while(m--)
    {
    	int q;
    	cin >>q;
    	for(int i=0;i<q;i++) cin >>qq[i];
		int flag=1;
    	int maxx=depth[qq[0]],res=0;
    	for(int i=1;i<q;i++) if(depth[qq[i]]>maxx) maxx=depth[qq[i]],res=i;
		for(int i=0;i<q;i++)
		{
			int fa=lca(qq[i],qq[res]);
			if(abs(depth[fa]-depth[qq[i]])>1)
			{
				flag=0;
				break;
			}
		}
		if(flag) cout <<"YES"<<endl;
		else cout <<"NO"<<endl;
	}
    return 0;
}
发布了224 篇原创文章 · 获赞 56 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_43872728/article/details/105139308