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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};
use std::collections::{HashMap, HashSet};

fn main() {
    input!{
        n: usize,
        es: [(usize1, usize1); n - 1],
        k: usize,
        vp: [(usize1, i64); k]
    }
    let mut t = vec![vec![]; n];
    for &(a, b) in &es {
        t[a].push(b);
        t[b].push(a);
    }
    let mut sets = vec![None; n];
    for &(v, p) in &vp {
        let mut s = HashSet::new();
        s.insert(p);
        sets[v] = Some(s);
    }
    dfs(0, 0, &t, &mut sets);
    if sets[0].clone().unwrap().is_empty() {
        println!("No");
        return;
    }
    println!("Yes");
    let mut ans = vec![0; n];
    ans[0] = *sets[0].clone().unwrap().iter().nth(0).unwrap();
    dfs2(0, 0, &t, &sets, &mut ans);
    for i in 0..n {
        println!("{}", ans[i]);
    }
}

fn dfs(r: usize, p: usize, t: &Vec<Vec<usize>>, sets: &mut Vec<Option<HashSet<i64>>>) {
    let mut ss = vec![];
    if let Some(s) = sets[r].clone() {
        ss.push(s);
    }
    for &c in &t[r] {
        if c == p {
            continue;
        }
        dfs(c, r, t, sets);
        if let Some(s) = sets[c].clone() {
            ss.push(s.iter().map(|x| x + 1).chain(s.iter().map(|x| x - 1)).collect());
        }
    }
    if ss.len() == 1 {
        sets[r] = Some(ss[0].clone());
    } else if ss.len() > 1 {
        ss.sort_by_key(|x| x.len());
        let mut sr: HashSet<i64> = ss[0].intersection(&ss[1]).cloned().collect();
        for i in 2..ss.len() {
            sr = sr.intersection(&ss[i]).cloned().collect();
        }
        sets[r] = Some(sr);
    }
}

fn dfs2(r: usize, p: usize, t: &Vec<Vec<usize>>, sets: &Vec<Option<HashSet<i64>>>, v: &mut Vec<i64>) {
    for &c in &t[r] {
        if c == p {
            continue;
        }
        if let Some(s) = sets[c].clone() {
            if s.contains(&(v[r] + 1)) {
                v[c] = v[r] + 1;
            } else {
                v[c] = v[r] - 1;
            }
        } else {
            v[c] = v[r] + 1;
        }
        dfs2(c, r, t, sets, v);
    }
}