• Codeforces Round #395 (Div. 2) C. Timofey and a tree


    地址:http://codeforces.com/contest/764/problem/C

    题目:

    C. 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 ≤ 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

     题意:给你一棵n个节点的树,每个节点都染了色,现在让你找出存不存在一个节点作为根,使不包含根节点的子树的所有节点颜色都一样。

    思路:直接枚举出哪些边的两端节点颜色不一样,然后判断这些边是否有相同一个端点,有的话则这个相同点是根,否则不存在。注意点所有边的端点颜色都一样时要特判。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int n,num,cl[K],u[K],v[K],ex,ey,fx,fy,ans;
    15 PII ed[K];
    16 int main(void)
    17 {
    18     cin>>n;
    19     for(int i=1;i<n;i++)
    20         scanf("%d%d",&u[i],&v[i]);
    21     for(int i=1;i<=n;i++)
    22         scanf("%d",&cl[i]);
    23     for(int i=1;i<n;i++)
    24         if(cl[u[i]]!=cl[v[i]])
    25             ed[++num]=MP(u[i],v[i]);
    26     if(num==0)
    27     {
    28         printf("YES
    1
    ");
    29         return 0;
    30     }
    31     ex=ed[1].first,ey=ed[1].second;
    32     fx=fy=ans=1;
    33     for(int i=2;i<=num;i++)
    34     {
    35         if(fx && !(ex==ed[i].first||ex==ed[i].second)) fx=0;
    36         if(fy && !(ey==ed[i].first||ey==ed[i].second)) fy=0;
    37         if(!(fx||fy))
    38         {
    39              ans=0;break;
    40         }
    41     }
    42     if(ans)
    43     {
    44         printf("YES
    ");
    45         if(fx)
    46             printf("%d
    ",ex);
    47         else
    48             printf("%d
    ",ey);
    49     }
    50     else
    51         printf("NO
    ");
    52     return 0;
    53 }

     

     

  • 相关阅读:
    工作中简单又实用的vim配置
    宏定义的专业写法(工作中常用)
    17八皇后问题
    04文件与IO
    响应式布局编码
    静态布局,自适应布局,流体式布局,响应式布局概念
    CSS:<link>标签rel和media的解释
    我想去的公司的入职要求
    JS:引用类型,基本类型
    Android Launcher 详解
  • 原文地址:https://www.cnblogs.com/weeping/p/6362476.html
Copyright © 2020-2023  润新知