We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
 - Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.

-----Constraints-----
 - 1 ≦ N ≦ 10^5
 - 1 ≦ K ≦ N
 - 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
 - 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
 - 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
 - The given graph is a tree.
 - All v_j are distinct.

-----Input-----
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K

-----Output-----
If it is possible to write integers into all empty vertices so that the condition is satisfied, print Yes. Otherwise, print No.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.

-----Sample Input-----
5
1 2
3 1
4 3
3 5
2
2 6
5 7

-----Sample Output-----
Yes
5
6
6
5
7

The figure below shows the tree when Takahashi fell asleep. For each vertex, the integer written beside it represents the index of the vertex, and the integer written into the vertex is the integer written by Takahashi.
Aoki can, for example, satisfy the condition by writing integers into the remaining vertices as follows:
This corresponds to Sample Output 1. Note that other outputs that satisfy the condition will also be accepted, such as:
Yes
7
6
8
7
7


Difficulty: competition

Starter code:

Solutions:
import sys
input = lambda: sys.stdin.readline().rstrip()

from collections import deque
N = int(input())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

Y = [(-10**9, 10**9) for _ in range(N)]
K = int(input())
for _ in range(K):
    v, p = map(int, input().split())
    Y[v-1] = (p, p)

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)


def calc():
    for i in R[::-1]:
        e, o = 0, 0
        l, r = Y[i]
        if r != 10 ** 9:
            if l % 2:
                o = 1
            else:
                e = 1
        for j in X[i]:
            a, b = Y[j]
            if b == 10**9: continue
            if a % 2:
                e = 1
            else:
                o = 1
            l = max(l, a - 1)
            r = min(r, b + 1)
        if (e and o) or (l > r):
            print("No")
            return 0
        elif e or o:
            Y[i] = (l, r)
    
    for i in R[1:]:
        if Y[P[i]][0] - 1 >= Y[i][0]:
            Y[i] = (Y[P[i]][0] - 1, 0)
        else:
            Y[i] = (Y[P[i]][0] + 1, 0)
    
    print("Yes")
    for i in range(N):
        print(Y[i][0])

calc()

class Tree():
    def __init__(self, n, edge, indexed=1):
        self.n = n
        self.tree = [[] for _ in range(n)]
        for e in edge:
            self.tree[e[0] - indexed].append(e[1] - indexed)
            self.tree[e[1] - indexed].append(e[0] - indexed)

    def setroot(self, root):
        self.root = root
        self.parent = [None for _ in range(self.n)]
        self.parent[root] = -1
        self.depth = [None for _ in range(self.n)]
        self.depth[root] = 0
        self.order = []
        self.order.append(root)
        stack = [root]
        while stack:
            node = stack.pop()
            for adj in self.tree[node]:
                if self.parent[adj] is None:
                    self.parent[adj] = node
                    self.depth[adj] = self.depth[node] + 1
                    self.order.append(adj)
                    stack.append(adj)

INF = 10**18

import sys
input = sys.stdin.readline

N = int(input())
E = [tuple(map(int, input().split())) for _ in range(N - 1)]

num = [None for _ in range(N)]

K = int(input())

for _ in range(K):
    v, p = map(int, input().split())
    num[v - 1] = p

tree = Tree(N, E)
tree.setroot(v - 1)

even = []
odd = []

for i in range(N):
    if num[i] is not None:
        if tree.depth[i] % 2 == 0:
            even.append(num[i] % 2)
        else:
            odd.append(num[i] % 2)

if not ((all(even) or not any(even)) and (all(odd) or not any(odd))):
    print('No')

else:
    I = [[-INF, INF] for _ in range(N)]
    for i in range(N):
        if num[i] is not None:
            I[i] = [num[i], num[i]]
    for node in tree.order[::-1]:
        lt, rt = I[node]
        for adj in tree.tree[node]:
            lt = max(lt, I[adj][0] - 1)
            rt = min(rt, I[adj][1] + 1)
        if lt > rt:
            print('No')
            break
        I[node] = [lt, rt]

    else:
        stack = [v - 1]
        visited = [0 for _ in range(N)]
        visited[v - 1] = 1
        while stack:
            node = stack.pop()
            for adj in tree.tree[node]:
                if visited[adj]:
                    continue
                visited[adj] = 1
                if I[adj][0] <= num[node] - 1 <= I[adj][1]:
                    num[adj] = num[node] - 1
                elif I[adj][0] <= num[node] + 1 <= I[adj][1]:
                    num[adj] = num[node] + 1
                else:
                    print('No')
                    break
                stack.append(adj)
            else:
                continue
            break
        else:
            print('Yes')
            print('\n'.join(map(str, num)))

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N = int(input())
G = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    G[A].append(B)
    G[B].append(A)
