C. Timofey and a tree codeforces 395 div2 C

来源:互联网 发布:遗传算法的过程 编辑:程序博客网 时间:2024/05/21 17:58

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 ≤ n, u ≠ 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
n个点,每个点有各自的颜色(可能相同可能不同),问一个棵树遮住一个点,令它的子树都只有一种颜色。
傻瓜题,找出不同颜色的两个点,判断就行,因为如果有两个颜色不一样的点,那么要遮住的点肯定就在这两个里面,还要特判一下,全部颜色相同的情况。贴代码。

#include <bits/stdc++.h>using namespace std;long long a[200100];vector<int> v[200100];int flag;int vis[200100];void dfs(int x,int f){    if(flag==1) return ;    if(a[x]!=f)    {     flag=1;     return ;    }    for(int i=0;i<v[x].size();i++)    {        if(vis[v[x][i]]) continue;        vis[v[x][i]]=1;        dfs(v[x][i],f);        vis[v[x][i]]=0;        if(flag==1) break;    }    return ;}pair<int,int> p[201000];int re[4];int main(){    int n;    while(cin>>n){        memset(vis,0,sizeof(vis));        memset(v,0,sizeof(v));        for(int i=0;i<n-1;i++)        {            int x1,x2;            cin>>x1>>x2;            v[x1].push_back(x2);            v[x2].push_back(x1);            p[i]=make_pair(x1,x2);        }        for(int i=1;i<=n;i++)        {            cin>>a[i];        }        int f=0;        for(int i=0;i<n-1;i++)        {            int x1=p[i].first;            int x2=p[i].second;            if(a[x1]!=a[x2])            {            re[0]=x1;            re[1]=x2;            f=1;            // printf("%d %d\n",x1,x2 );            }        }        if(!f)         {            printf("YES\n");            printf("1\n" );            continue;        }        int g=0;        for(int i=0;i<=1;i++)        {            flag=0;            int h=re[i];            vis[h]=1;            for(int j=0;j<v[h].size();j++)            {              dfs(v[h][j],a[v[h][j]]);            }            vis[h]=0;            if(flag==0)            {                printf("YES\n");                printf("%d\n",h );                g=1;                break;            }        }        if(!g) printf("NO\n");    }}
0 0