zoukankan      html  css  js  c++  java
  • Codeforces Round #395 (Div. 2) C. Timofey and a tree

    . 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
    题意:一个树上有n个节点,每个节点都有颜色,选一个节点为根节点 ,能不能使得根的子节点颜色一样
    要满足题意要求的情况 其实就是找任意相邻的节点,且颜色不同,使其中一个为根节点,因为这样可以满足 因为假如题目有符合的 那一定在这两点之中,
    所以只要把这两个点分别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 }
  • 相关阅读:
    K2 的Workspace 遭遇400 RequestLength 错误修复
    从APM角度上看:NoSQL和关系数据库并无不同
    Mono的Google Native Client(NaCl)技术支持
    FastReport.Mono 一款为Mono Framework设计的功能全面的报表生成工具
    修改 Windows Host 文件工具
    采用Mono进行移动开发图书推荐
    MonoDevelop添加NuGet支持
    WCF Service Hosting的线程关联性Mono实现比.NET统一?
    [转]WiX v3.7——支持MSBuild、自更新及引用计数
    [转]度量驱动开发
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/7229399.html
Copyright © 2011-2022 走看看