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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	List<Integer>[] g;

	boolean[] fixed;
	int[] val;
	boolean[] anything;
	
	int[] low, high;
	
	int[] outp;

	void solve() throws IOException {
		int n = nextInt();
		g = new List[n];
		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<>();
		}
		
		for (int i = 0; i < n - 1; i++) {
			int v = nextInt() - 1;
			int u = nextInt() - 1;
			g[v].add(u);
			g[u].add(v);
		}

		int k = nextInt();
		fixed = new boolean[n];
		val = new int[n];
		
		int root = -1;

		for (int i = 0; i < k; i++) {
			int v = nextInt() - 1;
			fixed[v] = true;
			val[v] = nextInt();
			root = v;
		}
		
//		System.err.println(root);
		
		low = new int[n];
		high = new int[n];
		anything = new boolean[n];
		
		boolean hasAns = dfs1(root, -1);
		out.println(hasAns ? "Yes" : "No");
		
		if (hasAns) {
			outp = new int[n];
			outp[root] = val[root];
			dfs2(root, -1, val[root]);
			for (int x : outp) {
				out.println(x);
			}
		}
	}
	
	void dfs2(int v, int p, int pVal) {
		if (p != -1) {
			if (anything[v]) {
				outp[v] = pVal - 1;
			} else if (low[v] <= pVal - 1 && pVal - 1 <= high[v]) {
				outp[v] = pVal - 1;
			} else if (low[v] <= pVal + 1 && pVal + 1 <= high[v]) {
				outp[v] = pVal + 1;
			} else {
				throw new AssertionError();
			}
		}
		
		for (int u : g[v]) {
			if (u == p) {
				continue;
			}
			dfs2(u, v, outp[v]);
		}
	}
	
	boolean dfs1(int v, int p) {
		if (fixed[v]) {
			low[v] = high[v] = val[v];
		} else {
			anything[v] = true;
		}
		
		for (int u : g[v]) {
			if (u == p) {
				continue;
			}
			
			boolean isOk = dfs1(u, v);
			if (!isOk) {
				return false;
			}
			
			if (anything[u]) {
				continue;
			}
			
			if (anything[v]) {
				anything[v] = false;
				low[v] = low[u] - 1;
				high[v] = high[u] + 1;
				continue;
			}
			
			if (((low[u] ^ low[v]) & 1) == 0) {
				return false;
			}
			
			low[v] = Math.max(low[v], low[u] - 1);
			high[v] = Math.min(high[v], high[u] + 1);
		}
		
		return low[v] <= high[v];
	}
	
	boolean isInside(int val, int low, int high) {
		if (((low ^ high) & 1) != 0) {
			throw new AssertionError();
		}
		return low <= val && val <= high && ((val ^ low) & 1) == 0;
	}

	Main() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		Thread t = new Thread(null, new Runnable() {

			@Override
			public void run() {
				try {
					solve();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}, "lul", 1 << 26);
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}