樹形結構 Codeforces Round #629 (Div. 3) E題

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章