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 ≤ 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 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} ≤ 10^5), 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

Difficulty: competition

Starter code:

Solutions:
def main():
    n = int(input())
    edges = []
    for _ in range(n - 1):
        u, v = list(map(int, input().split()))
        u -= 1
        v -= 1
        edges.append((u, v))

    colors = list(map(int, input().split()))
    suspect = [(u, v) for (u, v) in edges if colors[u] != colors[v]]

    if len(suspect) == 0:
        print("YES")
        print(1)
    else:
        cands = set(suspect[0])
        for u, v in suspect:
            cands &= set([u, v])

        if len(cands) == 0:
            print("NO")
        else:
            print("YES")
            e = list(cands)[0]
            print(e + 1)

main()


import sys

class UnionFind:
    def __init__(self, sz):
        self.__ranks   = [1] * sz
        self.__sizes   = [1] * sz
        self.__parents = [ i for i in range(sz) ]

    def find_parent(self, x):
        if x == self.__parents[x]:
            return x
        else:
            self.__parents[x] = self.find_parent( self.__parents[x] )
            return self.__parents[x]

    def same(self, x, y):
        return self.find_parent(x) == self.find_parent(y)

    def unite(self, x, y):
        px = self.find_parent(x)
        py = self.find_parent(y)
        if px == py:
            return
        if self.__ranks[px] > self.__ranks[py]:
            self.__parents[py] = px
            self.__sizes[px] += self.__sizes[py]
        else:
            self.__parents[px] = py
            self.__sizes[py] += self.__sizes[px]
            if self.__ranks[px] == self.__ranks[py]:
                self.__ranks[py] += 1

    def size(self, n):
        return self.__sizes[n]
#### 

def main():
    n = int(input())

    edge = {}
    for i in range(n - 1):
        u,v = list(map(int, sys.stdin.readline().split()))
        if u not in edge:
            edge[u] = []
        if v not in edge:
            edge[v] = []
        edge[u].append(v)
        edge[v].append(u)
    
    colors = [-1] * (n + 1)
    for i,c in enumerate(map(int, sys.stdin.readline().split())):
        colors[i + 1] = c

    uf = UnionFind(n + 1)
    for u in list(edge.keys()):
        for v in edge[u]:
            if colors[u] == colors[v]:
                uf.unite(u,v)

    tree = set()
    for v in range(1,n+1):
        tree.add(uf.find_parent(v))

    target_v = -1
    ok = False
    for u in range(1,n+1):
        cnt = set()
        for v in edge[u]:
            cnt.add(uf.find_parent(v))
        if len(cnt) == len(tree) - (1 if uf.size(uf.find_parent(u)) == 1 else 0):
            ok = True
            target_v = u
            break

    if ok:
        print("YES")
        print(target_v)
    else:
        print("NO")

def __starting_point():
    main()



__starting_point()

n = int(input())
e = [list(map(int, input().split())) for i in range(n - 1)]
c = list(map(int, input().split()))
ec, v = [0] * n, 0
for ea, eb in e:
    if c[ea - 1] != c[eb - 1]:
        ec[ea - 1] += 1
        ec[eb - 1] += 1
        v += 1
if v == max(ec):
    print('YES', ec.index(v) + 1, sep='\n')
else:
    print('NO')

n = int(input())
a = []
b = []
for i in range(n-1):
	x,y = list(map(int, input().split()))
	a.append(x)
	b.append(y)

colors = list(map(int, input().split()))


lehet = []
for i in range(n-1):
	if colors[a[i]-1] != colors[b[i]-1]:
		if len(lehet) == 0:
			lehet += [a[i], b[i]]
		else:
			if a[i] in lehet and b[i] in lehet:
				print("NO")
				return
			elif a[i] in lehet:
				lehet = [a[i]]
			elif b[i] in lehet:
				lehet = [b[i]]
			else:
				print("NO")
				return
print("YES")
if len(lehet) == 0:
	lehet = [1]
print(lehet[0])
	


n=int(input())

e=[]

v=[]

for i in range(n-1):

    e.append(list(map(int,input().split())))

c=[-1]+list(map(int,input().split()))

for i in e:

    if c[i[0]]!=c[i[1]]: v.append(i)

if not v:

    print('YES')

    print(1)

else:

    s=set(v[0]);list([s.intersection_update(set(x)) for x in v])

    if s:

        print('YES')

        print(list(s)[0])

    else: print('NO')



# Made By Mostafa_Khaled


import math

graph = []
colors = []


