【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/82584642

题干:

Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.

Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.

Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.

A subtree of some vertex is a subgraph containing that vertex and all its descendants.

Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.

Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.

The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

Output

Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.

Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

Examples

Input

4
1 2
2 3
3 4
1 2 1 1

Output

YES
2

Input

3
1 2
2 3
1 2 3

Output

YES
2

Input

4
1 2
2 3
3 4
1 2 1 2

Output

NO

题目大意:

    给定一棵每个顶点都标记颜色的树,问能否从一个顶点出发,使得它的子树都是一样颜色(不包括这个顶点本身的颜色)。

题解:因条件要求,我们可以知道不同的颜色的顶点中其中一个必须是答案顶点,我们统计一下不同颜色顶点个数(设为m),和不同颜色顶点中顶点出现次数。最后如果有一个顶点出现次数等于m,那么这个顶点就是答案顶点。

解题报告:

    思维题啊害死我、、、

    第一反应子树,我擦dfs序吧?

    第二反应不行啊总不能对每一个顶点建树吧?要不试试、、

    第三反应woc超时了,,,    

    关于图论的问题,可以考虑cnt计数、、、这题sum表示每一条边中有多少条边是两节点颜色不一样的。如果正好和节点 i 的边数相同,那 i 就是答案。

AC代码:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 1e5 + 5 ;
int u[MAX],v[MAX],cnt[MAX],col[MAX];
int main()
{
	int n,sum=0;
	cin>>n;
	for(int i = 1; i<n; i++) {
		scanf("%d%d",&u[i],&v[i]);
	}
	for(int i = 1; i<=n; i++) {
		scanf("%d",&col[i]);
	}
	for(int i = 1; i<n; i++) {
		if(col[u[i]]!=col[v[i]]) {
			sum++;cnt[u[i]]++;cnt[v[i]]++;
		}
	}
	for(int i = 1; i<=n; i++) {
		if(cnt[i] == sum) {
			printf("YES\n%d\n",i);
			return 0 ;
		}
	}
	printf("NO\n");
	return 0 ;
}

dfs序超时代码:(刚开始是o(n)的注释掉那一部分,后来改成logn的线段树,但是还是超时,看来时间都花在dfs序上了)

#include<bits/stdc++.h>

using namespace std;
const int MAX = 100000 +5;
const int INF = 0x3f3f3f3f;
int cnt,id;
int st[MAX],ed[MAX],col[MAX],q[MAX];
int head[MAX];
struct Edge{
	int to,ne;
}e[1000000 +  5];
struct TREE {
	int l,r;
	int maxx,minn;
} tree[MAX * 4];
void dfs(int x,int fa)
{
    q[++id]=x;
    st[x]=id;
    for(int i=head[x];i!=-1;i=e[i].ne)
        if(e[i].to!=fa)
            dfs(e[i].to,x);
    ed[x]=id;
}
void pushup(int cur) {
	tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);
	tree[cur].minn = min(tree[cur*2].minn,tree[cur*2+1].minn);
}
void build(int l,int r,int cur) {
	tree[cur].l=l;
	tree[cur].r=r;
	if(l == r) {
		tree[cur].maxx = col[q[r]];
		tree[cur].minn = col[q[r]];
		return ;
	}
	int m = (l + r)/2;
	build(l,m,cur*2);
	build(m+1,r,cur*2+1);
	pushup(cur);
}
int qmax(int pl,int pr,int cur) {
	if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].maxx;
	int ll=0,rr=0;
	if(pl <= tree[cur*2].r) ll = qmax(pl,pr,cur*2);
	if(pr >= tree[cur*2+1].l) rr = qmax(pl,pr,cur*2+1);
	return max(rr,ll);
}
int qmin(int pl,int pr,int cur) {
	if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].minn;
	int ll=INF,rr=INF;
	if(pl <= tree[cur*2].r) ll = qmin(pl,pr,cur*2);
	if(pr >= tree[cur*2+1].l) rr = qmin(pl,pr,cur*2+1);
	return min(rr,ll);
}
void add(int u,int v) {
	e[++cnt].to = v;
	e[cnt].ne = head[u];
	head[u] = cnt;
}
int main()
{
	int n,u,v;
	scanf("%d",&n);
	memset(head,-1,sizeof head);
	for(int i = 1; i<n; i++) {
		scanf("%d%d",&u,&v);
		add(u,v);add(v,u);
	}
	for(int i = 1; i<=n; i++) {
		scanf("%d",&col[i]);
	}
	//枚举每一个顶点
	
	int flag = 1; 
	for(int i = 1; i<=n; i++) {
		id=0;dfs(i,i);flag = 1;
		build(1,n,1);
		for(int j = head[i]; j!=-1; j=e[j].ne) {
			int tmp = col[e[j].to];
			int maxx = qmax(st[e[j].to],ed[e[j].to],1);
			int minn = qmin(st[e[j].to],ed[e[j].to],1);
			if(maxx != tmp || minn != tmp) flag=0;
//			for(int k = st[e[j].to]; k<=ed[e[j].to]; k++) {
//				if(col[q[k]] != tmp) {
//					flag=0;break;
//				}
//			}
			if(flag == 0) break;
		}
		if(flag == 1) {
			printf("YES\n");
			printf("%d\n",i);
			return 0;
		}
	}
	printf("NO\n");
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/82584642
今日推荐