Timofey and a tree CodeForces - 763A(树形dp)

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 c i.

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 c 1, c 2, …, c n (1 ≤ c i ≤ 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

题意:
询问是否有点,其每个子树的所有点颜色都相同

思路:
还是按照换根的思路写的。
定义f[u]f[u]代表uu子树的所有点颜色都相同。
跑完第一遍递归算出f[u]f[u]

然后从根节点往下遍历,如果满足当前点所有子树f[v]=1f[v]=1,则这个点可行。
否则最多只有一个子树满足f[v]=0f[v]=0,并且当前a[u]=a[fa]a[u]=a[fa],这样除去当前子树的所有点颜色相同,递归vv点。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <cmath>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 7;

vector<int>G[maxn];
int a[maxn],ind[maxn],f[maxn],flag[maxn];

void dfs(int u,int fa) {
    f[u] = 1; //代表与子树完全相同
    for(int i = 0;i < G[u].size();i++) {
        int v = G[u][i];
        if(v == fa) continue;
        dfs(v,u);
        if(f[v] != 1 || a[u] != a[v]) {
            f[u] = 0;
        }
    }
}

int dfs2(int u,int fa,int last) {
    int cnt1 = 0,cnt2 = 0,nex = 0;
    for(int i = 0;i < G[u].size();i++) {
        int v = G[u][i];
        if(v == fa) continue;
        if(f[v] == 1) {
            cnt1++; //多少个子树相同
            if(a[u] == a[v]) {
                cnt2++; //根节点和多少个子树相同
            }
        } else {
            nex = v;
        }
    }
    
    int num = G[u].size();
    if(u != 1) num--;
    
    if(cnt1 == num) {
        return u;
    }
    
    if(cnt2 == num - 1 && a[u] == last) {
        int num = dfs2(nex,u,a[u]);
        if(num != -1) return num;
    }
    
    return -1;
}

int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i < n;i++) {
        int x,y;scanf("%d%d",&x,&y);
        G[x].push_back(y);G[y].push_back(x);
    }
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    dfs(1,-1);

    int ans = dfs2(1,-1,a[1]);
    if(ans != -1) {
        printf("YES\n%d\n",ans);
    } else {
        printf("NO\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章