K = int(input())
num = [-1] * (N+1)
for _ in range(K):
    V, P = map(int, input().split())
    num[V] = P

# check parity
stack = [1]
depth = [-1] * (N+1)
depth[1] = 0
while stack:
    v = stack.pop()
    for c in G[v]:
        if depth[c] == -1:
            depth[c] = depth[v] + 1
            stack.append(c)
parity = [set(), set()]
for i in range(1, N+1):
    if num[i] != -1:
        parity[depth[i] % 2].add(num[i] % 2)
if len(parity[0]) == 2 or len(parity[1]) == 2:
    print('No')
    return

INF = 10**9
lb = [-INF] * (N+1)
ub = [INF] * (N+1)

def dfs1(v, p):
    for c in G[v]:
        if c == p:
            continue
        dfs1(c, v)
        lb[v] = max(lb[v], lb[c] - 1)
        ub[v] = min(ub[v], ub[c] + 1)
    if num[v] != -1:
        lb[v] = ub[v] = num[v]

dfs1(1, 0)

def dfs2(v, p):
    for c in G[v]:
        if c == p:
            continue
    for c in G[v]:
        if c == p:
            continue
        lb[c] = max(lb[c], lb[v] - 1)
        ub[c] = min(ub[c], ub[v] + 1)
        dfs2(c, v)

dfs2(1, 0)

for i in range(1, N+1):
    if lb[i] > ub[i]:
        print('No')
        return

print('Yes')

def dfs3(v, p):
    for c in G[v]:
        if c == p:
            continue
        if lb[c] <= num[v] - 1:
            num[c] = num[v] - 1
        else:
            num[c] = num[v] + 1
        dfs3(c, v)

num[1] = lb[1]
dfs3(1, 0)

print(*num[1:], sep='\n')

def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n=N()
edge=[[] for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)
INF=float("inf")
ma=[INF]*(n+1)
mi=[-INF]*(n+1)
k=N()
vp=LL(k)
for v,p in vp:
    ma[v]=p
    mi[v]=p

import sys
sys.setrecursionlimit(10**6)
def dfs(v,d,hi,lo):
    dep[v]=d
    ma[v]=min(ma[v],hi)
    mi[v]=max(mi[v],lo)
    hi=ma[v]
    lo=mi[v]
    for i in edge[v]:
        if dep[i]==-1:
            hi,lo=dfs(i,d+1,hi+1,lo-1)
            ma[v]=min(ma[v],hi)
            mi[v]=max(mi[v],lo)
            hi=ma[v]
            lo=mi[v]
    return hi+1,lo-1
for i in range(2):
    dep=[-1 for i in range(n+1)]
    dfs(v,0,p,p)
for i,j in zip(mi,ma):
    if i>j or (i-j)%2==1:
        print("No")
        break
else:
    print("Yes")
    for i in mi[1:]:
        print(i)

import heapq
import sys
input = sys.stdin.readline
INF = 10 ** 18


n = int(input())
edges = [list(map(int, input().split())) for i in range(n - 1)]
k = int(input())
info = [list(map(int, input().split())) for i in range(k)]

tree = [[] for i in range(n)]
for a, b in edges:
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

ans = [INF] * n
q = []
for v, val in info:
    v -= 1
    ans[v] = val
    heapq.heappush(q, (-val, v))
    for nxt_v in tree[v]:
        if ans[nxt_v] != INF and abs(ans[v] - ans[nxt_v]) != 1:
            print("No")
            return

while q:
    val, v = heapq.heappop(q)
    val = -val
    for nxt_v in tree[v]:
        if ans[nxt_v] == INF:
            ans[nxt_v] = val - 1
            heapq.heappush(q, (-(val - 1), nxt_v))
        else:
            if abs(ans[v] - ans[nxt_v]) != 1:
                print("No")
                return

print("Yes")
for val in ans:
    print(val)

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int,readline().split())

g = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,readline().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
    

order = []
parent = [-1]*n
st = [0]
while st:
    v = st.pop()
    order.append(v)
    for c in g[v]:
        if parent[v] != c:
            parent[c] = v
            st.append(c)

