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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector <ll> vi;
typedef pair<ll,ll> pd;

typedef vector <ll> vpi;
typedef pair<pi,ll> pii;
#define f first
#define s second
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0)
#define maxn 100001
#define INF INT_MAX/2
#define int ll

int N,K;
vi adj[maxn];
pi range[maxn];
bool vis[maxn],vis1[maxn];
int ans[maxn];
int par[maxn];
pi intersect(pi i, pi j){
	return pi(max(i.f,j.f),min(i.s,j.s));
}
bool np = 0;



void dfs(int x,int p,int val){
	
	ans[x] = val;
	
	aFOR(i,adj[x]){
		if (i != p){
			if (range[i].f <= val - 1 && val - 1 <= range[i].s) dfs(i,x,val-1);
			else if (range[i].f <= val + 1 && val + 1 <= range[i].s) dfs(i,x,val+1);
			else np = 1;
		}
	}
}

int32_t main(){
	fast;

	cin>>N;
	
	FOR(i,0,N-2){
		int a,b;
		cin>>a>>b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	cin>>K;
	
	FOR(i,1,N) range[i] = pi(-INF,INF);
	
	queue <pii> q;
	int x;
	FOR(i,0,K-1){
		int a,b;
		cin>>a>>b;
		q.push(pii(pi(b,b),a));
		x = a;
		
		range[a] = pi(b,b);
	}
	
	queue <int> q1;
	q1.push(x);
	par[x] = abs(range[x].f) % 2;
	
	while (!q1.empty()){
		int cur = q1.front(); q1.pop();
		
		if (vis1[cur]) continue;
		vis1[cur] = 1;
		aFOR(i,adj[cur]){

			par[i] = (par[cur] + 1) % 2;
			if (range[i].f == range[i].s && par[i] != abs(range[i].f) % 2) np = 1;
			q1.push(i);
		}
	}
	
	
	while (!q.empty()){
		pii cur = q.front(); q.pop();
		
		if (range[cur.s] == intersect(range[cur.s],cur.f)) continue;
		
		range[cur.s] = intersect(range[cur.s],cur.f);
		if (range[cur.s].f > range[cur.s].s) np = 1;
		//if (vis[cur.s]) continue;
		//vis[cur.s] = 1;
		
		aFOR(i,adj[cur.s]){
			q.push(pii(pi(cur.f.f-1,cur.f.s+1),i));
		}
	}
	
	if (np == 1){
		cout<<"NO";
		return 0;
	}
	
	dfs(x,-1,range[x].f);
	cout<<"YES\n";
	FOR(i,1,N) cout<<ans[i]<<'\n';
	
		
	
}