• 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 }
  • 相关阅读:
    OpenCV 在android studio 中的用法
    Python中用requests处理cookies的3种方法
    Jmeter利用正则表达式提取器提取登录cookie供下一步使用
    vue项目 el-tree的界面自定义 实现增删改查操作
    PostgreSQL高可用方案-patroni+etcd+vipmanager(二)
    PostgreSQL高可用方案-patroni+etcd+vipmanager(一)
    【转载】Linux下PostgreSQL主备环境搭建和切换
    一些自定义 PostgreSQL 随机数据生成器 —— Some self-defined PostgreSQL random data generators
    ClickHouse 简单使用(六)
    ClickHouse 简单使用(五)
  • 原文地址:https://www.cnblogs.com/z-712/p/7324456.html
Copyright © 2020-2023  润新知