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 }

 

转载于:https://www.cnblogs.com/ccd2333/p/6364503.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值