Timofey and a tree

C. Timofey and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
http://codeforces.com/problemset/problem/764/C
其实思路一般来做都是,dfs一下,但是有一个更直接的方法,
既然要知道存不存在那么一个点,那我干脆假设有,那么对于
那个点来说,只要树里面存在两点不同,其中一个必然是它。
下面就是AC代码了:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #define mem(a) memset(a,0,sizeof(a))
 7 #define  N 100005
 8 using namespace std;
 9 struct Node{
10     int a;
11     int b;
12 };
13 Node node[N];
14 int xn[N],xm[N];
15 int main(){
16     int n;
17     scanf("%d",&n);
18     for(int i=1;i<n;i++){
19         scanf("%d%d",&node[i].a,&node[i].b);
20     }
21     for(int i=1;i<=n;i++){
22         scanf("%d",&xm[i]);
23     }
24     int cnt=0;
25     for(int i=1;i<n;i++){
26         int k=node[i].a;
27         int p=node[i].b;
28         if(xm[k]!=xm[p]){
29             xn[k]++;
30             xn[p]++;
31             cnt++;
32         }
33     }
34     bool prime=true;
35     for(int i=1;i<=n;i++){
36         if(xn[i]==cnt){
37             printf("YES
");
38             printf("%d",i);
39             prime=false;
40             break;
41         }
42     }
43     if(prime){
44         printf("NO
");
45     }
46 
47     return 0;
48 }

 当然还有一种是DFS啦,但是我写了好几遍都错了,然后看了下大神的。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cstdlib>
  6 #include<string.h>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<cmath>
 13 typedef long long ll;
 14 typedef unsigned long long LL;
 15 using namespace std;
 16 const double PI=acos(-1.0);
 17 const double eps=0.0000000001;
 18 const int INF=0x3f3f3f3f;
 19 const int N=1000000+100;
 20 int tot;
 21 int head[N];
 22 int a[N];
 23 int vis[N];
 24 struct node{
 25     int to,next;
 26 }edge[N<<1];
 27 void init(){
 28     memset(vis,0,sizeof(vis));
 29     memset(head,-1,sizeof(head));
 30     tot=0;
 31 }
 32 void add(int u,int v){
 33     edge[tot].to=v;
 34     edge[tot].next=head[u];
 35     head[u]=tot++;
 36 }
 37 int flagg;
 38 void  DFS(int x){
 39     vis[x]=1;
 40     for(int i=head[x];i!=-1;i=edge[i].next){
 41         int v=edge[i].to;
 42         if(vis[v])continue;
 43         if(a[v]!=a[x]) {flagg=1;return;}
 44         DFS(v);
 45     }
 46     return ;
 47 }
 48 map<int,int>mp;
 49 int main(){
 50     int n;
 51     while(scanf("%d",&n)!=EOF){
 52         init();
 53         int u,v;
 54         for(int i=2;i<=n;i++){
 55             scanf("%d%d",&u,&v);
 56             mp[u]=v;
 57             add(u,v);
 58             add(v,u);
 59         }
 60         for(int i=1;i<=n;i++)scanf("%d",&a[i]);
 61         map<int,int>::iterator it;
 62         int flag=0;
 63         for(it=mp.begin();it!=mp.end();it++){
 64             u=it->first;
 65             v=it->second;
 66             if(a[u]!=a[v]){
 67                 flag=1;
 68                 break;
 69             }
 70         }
 71          if(flag==0){
 72             cout<<"YES"<<endl;
 73             cout<<n<<endl;continue;
 74         }
 75         //cout<<u<<" "<<v<<endl;
 76         vis[u]=1;
 77         int tt=0;
 78         for(int i=head[u];i!=-1;i=edge[i].next){
 79             int vv=edge[i].to;
 80             vis[vv]=1;
 81             flagg=0;
 82             DFS(vv);
 83             if(flagg==1){
 84                 tt=1;
 85                 break;
 86             }
 87         }
 88         if(tt==0){
 89             cout<<"YES"<<endl;
 90             cout<<u<<endl;
 91             continue;
 92         }
 93         memset(vis,0,sizeof(vis));
 94         vis[u]=0;
 95         vis[v]=1;
 96         tt=0;
 97         for(int i=head[v];i!=-1;i=edge[i].next){
 98             int vv=edge[i].to;
 99             vis[vv]=1;
100             flagg=0;
101             DFS(vv);
102             if(flagg==1)tt=2;
103         }
104         if(tt==0){
105             cout<<"YES"<<endl;
106             cout<<v<<endl;
107             continue;
108         }
109         cout<<"NO"<<endl;
110 
111     }
112 }


原文地址:https://www.cnblogs.com/zllwxm123/p/7236432.html