zoukankan      html  css  js  c++  java
  • codeforces 764C【dfs】

    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 ≤ n, u ≠ 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 <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <bitset>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <cmath>
    10 #include <list>
    11 #include <set>
    12 #include <map>
    13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
    14 #define per(i,a,b) for(int i = a;i >= b;-- i)
    15 #define mem(a,b) memset((a),(b),sizeof((a)))
    16 #define FIN freopen("in.txt","r",stdin)
    17 #define FOUT freopen("out.txt","w",stdout)
    18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
    19 #define mid ((l+r)>>1)
    20 #define ls (id<<1)
    21 #define rs ((id<<1)|1)
    22 #define N 100010
    23 #define INF 0x3f3f3f3f
    24 #define INFF ((1LL<<62)-1)
    25 using namespace std;
    26 typedef long long LL;
    27 typedef pair<int, int> PIR;
    28 const double eps = 1e-8;
    29 
    30 int n, u, v, a[N], ok, st;
    31 bool vis[N];
    32 vector <int> G[N];
    33 map <int, bool> can[N];
    34 
    35 void dfs(int p, int col){
    36     //if(vis[p])    return ;
    37     //vis[p] = true;
    38     rep(i, 0, (int)G[p].size()-1){
    39         int v = G[p][i];
    40         if(vis[v])    continue;
    41         if(can[p][v] && col)     { ok = 0; return ;}
    42         
    43         if(col == 0 || (col != 0 && a[v] == col)){
    44             vis[v] = true;
    45             dfs(v, a[v]);
    46             if(ok == 0)     { can[p][v] = true; return ; }
    47             vis[v] = false;
    48         }
    49         else{
    50             can[p][v] = true;
    51             ok = 0;
    52             return ;
    53         }
    54     }
    55 }
    56 int main()
    57 {IO;
    58     //FIN;
    59     while(cin >> n){
    60         rep(i, 0, n)    G[i].clear();
    61         rep(i, 0, n)    can[i].clear();
    62         rep(i, 1, n-1){
    63             cin >> u >> v;
    64             G[u].push_back(v);
    65             G[v].push_back(u);
    66         }
    67         rep(i, 1, n)    cin >> a[i];
    68         
    69         int ans = -1;
    70         rep(i, 1, n){
    71             ok = 1;
    72             mem(vis, false);
    73             vis[i] = true;
    74             dfs(i, 0);
    75 
    76             if(ok)    { ans = i; break; }
    77         }
    78         if(ans == -1){
    79             cout << "NO" << endl;
    80         }
    81         else{
    82             cout << "YES" << endl;
    83             cout << ans << endl;
    84         }
    85     }
    86     retur
    View Code
  • 相关阅读:
    分页 存储过程
    Base64编码
    汉字转拼音 完整类
    C#利用SharpZipLib解压或压缩文件夹实例操作
    C#压缩解压zip 文件
    MapReduce shuffle原理
    设计模式(一)—— 代理模式
    Lombok的基本使用
    解决idea 每次新建项目需要重新配置maven
    10-20 Spring框架(三)—— AOP核心
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6363429.html
Copyright © 2011-2022 走看看