zoukankan      html  css  js  c++  java
  • Round #395 C. 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 integersu 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.

    Example

    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 //
     2 #include <iostream>
     3 #include <stdio.h>
     4 #include <string.h>
     5 using namespace std;
     6 const int maxn=100005;
     7 struct node{
     8     int u,v;
     9 }a[maxn];
    10 int degree[maxn],c[maxn];
    11 int main(){
    12     int n;
    13     scanf("%d",&n);
    14     for(int i=1;i<n;i++)
    15         scanf("%d%d",&a[i].u,&a[i].v);
    16     for(int i=1;i<=n;i++)
    17         scanf("%d",&c[i]);
    18     int ans=0;
    19     memset(degree,0,sizeof(degree));
    20     for(int i=1;i<n;i++){
    21         if(c[a[i].u]!=c[a[i].v]){
    22             ans++;
    23             degree[a[i].u]++;
    24             degree[a[i].v]++;
    25         }
    26     }
    27     for(int i=1;i<=n;i++){
    28         if(ans==degree[i]) {
    29             printf("YES
    %d
    ",i);
    30             return 0;
    31         }
    32     }
    33     printf("NO
    ");
    34      return 0;
    35 }
  • 相关阅读:
    SQL 学习笔记<六> MySQL存储过程基础
    SQL 学习笔记<三> SELECT之连接查询
    六、Delphi10.3通过Json.Serializers单元对大量数据序列化
    七、Delphi10.3读取JSON数组
    五、Delphi10.3通过REST单元使类和JSON数据互相转换
    四、Delphi10.3读取JSON数据
    python格式化输出
    python基础知识02
    发送邮件实例
    python基础知识03
  • 原文地址:https://www.cnblogs.com/z-712/p/7324456.html
Copyright © 2011-2022 走看看