Problem:
 Score : 1600 points    Problem Statement There is a tree with N vertices, numbered 1 through N .
The i -th of the N-1 edges connects vertices a_i and b_i .  Currently, there are A_i stones placed on vertex i .
Takahashi and Aoki will play a game using this tree.  First, Takahashi will select a vertex and place a piece on it.
Then, starting from Takahashi, they will alternately perform the following operation:   Remove one stone from the vertex currently occupied by the piece.  Then, move the piece to a vertex that is adjacent to the currently occupied vertex.   The player who is left with no stone on the vertex occupied by the piece and thus cannot perform the operation, loses the game.
Find all the vertices v such that Takahashi can place the piece on v at the beginning and win the game.      Constraints  2 ≦ N ≦ 3000  1 ≦ a_i,b_i ≦ N  0 ≦ A_i ≦ 10^9  The given graph is a tree.         Input The input is given from Standard Input in the following format:  N  A_1  A_2 … A_N  a_1  b_1 : a_{N-1}  b_{N-1}       Output Print the indices of the vertices v such that Takahashi can place the piece on v at the beginning and win the game, in a line, in ascending order.        Sample Input 1 3
1 2 3
1 2
2 3      Sample Output 1 2  The following is one possible progress of the game when Takahashi places the piece on vertex 2 :   Takahashi moves the piece to vertex 1 . The number of the stones on each vertex is now: (1,1,3) .  Aoki moves the piece to vertex 2 . The number of the stones on each vertex is now: (0,1,3) .  Takahashi moves the piece to vertex 1 . The number of the stones on each vertex is now: (0,0,3) .  Aoki cannot take a stone from the vertex, and thus Takahashi wins.        Sample Input 2 5
5 4 1 2 3
1 2
1 3
2 4
2 5      Sample Output 2 1 2       Sample Input 3 3
1 1 1
1 2
2 3      Sample Output 3   Note that the correct output may be an empty line.

Solution (Python):
import copy, sys
sys.setrecursionlimit(10 ** 6)

n = int(input())
A = [int(i) for i in input().split()]
edge = [[] for i in range(n)]

grundy = [-1] * n

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

def dfs(v):
    if grundy[v] != -1: return grundy[v]
    mex = [0] * (len(edge[v]) + 1)
    for vv in edge[v]:
        if A[vv] < A[v]: 
            gg = dfs(vv)
            if gg <= len(edge[v]): mex[gg] = 1
    for i in range(len(edge[v])+1):
        if mex[i] == 0:
            grundy[v] = i
            return i

for i in range(n):
    dfs(i)

ans = []
for i in range(n):
    if grundy[i] != 0: ans.append(i+1)

print(*ans)

Solution (Python):
import copy, sys
sys.setrecursionlimit(10 ** 6)

n = int(input())
A = [int(i) for i in input().split()]
edge = [[] for i in range(n)]

grundy = [-1] * n

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

def dfs(v):
    if grundy[v] != -1: return grundy[v]
    mex = [0] * (len(edge[v]) + 1)
    for vv in edge[v]:
        if A[vv] < A[v]: 
            gg = dfs(vv)
            if gg <= len(edge[v]): mex[gg] = 1
    for i in range(len(edge[v])+1):
        if mex[i] == 0:
            grundy[v] = i
            return i


ans = []
for i in range(n):
    if dfs(i) != 0: ans.append(i+1)

print(*ans)

Solution (Python):
import sys

input=sys.stdin.readline
sys.setrecursionlimit(2*10**5)

N=int(input())
A=list(map(int,input().split()))
edge=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

grundy=[-1]*N

def dfs(v):
    if grundy[v]!=-1:
        return grundy[v]

    mex=[False]*(len(edge[v])+1)
    for nv in edge[v]:
        if A[nv]<A[v]:
            g=dfs(nv)
            if g<=len(edge[v]):
                mex[g]=True
    for i in range(len(edge[v])+1):
        if not mex[i]:
            grundy[v]=i
            return i

ans=[]
for i in range(N):
    if dfs(i)!=0:
        ans.append(i+1)

print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
b = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(n-1):
	u, v = map(int, input().split())
	adj[u-1].append(v-1)
	adj[v-1].append(u-1)

def dfs(x, p):
	s = set()
	for v in adj[x]:
		if v != p and b[v] < b[x]:
			s.add(dfs(v, x))
	res = 0
	while res in s:
		res += 1
	return res

ans = []
for i in range(n):
	if dfs(i, -1) != 0:
		ans.append(i+1)
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
b = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(n-1):
	u, v = map(int, input().split())
	adj[u-1].append(v-1)
	adj[v-1].append(u-1)

def dfs(x, p):
	s = set()
	for v in adj[x]:
		if v != p and b[v] < b[x]:
			s.add(dfs(v, x))
	res = 0
	while res in s:
		res += 1
	return res

ans = []
for i in range(n):
	if dfs(i, -1) != 0:
		ans.append(i+1)
print(*ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10000000)
n=int(input())
a=list(map(int,input().split()))
edge=[[]for _ in range(n)]
for x,y in[list(map(int,input().split()))for _ in range(n-1)]:
  edge[x-1].append(y-1)
  edge[y-1].append(x-1)
