Problem:
Score : 800 points

Problem Statement

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 1

5
1 2
3 1
4 3
3 5
2
2 6
5 7

Sample Output 1

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

Sample Input 2

5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7

Sample Output 2

No

Sample Input 3

4
1 2
2 3
3 4
1
1 0

Sample Output 3

Yes
0
-1
-2
-3

The integers written by Aoki may be negative or exceed 10^6.

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define F first
#define S second
#define pb emplace_back
#define mt make_tuple
#define gcd __gcd
#define PI 3.141592653589
// Input
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define llin(a) cin >> a
#define inl(a) scanf("%lld",&a)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)in(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
// Output
#define pr(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d\n",a,b)
#define pr3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out(a) printf("%d\n",a)
#define outl(a) printf("%lld\n",a)
#define llpr(a) cout << a << " "
#define llout(a) cout << a << "\n"
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
// Iterator
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define all(vec) vec.begin(),vec.end()
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
// Debug
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define what(x) cerr << #x << " : " << x << "\n"
#define ck printf("continue\n")
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cerr << *it << " = " << a << "\n";
	err(++it, args...);
}
// Data Type
#define ll long long int
#define ii pair<int,int>
#define pli pair<ll,int>
#define triple tuple<int,int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
#define M 1000000007
#define N 100005  //check the limits, man
const ll INF = 1e12;
vvi ad(N);
int low[N],high[N];
bool odd[N],vis[N];
int lev[N];
bool possible=true;
int root;
void level(int s)
{
    int i,a,x=ad[s].size();
    vis[s]=1;
    rep(i,x)
    {
        a=ad[s][i];
        if(!vis[a])
            lev[a]=lev[s]+1,level(a);
    }
    return ;
}
void dfs(int s,int p)
{
    int i,a,x=ad[s].size(),f=1;
    while(f)
    {
        f=0;
        rep(i,x)
        {
            a=ad[s][i];
            if(a!=p)
            {
                low[a]=max(low[a],low[s]-1);
                high[a]=min(high[a],high[s]+1);
                dfs(a,s);
                if(low[s]<low[a]-1 || high[s]>high[a]+1)
                {
                    f=1;
                    low[s]=max(low[s],low[a]-1);
                    high[s]=min(high[s],high[a]+1);
                }
            }
        }
    }
}
void value(int s,int p)
{
    int i,a,x=ad[s].size();
    if(low[s]<high[s])
    {
        a=(lev[s]&1)-odd[root];
        if(a<0)
            a+=2;
        if((low[s]&1)!=a)
            low[s]++;
        high[s]=low[s];
    }
    rep(i,x)
    {
        a=ad[s][i];
        if(a!=p)
        {
            low[a]=max(low[a],low[s]-1);
            high[a]=min(high[a],high[s]+1);
            value(a,s);
        }
    }
    return ;
}
int main()
{
    int n,i,a,b,k;
    in(n);
    rep(i,n-1)
    {
        in2(a,b);
        ad[a].pb(b);
        ad[b].pb(a);
    }
    fill(low,low+N,-M);
    fill(high,high+N,M);
    in(k);
    while(k--)
    {
        in2(a,b);
        low[a]=max(low[a],b);
        high[a]=min(high[a],b);
        odd[a]=(b&1);
    }
    root=a;
    level(root);
    dfs(root,-1);
    lp(i,1,n+1)
    {
        if(low[i]>high[i])
            possible=false;
        else if(high[i]-low[i]==0)
        {
            a=(lev[i]&1)-odd[root];
            if(a<0)
                a+=2;
            if((low[i]&1)!=a)
                possible=false;
        }
    }
    if(possible)
    {
        printf("Yes\n");
        lp(i,1,n+1)
            //pr2(low[i],high[i]);
        value(root,-1);
        lp(i,1,n+1)
            out(low[i]);
    }
    else
        printf("No\n");
}