def dfs(u, p, color) -> bool:
    if color != colors[u]:
        return False
    for v in graph[u]:
        if v == p:
            continue
        if not dfs(v, u, color):
            return False
    return True


def check(u):
    for v in graph[u]:
        if not dfs(v, u, colors[v]):
            return False
    return True


def main():
    n = int(input())
    for _ in range(n):
        graph.append([])
    for i in range(n-1):
        u, v = [int(x) - 1 for x in input().split()]
        graph[u].append(v)
        graph[v].append(u)
    nonlocal colors
    colors += list(map(int, input().split()))
    for u in range(n):
        for v in graph[u]:
            if colors[u] != colors[v]:
                if check(u):
                    print("YES")
                    print(u + 1)
                    return
                elif check(v):
                    print("YES")
                    print(v + 1)
                    return
                else:
                    print("NO")
                    return
    print("YES")
    print(1)

main()


import sys
sys.setrecursionlimit(10**9)
def dfs(a,pre=-1,par=None):
    nonlocal aa
    for i in adj[a]:
      #  print(i,pre,a,par)
        if i==par:
            continue
        if pre==-1:
            dfs(i,it[i],a)
        elif pre!=it[i]:
            aa=True
            return ''
        else:
            dfs(i,it[i],a)
            
n=int(input())
ed=[]
adj=[[] for i in range(n)]

for _ in range(n-1):
    a,b=map(int,input().split())
    ed.append([a-1,b-1])
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)
it=list(map(int,input().split()))
st=False
for i in ed:
    if it[i[0]]!=it[i[1]]:
        st=True
        e=i
        break
if not(st):
    print("YES")
    print(1)
else:
    ans=True
    aa=False
    dfs(e[0])
   # print(aa)
    if aa==False:
        print("YES",e[0]+1,sep="\n")
    else:
        aa=False
        dfs(e[1])
        if aa==False:
            print("YES")
            print(e[1]+1)
        else:
            print("NO")

    
    


n = int(input())
es = []
rs = []
for i in range(n-1) :
    es.append(list(map(int , input().split())))
vs = list(map(int , input().split()))
for i in es :
    if vs[i[0] -1 ] != vs[i[1] - 1 ] :
        rs.append(i)
if rs == [] :
    print('YES')
    print(1)
    return
for r1 in rs[0] :
    for i in rs :
        if r1 != i[0] and r1 != i[1] :
            break
    else :
        print('YES')
        print(r1)
        return
print('NO')

n = int(input())
u = []
v = []
for i in range(n-1):
    a, b = map(int, input().split())
    u.append(a)
    v.append(b)
 
c = [0] + [int(x) for x in input().split()]
 
e = 0
dic = {}
 
for i in range(1, n+1):
    dic[i] = 0
 
def plus(dic, n):
    if n in dic:
        dic[n] += 1
    else:
        dic[n] = 1
 
for i in range(n-1):
    if c[u[i]] != c[v[i]]:
        e += 1
        dic[u[i]] += 1
        dic[v[i]] += 1
 
for i in range(1, n+1):
    if dic[i] == e:
        print ('YES', i,sep='\n')
        return
 
print ("NO")


n = int(input())
u,v = [], []
for i in range(n-1):
    a, b = list(map(int, input().split()))
    u.append(a)
    v.append(b)
 
colors = [0] + [int(x) for x in input().split()]
 
maximumDifference = 0
differences = {i:0 for i in range(1, n+1)}

for i in range(n-1):
    vertex1 = u[i]
    vertex2 = v[i]
    
    if colors[vertex1] != colors[vertex2]:
        maximumDifference += 1
        differences[vertex1] += 1
        differences[vertex2] += 1

if maximumDifference in list(differences.values()):
    print ('YES')
    print(list(differences.values()).index(maximumDifference)+1)
else:
    print ("NO")


n = int(input())
arr = []
brr = []
for i in range(n-1):
    u,v = list(map(int,input().split()))
    arr.append(u)
    brr.append(v)
color = list(map(int,input().split()))

ans = []
for i in range(n-1):
    if color[arr[i]-1]!=color[brr[i]-1]:
        if ans==[]:
            ans+=[arr[i],brr[i]]
        else:
            if arr[i] in ans and brr[i] in ans:
                print("NO")
                return
            elif arr[i] in ans:
                ans = [arr[i]]
            elif brr[i] in ans:
                ans = [brr[i]]
            else:
                print("NO")
                return
print("YES")
if len(ans)==0:
    ans.append(1)
print(ans[0])