def bfs(r,v):
  for i in edge[v]:
    if(r!=i)and(a[v]>a[i])and(not(bfs(v,i))):return True
  return False
print(*[i+1 for i in range(n)if bfs(-1,i)])

Solution (Python):
n=int(input());a=list(map(int,input().split()));e=[[]for _ in[0]*n]
for x,y in[map(int,input().split())for _ in[0]*(n-1)]:e[x-1].append(y-1);e[y-1].append(x-1)
def b(r,v):return sum((r!=i)and(a[v]>a[i])and(not(b(v,i)))for i in e[v])
print(*[i+1for i in range(n)if b(n,i)])

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10000000)
n=int(input())
a=list(map(int,input().split()))
edge=[[]for _ in range(n)]
for x,y in[list(map(int,input().split()))for _ in range(n-1)]:
  edge[x-1].append(y-1)
  edge[y-1].append(x-1)
def bfs(r,v):
  for i in edge[v]:
    if(r!=i)and(a[v]>a[i])and(not(bfs(v,i))):return True
  return False
print(*[i+1 for i in range(n)if bfs(-1,i)])

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
stone = [0]+list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
deg = [0]*(n+1)
for a,b in ab:
  if stone[a] < stone[b]:
    graph[a].append(b)
    deg[b] += 1
  elif stone[a] > stone[b]:
    graph[b].append(a)
    deg[a] += 1
que = deque()
dp = [[] for i in range(n+1)]
wl = [0]*(n+1)
ans = []
for i in range(1,n+1):
  if deg[i] == 0:
    que.append(i)
    wl[i] = 0
while que:
  x = que.popleft()
  if dp[x]:
    if dp[x].count(0) >= 1:
      ans.append(x)
      wl[x] = 1
  for y in graph[x]:
    if deg[y]:
      dp[y].append(wl[x])
      deg[y] -= 1
    if deg[y] == 0:
      que.append(y)
ans.sort()
print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
stone = [0]+list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
deg = [0]*(n+1)
for a,b in ab:
  if stone[a] < stone[b]:
    graph[a].append(b)
    deg[b] += 1
  elif stone[a] > stone[b]:
    graph[b].append(a)
    deg[a] += 1
que = deque()
dp = [[] for i in range(n+1)]
wl = [0]*(n+1)
ans = []
for i in range(1,n+1):
  if deg[i] == 0:
    que.append(i)
    wl[i] = 0
while que:
  x = que.popleft()
  if dp[x]:
    if dp[x].count(0) >= 1:
      ans.append(x)
      wl[x] = 1
  for y in graph[x]:
    if deg[y]:
      dp[y].append(wl[x])
      deg[y] -= 1
    if deg[y] == 0:
      que.append(y)
ans.sort()
print(*ans)

Solution (Python):

"""
Writer : SPD_9X2
https://atcoder.jp/contests/agc010/tasks/agc010_f

Nが小さいので、各頂点に関して木を探索できる
つまり、この頂点に置いた時勝てるか？がわかれば解ける

まず2頂点で考えると、Aがもう片方以上の方に置けばかつ
→値の大小が重要そう

置いた点から小さい方に下っていくのが最適。

●→高橋
〇→青木とする

●-〇-●-〇
小さい方に下って行って、周囲に自分より小さいノードが無かったら
先っぽのノードの持ち主が負けになる

複数小さいノードがある場合、一つでも勝ちがある場合勝てるノードになる
一つもない場合負け
→結局dfsしてあげればいい

"""
import sys
sys.setrecursionlimit(5000)
N = int(input())

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

lis = [ [] for i in range(N) ]

for i in range(N-1):

    a,b = map(int,input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)

def dfs(p,state): 

    temp = [0,0]

    for nex in lis[p]:

        if A[nex] < A[p]:
            temp[ dfs(nex,state^1) ] += 1

    if temp[state] > 0:
        return state
    else:
        return state ^ 1

ans = []
for i in range(N):

    if dfs(i,0) == 0:
        ans.append(i+1)

print (*ans)

Solution (Python):



from fractions import gcd

def readln():
    _res = list(map(int,str(input()).split(' ')))
    return _res

def calc(x,line):
    if line == 0:
        for l in v[x]:
            if e[l][0] == x: son = e[l][1]
            else: son = e[l][0]
            if a[son-1] < a[x-1] and calc(son,l) == -1:
                return 1
        return -1
    else:
        if e[line][0] == x: y = 0
        else: y = 1
        if f[line][y] != 0: return f[line][y]
        for l in v[x]:
            if l != line:
                if e[l][0] == x: son = e[l][1]
                else: son = e[l][0]
                if a[son-1] < a[x-1] and calc(son,l) == -1:
                    f[line][y] = 1
                    return 1
        f[line][y] = -1
        return -1

n = int(input())
v = [set([]) for i in range(0,n+1)]
e = [0 for i in range(0,n)]
a = readln()
for i in range(1,n):
    s = readln()
    v[s[0]].add(i)
    v[s[1]].add(i)
    e[i] = s

f = [[0,0] for i in range(0,n)]
ans = []
for i in range(1,n+1):
    if calc(i,0) == 1:
        ans.append(i)
ans = list(map(str,ans))
print(' '.join(ans))