Buggy python submission (verdict: Time Limit Exceeded):
import sys

input=sys.stdin.readline
sys.setrecursionlimit(1000000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)

K=int(input())
dic={}
for i in range(K):
    v,p=map(int,input().split())
    dic[v]=0
    dic[v]=p

memo={i:[] for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
def dfs(num):
    flag[num]=False
    if memo[num]:
        return memo[num]

    if num!=1 and len(edge[num])==1:
        if num in dic.keys():
            memo[num]=[dic[num],dic[num],dic[num]%2]
            return memo[num]
        else:
            memo[num]=[-float("inf"),float("inf"),2]
        return memo[num]

    ansl=-float("inf")
    ansr=float("inf")
    anseo=2
    if num in dic.keys():
        ansl=dic[num]
        ansr=dic[num]
        anseo=dic[num]%2
    for i in edge[num]:
        if flag[i]:
            l,r,eo=dfs(i)
            if eo!=2:
                eo=(eo+1)%2
            if anseo==2:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                anseo=eo
                if ansl>ansr:
                    print("No")
                    exit()
            else:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                if ansl>ansr or (eo!=2 and eo!=anseo):
                    print("No")
                    exit()

    memo[num]=[ansl,ansr,anseo]
    return memo[num]

l,r,eo=dfs(1)
print("Yes")
ans={i:-float("inf") for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
ans[1]=l
flag[1]=False
q=set([(1,ans[1])])
sub=set([])
while q:
    while q:
        node,num=q.pop()
        for i in edge[node]:
            if flag[i]:
                flag[i]=False
                l,r,eo=memo[i]
                if r>=num-1>=l:
                    ans[i]=num-1
                    sub.add((i,ans[i]))
                else:
                    ans[i]=num+1
                    sub.add((i,ans[i]))

        if not q:
            q=sub
            sub=set([])
            break

for i in range(1,N+1):
    print(ans[i])


Fixed submission:
import sys

input=sys.stdin.readline
sys.setrecursionlimit(1000000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)

K=int(input())
dic={}
for i in range(K):
    v,p=map(int,input().split())
    dic[v]=0
    dic[v]=p

memo={i:[] for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
def dfs(num):
    flag[num]=False
    if memo[num]:
        return memo[num]

    if num!=1 and len(edge[num])==1:
        if num in dic.keys():
            memo[num]=[dic[num],dic[num],dic[num]%2]
            return memo[num]
        else:
            memo[num]=[-float("inf"),float("inf"),2]
        return memo[num]

    ansl=-float("inf")
    ansr=float("inf")
    anseo=2
    if num in dic.keys():
        ansl=dic[num]
        ansr=dic[num]
        anseo=dic[num]%2
    for i in edge[num]:
        if flag[i]:
            l,r,eo=dfs(i)
            if eo!=2:
                eo=(eo+1)%2
            if anseo==2:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                anseo=eo
                if ansl>ansr:
                    print("No")
                    exit()
            else:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                if ansl>ansr or (eo!=2 and eo!=anseo):
                    print("No")
                    exit()

    memo[num]=[ansl,ansr,anseo]
    return memo[num]

l,r,eo=dfs(1)
print("Yes")
ans={i:-float("inf") for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
ans[1]=l
flag[1]=False
q=set([(1,ans[1])])
sub=set([])
while q:
    while q:
        node,num=q.pop()
        for i in edge[node]:
            if flag[i]:
                flag[i]=False
                l,r,eo=memo[i]
                if r>=num-1>=l:
                    ans[i]=num-1
                else:
                    ans[i]=num+1
                sub.add((i,ans[i]))

        if not q:
            q=sub
            sub=set([])
            break

for i in range(1,N+1):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input=sys.stdin.readline
sys.setrecursionlimit(1000000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)

K=int(input())
dic={}
for i in range(K):
    v,p=map(int,input().split())
    dic[v]=0
    dic[v]=p

parent={i:-1 for i in range(1,N+1)}
parent[1]=0
q=set([1])
sub=set([])
while q:
    while q:
        x=q.pop()
        for i in edge[x]:
            if parent[i]==-1:
                parent[i]=x
                sub.add(i)

        if not q:
            q=sub
            sub=set([])
            break


memo={i:[] for i in range(1,N+1)}
def dfs(num):
    if memo[num]:
        return memo[num]

    if edge[num]==[parent[num]]:
        if num in dic.keys():
            memo[num]=[dic[num],dic[num],dic[num]%2]
            return memo[num]
        else:
            memo[num]=[-float("inf"),float("inf"),2]
        return memo[num]

    ansl=-float("inf")
    ansr=float("inf")
    anseo=2
    if num in dic.keys():
        ansl=dic[num]
        ansr=dic[num]
        anseo=dic[num]%2
    for i in edge[num]:
        if i!=parent[num]:
            l,r,eo=dfs(i)
            if eo==1:
                eo=0
            elif eo==0:
                eo=1
            if (anseo!=2 and eo!=2 and anseo!=eo) or l-1>ansr or ansl>r+1:
                print("No")
                exit()
            else:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                if anseo==2:
                    anseo=eo

    memo[num]=[ansl,ansr,anseo]
    return memo[num]

l,r,eo=dfs(1)
print("Yes")
ans={i:-float("inf") for i in range(1,N+1)}
ans[1]=l
q=set([(1,ans[1])])
sub=set([])
while q:
    while q:
        node,num=q.pop()
        for i in edge[node]:
            if i!=parent[node]:
                l,r,eo=memo[i]
                if r>=num-1>=l:
                    ans[i]=num-1
                    sub.add((i,ans[i]))
                else:
                    ans[i]=num+1
                    sub.add((i,ans[i]))

        if not q:
            q=sub
            sub=set([])
            break

for i in range(1,N+1):
    print(ans[i])

Fixed submission:
import sys

input=sys.stdin.readline
sys.setrecursionlimit(1000000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)

K=int(input())
dic={}
for i in range(K):
    v,p=map(int,input().split())
    dic[v]=0
    dic[v]=p

memo={i:[] for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
def dfs(num):
    flag[num]=False
    if memo[num]:
        return memo[num]

    if num!=1 and len(edge[num])==1:
        if num in dic.keys():
            memo[num]=[dic[num],dic[num],dic[num]%2]
            return memo[num]
        else:
            memo[num]=[-float("inf"),float("inf"),2]
        return memo[num]

    ansl=-float("inf")
    ansr=float("inf")
    anseo=2
    if num in dic.keys():
        ansl=dic[num]
        ansr=dic[num]
        anseo=dic[num]%2
    for i in edge[num]:
        if flag[i]:
            l,r,eo=dfs(i)
            if eo!=2:
                eo=(eo+1)%2
            if anseo==2:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                anseo=eo
                if ansl>ansr:
                    print("No")
                    exit()
            else:
                ansr=min(ansr,r+1)
                ansl=max(ansl,l-1)
                if ansl>ansr or (eo!=2 and eo!=anseo):
                    print("No")
                    exit()

    memo[num]=[ansl,ansr,anseo]
    return memo[num]

l,r,eo=dfs(1)
print("Yes")
ans={i:-float("inf") for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
ans[1]=l
flag[1]=False
q=set([(1,ans[1])])
sub=set([])
while q:
    while q:
        node,num=q.pop()
        for i in edge[node]:
            if flag[i]:
                flag[i]=False
                l,r,eo=memo[i]
                if r>=num-1>=l:
                    ans[i]=num-1
                else:
                    ans[i]=num+1
                sub.add((i,ans[i]))

        if not q:
            q=sub
            sub=set([])
            break

for i in range(1,N+1):
    print(ans[i])