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

    Description

    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

    题意:给了一棵树,每个节点都有颜色,找到一个点作为根,往下子树都是相同颜色的

    解法:弱鸡dfs超时,后来统计相邻节点颜色不同的对数,同时相邻节点也记录自己与相邻节点颜色不同的个数,然后找到。。。节点颜色不同的个数等于对数,就可以作为根了

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int m;
     4 int x[200000],y[200000];
     5 int color[200000];
     6 map<int,int>q,p;
     7 int main()
     8 {
     9     int n;
    10     cin>>n;
    11     for(int i=1;i<=n-1;i++)
    12     {
    13         cin>>x[i]>>y[i];
    14     }
    15     for(int i=1;i<=n;i++)
    16     {
    17         cin>>color[i];
    18     }
    19     for(int i=1;i<=n-1;i++)
    20     {
    21         if(color[x[i]]!=color[y[i]])
    22         {
    23             q[x[i]]++;
    24             q[y[i]]++;
    25             m++;
    26         }
    27     }
    28     for(int i=1;i<=n;i++)
    29     {
    30         if(q[i]==m)
    31         {
    32             cout<<"YES"<<endl;
    33             cout<<i<<endl;
    34             return 0;
    35         }
    36     }
    37     cout<<"NO"<<endl;
    38     return 0;
    39 }
  • 相关阅读:
    笔试题 9.11
    shell脚本编程
    android源码中用到的设计模式
    struts2.0的工作流程
    无奈的信息产业部备案网站流程
    VS2005调试时弹出“无法附加。绑定句柄无效”对话框
    彻底删除项目的VSS源代码管理信息
    软件版本详细介绍
    导入SourceSafe过程中项目结构无法修改问题的解决方案
    建立良好的客户关系=节约项目成本
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6602141.html
Copyright © 2011-2022 走看看