INF = 1<<30
high = [INF]*n
low = [-INF]*n
val = [INF]*n

k, = map(int,readline().split())
for _ in range(k):
    v,p = map(int,readline().split())
    high[v-1] = low[v-1] = val[v-1] = p

for v in order[:0:-1]:
    p = parent[v]
    #elif val[v]== INF: # vに好きな数書ける
    h,l = high[v]+1,low[v]-1
    
    if h >= INF:
        pass
    elif high[p]>=INF:
        high[p] = h
        low[p] = l
    elif (high[p]-h)&1==0: #偶奇一致
        if h < high[p]: high[p] = h
        if l > low[p]: low[p] = l
        if high[p] < low[p]:
            print("No")
            break
    else: #偶奇違ってダメ
        print("No")
        break
    #print(v,val,high,low)
    
else: #OK
    print("Yes")
    for v in order:
        if val[v] == INF:
            val[v] = low[v]
        for c in g[v]:
            high[c] = min(high[c],val[v]+1)
            low[c] = max(low[c],val[v]-1)
    
    
    print(*val,sep="\n")








import sys
readline = sys.stdin.readline   

def getpar(Edge, p):
    N = len(Edge)
    par = [0]*N
    par[0] = -1
    par[p]  -1
    stack = [p]
    visited = set([p])
    while stack:
        vn = stack.pop()
        for vf in Edge[vn]:
            if vf in visited:
                continue
            visited.add(vf)
            par[vf] = vn
            stack.append(vf)
    return par

def topological_sort_tree(E, r):
    Q = [r]
    L = []
    visited = set([r])
    while Q:
        vn = Q.pop()
        L.append(vn)
        for vf in E[vn]:
            if vf not in visited:
                visited.add(vf)
                Q.append(vf)
    return L

def getcld(p):
    res = [[] for _ in range(len(p))]
    for i, v in enumerate(p[1:], 1):
        res[v].append(i)
    return res


def check(L, Par, wri, even):
    for i in range(len(Par)):
        if wri[i] and (wri[i] % 2 == even[i]):
            return False
    
    inf = 10**9+7
    dpu = [w if w is not None else inf for w in wri]
    dpd = [w if w is not None else -inf for w in wri]
    for l in L[::-1][:-1]:
        if dpu[l] == inf:
            continue
        if dpd[l] > dpu[l]:
            return False
        p = Par[l]
        dpu[p] = min(dpu[p], dpu[l] + 1)
        dpd[p] = max(dpd[p], dpd[l] - 1)
    if dpd[root] > dpu[root]:
        return False
    ans = [None]*N
    ans[root] = wri[root]
    for l in L[1:]:
        p = Par[l]
        if ans[p] - 1 >= dpd[l]:
            ans[l] = ans[p] - 1
        else:
            ans[l] = ans[p] + 1
    return ans
N = int(readline())
Edge = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, readline().split())
    a -= 1
    b -= 1
    Edge[a].append(b)
    Edge[b].append(a)
K = int(readline())

VP = [tuple(map(lambda x: int(x), readline().split())) for _ in range(K)]
wri = [None]*N
for v, p in VP:
    wri[v-1] = p

root = VP[0][0] - 1
Par = getpar(Edge, root)
L = topological_sort_tree(Edge, root)

even = [True]*N
even[root] = (wri[root] %2 == 0)
for l in L[1:]:
    p = Par[l]
    even[l] = not even[p]
ans = check(L, Par, wri, even)
if ans == False:
    print('No')
else:
    print('Yes')
    print('\n'.join(map(str, ans)))

