cf 763A. Timofey and a tree

呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边)

(一开始还想各种各样奇怪的dfs。。。垃圾)

 1 #include<bits/stdc++.h> 
 2 #define LL long long 
 3 #define N 100005
 4 #define lowbit(x) x&(-x)
 5 using namespace std;
 6 inline int ra()
 7 {
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
10     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
11     return x*f;
12 }
13 int head[N],cnt;
14 struct node{
15     int next,to;
16 }e[N<<1];
17 int n,col[N],tot;
18 void insert(int x, int y)
19 {
20     e[++cnt].next=head[x];
21     e[cnt].to=y;
22     head[x]=cnt;
23 }
24 void dfs(int x, int fa)
25 {
26     for (int i=head[x];i;i=e[i].next)
27     {
28         if (e[i].to==fa) continue;
29         if (col[e[i].to]!=col[x]) tot++;
30         dfs(e[i].to,x); 
31     }
32 }
33 int main()
34 {
35     int n=ra();
36     for (int i=1; i<n; i++)
37     {
38         int x=ra(),y=ra();
39         insert(x,y);
40         insert(y,x);
41     }
42     for (int i=1; i<=n; i++)
43         col[i]=ra();
44     dfs(1,0);
45     for (int i=1; i<=n; i++)
46     {
47         int sum=0;
48         for (int j=head[i];j;j=e[j].next)
49             if (col[e[j].to]!=col[i]) sum++;
50         if (sum==tot) 
51         {
52             cout<<"YES"<<endl<<i;
53             return 0;
54         }
55     }
56     cout<<"NO";
57     return 0;
58 }

 

posted @ 2017-02-04 11:34  ws_ccd  阅读(188)  评论(0编辑  收藏  举报