Codeforces Round #395 (Div. 1) A. Timofey and a tree(深搜)

传送门

Description

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.

Sample Input

4
1 2
2 3
3 4
1 2 1 1

3
1 2
2 3
1 2 3

4
1 2
2 3
3 4
1 2 1 2

Sample Output

YES
2

YES
2

NO

思路

题意:给定一棵树,每个结点有一种颜色,是否存在一个结点,以它为根,使得这棵树的每个子树仅有一种颜色。

题解:方法一:若这个树每个结点颜色相同,则肯定存在;若有不同颜色结点,我们只需随便找到两个相邻的不同颜色的结点,分别以之为根搜索看其每个子树是否只有一种颜色,若这两种情况都不成立,则最后可推之肯定不存在这样的结点。

方法二:换一个结点使成为根后,其子树所有结点颜色相同,那么如果这样的树存在说明了,与这个结点相连的结点都跟这个结点颜色不相同,因此找出结点连接数等于相邻节点颜色不同的总数,这个点可调整成为根。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int color[maxn];
vector<int>itv[maxn];
bool ok;
int curr_color;

void dfs(int v,int fa){
	ok = ok & (curr_color == color[v]);
	for (auto u : itv[v]){
		if (u == fa)	continue;
		dfs(u,v);
	}
}

bool solve(int root){
	bool res = true;
	for (auto u : itv[root]){
		curr_color = color[u];
		ok = true;
		dfs(u,root);
		res = res & ok;
	}
	return res;
}

int main(){
	int n,u,v,root1 = -1,root2 = -1;
	scanf("%d",&n);
	for (int i = 1;i < n;i++){
		scanf("%d%d",&u,&v);
		itv[u].push_back(v);
		itv[v].push_back(u);
	}
	for (int i = 1;i <= n;i++)	scanf("%d",&color[i]);
	for (int i = 0;i < n;i++){
		for (auto u: itv[i]){
			if (color[u] != color[i]){
				root1 = u;
				root2 = i;
			}
		}
	}
	if (root1 == -1){
		printf("YES\n1");
	}
	else	if (solve(root1))	printf("YES\n%d\n",root1);
	else	if (solve(root2))	printf("YES\n%d\n",root2);
	else	printf("NO\n");
	return 0;
}

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int color[maxn],degree[maxn];
struct Edge{
    int u,v;
}edge[maxn];

int main(){
    int n,cnt = 0;
    scanf("%d",&n);
    for (int i = 0;i < n - 1;i++){
        scanf("%d%d",&edge[i].u,&edge[i].v);
    }
    for (int i = 1;i <= n;i++)   scanf("%d",&color[i]);
    for (int i = 0;i < n - 1;i++){
        if (color[edge[i].u] != color[edge[i].v]){
            cnt++;
            degree[edge[i].u]++;
            degree[edge[i].v]++;
        }
    }
    bool flag = false;
    for (int i = 1;i <= n;i++){
        if (degree[i] == cnt){
            printf("YES\n%d\n",i);
            flag = true;
            break;
        }
    }
    if (!flag){
        printf("NO\n");
    }
    return 0;
}

  

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/7342551.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值