import sys
sys.setrecursionlimit(10**7)
def main0(n,ab,k,vp):
  ki=[[] for _ in range(n)]
  for a,b in ab:
    a,b=a-1,b-1
    ki[a].append(b)
    ki[b].append(a)
  lrf=[[-1,-1,-1,] for _ in range(n)]
  for v,p in vp:
    v-=1
    lrf[v]=[p,p,p%2]
  flg=[True]
  def func(p,v):
    if lrf[v][2]!=-1:
      l,r,f=lrf[v]
      # 親頂点との整合性
      if p>=0:
        pl,pr,pf=lrf[p]
        if pl-1<=l<=pr+1 and pf^f==1:
          # ok
          pass
        else:
          flg[0]=False
    else:
      # 親頂点から条件を計算
      pl,pr,pf=lrf[p]
      l,r,f=pl-1,pr+1,pf^1
      lrf[v]=[l,r,f]
    l,r,f=lrf[v]
    for nv in ki[v]:
      if nv==p:continue
      # 子頂点たちの条件から、自分の条件を更新
      nl,nr,nf=func(v,nv)
      if f^nf==0:flg[0]=False
      l=max(l,nl-1)
      r=min(r,nr+1)
      if l>r:flg[0]=False
    lrf[v]=[l,r,f]
    # 最終的な自分の条件を返す
    return (l,r,f)
  func(-1,vp[0][0]-1)
  if not flg[0]:return []
  ret=[0]*n
  def func0(p,v):
    l,r,f=lrf[v]
    np=ret[p]
    if l<=np-1<=r and (np-1)%2==f:
      ret[v]=np-1
    elif l<=np+1<=r and (np+1)%2==f:
      ret[v]=np+1
    else:
      flg[0]=False
    for nv in ki[v]:
      if nv!=p:
        func0(v,nv)
  ret[vp[0][0]-1]=vp[0][1]
  for nv in ki[vp[0][0]-1]:
    func0(vp[0][0]-1,nv)
  if flg[0]:return ret
  else:return []


def __starting_point():
  n=int(input())
  ab=[list(map(int,input().split())) for _ in range(n-1)]
  k=int(input())
  vp=[list(map(int,input().split())) for _ in range(k)]
  ret0=main0(n,ab,k,vp)
  if len(ret0)==0:print('No')
  else:
    print('Yes')
    for x in ret0:print(x)

__starting_point()

class Graph:
    def __init__(self, n_vertices, edges, directed=True):
        self.n_vertices = n_vertices
        self.directed = directed
        self.edges = edges

    @property
    def adj(self):
        try:
            return self._adj
        except AttributeError:
            adj = [[] for _ in range(self.n_vertices)]
            if self.directed:
                for u,v in self.edges:
                    adj[u].append(v)
            else:
                for u,v in self.edges:
                    adj[u].append(v)
                    adj[v].append(u)
            self._adj = adj
            return adj


def solve(tree, vp):

    adj = tree.adj
    n = tree.n_vertices
    q = [v-1 for v,p in vp]
    ranges = [None]*n
    for v,p in vp:
        ranges[v-1] = (p,p)
    
    while q:
        nq = []
        for v in q:
            a,b = ranges[v]
            na,nb = a-1,b+1
            for u in adj[v]:
                if ranges[u] is None:
                    ranges[u] = na,nb
                    nq.append(u)
                else:
                    c,d = ranges[u]
                    if (c+na)%2 == 1:
                        return None
                    x,y = max(na,c),min(nb,d)
                    if x > y:
                        return None
                    ranges[u] = (x,y)
                    if (x,y) != (c,d):
                        nq.append(u)
        q = nq
    return [a for a,b in ranges]


def __starting_point():
    n = int(input())
    edges = [tuple(map(lambda x:int(x)-1,input().split())) for _ in range(n-1)]
    k = int(input())
    vp = [tuple(map(int,input().split())) for _ in range(k)]

    tree = Graph(n, edges, False)

    res = solve(tree, vp)
    if res is None:
        print('No')
    else:
        print('Yes')
        print(*res,sep='\n')
__starting_point()

import sys
sys.setrecursionlimit(500000)

N = int(input())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    E[a].append(b)
    E[b].append(a)
K = int(input())
L = [-float("inf")] * (N+1)
R = [float("inf")] * (N+1)
for _ in range(K):
    v, p = list(map(int, input().split()))
    L[v] = p
    R[v] = p

tour = []
def dfs(v, p):
    tour.append(v)
    for u in E[v]:
        if u!=p:
            dfs(u, v)
            tour.append(v)
dfs(v, 0)

l, r = L[v], R[v]
odd = p % 2
for v in tour[1:]:
    l -= 1
    r += 1
    odd = 1 - odd
    l_, r_ = L[v], R[v]
    if r_ != float("inf") and r_%2 != odd:
        print("No")
        return
    l = max(l, l_)
    r = min(r, r_)
    L[v] = l
    R[v] = r
for v in tour[-2::-1]:
    l -= 1
    r += 1
    odd = 1 - odd
    l_, r_ = L[v], R[v]
    l = max(l, l_)
    r = min(r, r_)
    if l > r:
        print("No")
        return
    L[v] = l
    R[v] = r
Ans = [-1] * (N+1)
print("Yes")
print(("\n".join(map(str, L[1:]))))