Tree Queries
You are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is a vertex number 1.
A tree is a connected undirected graph with n−1 edges.
You are given m queries. The i-th query consists of the set of ki distinct vertices 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 1 to some vertex of this path.
题目大意:给你一颗n个结点的树,m个询问,每个询问包含 k 个结点,问是否存在一条路径(由根结点 1 开始的路径)包含这k个结点,或者这些结点距离这条路径的长度为 1;
非常思维的一道树上题目,mx[] 数组表示按照dfs序表示结点顺序,mi[] 数组表示该结点可以往下到达的最大结点(结点按dfs序表示);可以发现,如果两个点x,y不在一条路径上,那么深度更小的点 mi[x] 一定小于深度更大的点的 mx[y];
所以只要记录 k 个点的mi的最小值,mx的最大值,比较大小就可以;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=200100;
const int M=50100;
const int mod=1e9;
int n,m;
struct Node{
int to,nex;
}edge[N*2];
int head[N],cnt;
int fa[N],mi[N],mx[N],num;
void add(int p,int q){
edge[cnt].to=q,edge[cnt].nex=head[p],head[p]=cnt++;
}
void dfs(int p,int ft){
fa[p]=ft;
mx[p]=++num;
for(int i=head[p];~i;i=edge[i].nex){
int q=edge[i].to;
if(q!=ft) dfs(q,p);
}
mi[p]=num;
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(1,0);
while(m--){
int k;scanf("%d",&k);
int m_i=n,m_x=1;
for(int i=1;i<=k;i++){
int x;scanf("%d",&x);
if(x!=1) x=fa[x];
m_i=min(m_i,mi[x]);
m_x=max(m_x,mx[x]);
}
if(m_x<=m_i) printf("YES\n");
else printf("NO\n");
}
return 0;
}