import math as mt 
import sys,string
input=sys.stdin.readline
import random
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : list(map(int,input().split()))
I=lambda :int(input())
def bfs(a):
    w=g[a]
    v=[0]*n
    for i in w:
        v[i]=1
    v[a]=1
    j=0
    while(j<len(w)):
        k=w[j]
        for i in g[k]:
            if(v[i]==0):
                if(l[i]==l[k]):
                    w.append(i)
                    v[i]=1
                else:
                    return False
        j+=1
    return True
n=I()
g=[]
for i in range(n):
    g.append([])
x=[]
for _ in range(n-1):
    a,b=M()
    a-=1
    b-=1
    x.append((a,b))
    g[a].append(b)
    g[b].append(a)
l=L()
for i in range(n-1):
    if(l[x[i][0]]!=l[x[i][1]]):
        if(bfs(x[i][0])):
            print("YES")
            print(x[i][0]+1)
            return
        if(bfs(x[i][1])):
            print("YES")
            print(x[i][1]+1)
            return
        else:
            print("NO")
            return
print("YES")
print("1")


import collections
import sys


def read_ints():
    return [int(x) for x in sys.stdin.readline().strip().split()]


def main():
    N = read_ints()[0]
    edges = []
    vx_to_neighbor_colors = collections.defaultdict(set) 
    for _ in range(N - 1):
        u, v = read_ints()
        u -= 1
        v -= 1
        edges.append((u, v))
    colors = read_ints()

    vx_to_cnt = collections.Counter()
    e_cnt = 0
    for e in edges:
        if colors[e[0]] == colors[e[1]]:
            continue
        vx_to_cnt[e[0]] += 1
        vx_to_cnt[e[1]] += 1
        e_cnt += 1

    if e_cnt == 0:
        return 1  # any
    if e_cnt == 1:
        return 1 + list(vx_to_cnt.keys())[0]

    root = None
    for vx, cnt in list(vx_to_cnt.items()):
        if cnt == e_cnt:
            if root is not None:
                return None
            root = 1 + vx
        elif cnt != 1:
            return None
    return root


def __starting_point():
    root = main()
    if root is not None:
        print(f'YES\n{root}')
    else:
        print('NO')

__starting_point()

n = int(input())
edges = list(list(map(int, input().split())) for _ in range(n-1)) 
colors = list(map(int, input().split()))

edge_cnt = 0
pnt_cnt = {0: 0}
for a, b in edges:
    a -= 1 ; b -= 1
    if colors[a] != colors[b]:
        edge_cnt += 1
        pnt_cnt[a] = pnt_cnt.get(a, 0)+1
        pnt_cnt[b] = pnt_cnt.get(b, 0)+1

for k, v in list(pnt_cnt.items()):
    if v == edge_cnt:
        print("YES")
        print(k+1)
        break
else:
    print("NO")


# import io, os
# input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
# sys.stdin=open('input.txt','r')
# sys.stdout=open('output.txt','w')
input=sys.stdin.readline
# sys.setrecursionlimit(300010)
MOD = 1000000007
MOD2 = 998244353
ii = lambda: int(input().strip('\n'))
si = lambda: input().strip('\n')
dgl = lambda: list(map(int,input().strip('\n')))
f = lambda: map(int, input().strip('\n').split())
il = lambda: list(map(int, input().strip('\n').split()))
ls = lambda: list(input().strip('\n'))
lsi = lambda: [int(i) for i in ls()]
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(1):
    n=ii()
    edges=[il() for i in range(n-1)]
    clr=[0]+il()
    deg=[0]*(n+1)
    tot=0
    for i in edges:
        if clr[i[0]]!=clr[i[1]]:
            deg[i[0]]+=1
            deg[i[1]]+=1
            tot+=1
    for i in range(1,n+1):
        if deg[i]==tot:
            print('YES\n'+str(i));return
    print('NO')

from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n=int(input())
a=[]
b=[]
for i in range(n-1):
	x,y=map(int,input().split())
	a.append(x)
	b.append(y)
l=list(map(int,input().split()))
ans=[]
for i in range(n-1):
	if l[a[i]-1]!=l[b[i]-1]:
		if len(ans)==0:
			ans=[a[i],b[i]]
		else:
			if a[i] in ans and b[i] in ans:
				print("NO")
				return
			elif a[i] in ans:
				ans=[a[i]]
			elif b[i] in ans:
				ans=[b[i]]
			else:
				print("NO")
				return
print("YES")
if ans:
	print(ans[0])
else:
	print(1)