zoukankan      html  css  js  c++  java
  • Codeforces 764C Timofey and a tree

    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 ≤ nu ≠ 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

    题解:

    规定一个特殊边,也就是这条边的两个端点的颜色是不一样的,找出所有的特殊边,同时记录相关颜色
    出现的次数,如果有一种颜色出现的次数和边的数目是相同的,那么就存在这么一个点。

    
    
     1 #include<set>  
     2 #include<cstdio>  
     3 #include<vector>  
     4 #include<cstring>  
     5 #include<iostream>  
     6 #include<algorithm>  
     7 using namespace std;  
     8 const int maxn=1e5+50;  
     9 struct Node  
    10 {  
    11     int u,v;  
    12 };  
    13 int n;  
    14 int d[maxn];  
    15 int col[maxn];  
    16 Node e[maxn];  
    17 int main()  
    18 {  
    19     scanf("%d",&n);  
    20     for(int i=1; i<n; i++)  
    21         scanf("%d%d",&e[i].u,&e[i].v);  
    22     for(int i=1; i<=n; i++)  
    23         scanf("%d",&col[i]);  
    24     int tot=0;  
    25     for(int i=1; i<n; i++)  
    26     {  
    27         if(col[e[i].u]!=col[e[i].v])  
    28         {  
    29            tot++;  
    30            d[e[i].u]++,d[e[i].v]++;  
    31         }  
    32     }  
    33     for(int i=1;i<=n;i++)  
    34     {  
    35         if(d[i]==tot)  
    36         {  
    37             printf("YES
    %d
    ",i);  
    38             return 0;  
    39         }  
    40     }  
    41     printf("NO
    ");  
    42    return 0;  
    43 }  
    
    
  • 相关阅读:
    JavaScript实现的7种排序算法
    ECMAScript 2021 正式确认
    win10激活方法
    数据结构之Set | 让我们一块来学习数据结构
    使用jenkins一键打包发布vue项目
    数据结构之LinkedList | 让我们一块来学习数据结构
    数据结构之Queue | 让我们一块来学习数据结构
    数据结构之Stack | 让我们一块来学习数据结构
    数据结构之List | 让我们一块来学习数据结构
    JavaScript中的new,bind,call,apply的原理及简易实现
  • 原文地址:https://www.cnblogs.com/wydxry/p/7243060.html
Copyright © 2011-2022 走看看