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. InputThe 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. OutputPrint “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.
在有根树上对每个节点维护 down,up,f,g 表示子树颜色是否各自相同、父亲和往上颜色是否相同、自己和所有子树的颜色【不一致为-1】、自己和上面的颜色【不一致为-1】。dfs两遍,对于 up 需要大力讨论一下。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fir[100010],ne[200010],to[200010],c[100010],
n,fa[100010],down[100010],f[100010],up[100010],g[100010],
flag,cnt,f2,c2;
void add(int num,int u,int v)
{
ne[num]=fir[u];
fir[u]=num;
to[num]=v;
}
void dfs1(int u)
{
int i,v;
f[u]=c[u];
for (i=fir[u];i;i=ne[i])
if ((v=to[i])!=fa[u])
{
fa[v]=u;
dfs1(v);
if (f[v]==-1) down[u]=-1;
if (f[u]!=f[v]) f[u]=-1;
}
}
void solve(int x)
{
if (flag==-1) return;
if (x==-1)
{
c2++;
return;
}
if (flag==0||x==flag)
{
flag=x;
cnt++;
}
else
{
if (f2==0) f2=x;
else
{
if (f2==x&&cnt==1)
{
cnt=2;
swap(f2,flag);
}
else flag=-1;
}
}
}
void dfs2(int u)
{
int i,v;
if (up[u]==c[u]||!up[u]) g[u]=c[u];
else g[u]=-1;
flag=cnt=f2=c2=0;
solve(g[u]);
for (i=fir[u];i;i=ne[i])
if ((v=to[i])!=fa[u])
solve(f[v]);
if (flag==-1||c2>1||(c2==1&&f2))
{
for (i=fir[u];i;i=ne[i])
if (to[i]!=fa[u]) up[to[i]]=-1;
}
else
{
if (c2==1)
{
for (i=fir[u];i;i=ne[i])
if (to[i]!=fa[u])
{
if (f[to[i]]==-1) up[to[i]]=flag;
else up[to[i]]=-1;
}
}
else
{
if (!f2) {for (i=fir[u];i;i=ne[i]) if (to[i]!=fa[u]) up[to[i]]=flag;}
else
{
if (cnt==1) {for (i=fir[u];i;i=ne[i]) if (to[i]!=fa[u]) up[to[i]]=g[u];}
else
{
for (i=fir[u];i;i=ne[i])
if (to[i]!=fa[u])
{
if (f[to[i]]==f2) up[to[i]]=flag;
else up[to[i]]=-1;
}
}
}
}
}
for (i=fir[u];i;i=ne[i])
if (to[i]!=fa[u])
dfs2(to[i]);
}
int main()
{
int i,u,v;
scanf("%d",&n);
for (i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(i*2,u,v);
add(i*2+1,v,u);
}
for (i=1;i<=n;i++)
scanf("%d",&c[i]);
dfs1(1);
dfs2(1);
for (i=1;i<=n;i++)
if (up[i]!=-1&&down[i]!=-1)
{
printf("YES\n%d\n",i);
return 0;
}
printf("NO\n");
}