Codeforces Round #395 (Div. 2) -- C. Timofey and a tree(并查集+缩点)

来源:互联网 发布:苹果手机芒果tv没网络 编辑:程序博客网 时间:2024/05/22 15:08

大体题意:

给你一棵树,要求找一个结点来作为根节点,使得所有的子树颜色都一样,整棵树不是子树,其余的是,存在的话输出那个点。

思路:

并查集+缩点。

先把颜色一样的,并且连在一起的点缩成一个点。(类似连通块) 缩点后也一定是树。

这样最后的形式就是  一个根结点连接着很多其他结点,这样枚举所有的边,统计一下缩点的度数。

找出那个度数为总和-1的那个点就是答案,没有的话,就是NO咯。

#include <bits/stdc++.h>#define ps push_back#define fi first#define se second#define mr make_pairusing namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-10;const double pi = acos(-1.0);typedef long long ll;typedef unsigned long long ULL;const int maxn = 100000 + 10;vector<int>g[maxn];int c[maxn];int d[maxn];int n;struct Node{    int f,t;    void read(){        scanf("%d %d",&f, &t);    }}p[maxn];int de[maxn];int fa[maxn];int cnt = 0;void dfs(int cur,int la){ ///dfs 进行缩点。    de[cur] = 1;    fa[cur] = cnt;    for (int i = 0; i < g[cur].size(); ++i){        int v = g[cur][i];        if (v!=la && c[v]==c[cur]){            dfs(v,cur);        }    }}int main(){    scanf("%d",&n);    memset(fa,-1,sizeof fa);    for (int i = 0; i < n-1; ++i){        p[i].read();/// 记录边        int u = p[i].f;        int v = p[i].t;        g[u].ps(v);/// 便于dfs        g[v].ps(u);    }    for (int i = 1; i <= n; ++i) {        scanf("%d",c+i);    }    for (int i=  1; i <= n; ++i){        if (!de[i]){/// 已经加入连通块的点 就不在dfs了。            cnt++;            dfs(i,-1);        }    }    memset(de,0,sizeof de);    bool ok = 1;    for (int i = 0; i < n-1; ++i){        int u = p[i].f;        int v = p[i].t;        int fu = fa[u];        int fv = fa[v];        if (fa[u] != fa[v]){            d[u]++;///统计度数            d[v]++;        }    }    for (int i = 1 ; i <= n; ++i){        if (d[i] == cnt-1) return 0 * printf("YES\n%d\n",i);    }    puts("NO");///这里的NO 是找不到那个根结点,度数为所有点-1.    return 0;}


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 don't like when many colors 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
41 22 33 41 2 1 1
output
YES2
input
31 22 31 2 3
output
YES2
input
41 22 33 41 2 1 2
output
NO


0 0
原创粉丝点击