Description
Each New year Timofey and he friends cut down a tree of n vertices and bring it home. After this they paint all of the n its vertices and so that the i-th vertex color ci.
Now it's time for Timofey birthday, and he mother asked him to remove the tree. Timofey removes the "tree" in the following way:he takes some vertex in hands At the "Tree becomes rooted" at the chosen vertex. After that Timofey brings the trash can.
Timofey doesn ' t like it when many colors are mixing. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which him should take in hands, so, there no are that subtrees. He doesn ' t consider the whole, as a subtree since he can ' t, the color of the root vertex.
A subtree of some vertex is a subgraph containing the vertex and all its descendants.
Your task is to determine if there are a vertex, taking which in hands Timofey ' t be wouldn.
Input
The contains single integer n (2≤n≤10^5)-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 are an edge between vertic Es u and v. It is guaranteed this given graph is a tree.
The next line contains n integers c1, c2, ..., CN (1≤ci≤10^5), denoting the colors of the vertices.
Output
Print "NO" in a single line, if Timofey can ' t take the ' such a way ' it doesn ' t annoy him.
Otherwise print "YES" in the "the". 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
examples Output
YES
2
the
Given a tree, each node in the tree has its own fixed color, asking if a node can be found so that each subtree connected to that node contains only one color.
train of Thought
If you think about it, we just need to find the point with the largest number of adjacent points in the different colors of the current node, and then judge the subtree that is connected to it.
YES if each of the other subtrees contains only one color, otherwise NO.
AC Code
#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
const int MAXN = 1E5+10;
typedef __int64 LL;
struct node {int to;
int next;
} edge[maxn<<1];
int Head[maxn],tot;
int COLOR[MAXN];
int N,now;
BOOL flag = TRUE;
void Init () {memset (head,-1,sizeof head);
tot = 0;
} void Addedge (int u,int v) {edge[tot].to = v;
Edge[tot].next = Head[u];
Head[u] = tot++;
} void Dfs (int x,int fa) {if (Color[x]!=now) {flag = false;
Return
for (int i=head[x]; I!=-1 i=edge[i].next) {int to = edge[i].to;
if (TO==FA) continue;
DFS (TO,X);
if (!flag) return;
int main () {IO;
Init ();
cin>>n;
for (int i=1; i<n; i++) {int u,v;
cin>>u>>v;
Addedge (U,V);
Addedge (V,u); for (int i=1; i<=n; i++) {Cin>>coloR[i];
int ANSV = -1,MAXV =-1;
for (int i=1; i<=n; i++) {int tmp = 0;
for (int j=head[i]; J!=-1 j=edge[j].next) {if (color[edge[j].to]!=color[i)) tmp++;
} if (TMP>MAXV) {MAXV = tmp;
ANSV = i;
(int i=head[ansv]; I!=-1 i=edge[i].next) {now = color[edge[i].to];
DFS (EDGE[I].TO,ANSV);
if (!flag) {cout<< "NO" <<endl;
Break
} if (flag) cout<< "YES" <<endl<<ansv<<endl;
return 0; }