Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <stdlib.h>
#include <functional>
#include <string>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <nmmintrin.h>
#include <chrono>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}

class Graph {
public:
	int vn;
	int sumcost = 0;
	vector<vector<pii>> g;

	Graph(int n) {
		vn = n;
		g.resize(n);
	}
	virtual void con(int a, int b, int w) = 0;
	int getWeight(int f, int t) {
		auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
		if (itr != g[f].end())
			return itr->second;
		return INT_MIN;
	}
	int Costsum() {
		return sumcost;
	}
	void scan(int edcount, bool oindexed, bool w) {
		REP(i, edcount) {
			int a, b, c = 1;
			scanf("%d %d", &a, &b);
			if (w)scanf("%d", &c);
			con(a - oindexed, b - oindexed, c);
		}
	}
};
class BiDGraph : public Graph {//無向
public:
	BiDGraph(int n) : Graph(n) {}

	void con(int a, int b, int w = 1) {
		g[a].push_back({ b,w });
		g[b].push_back({ a, w });
		sumcost++;
	}
};

signed main() {
	int n;
	cin >> n;
	BiDGraph g(n);
	REP(i, n-1) {
		int a, b;
		scanf("%d %d", &a, &b);
		g.con(a - 1, b - 1);
	}

	vector<int> depth(n),par(n);
	function<void(int, int)> dfs = [&](int c, int p) {
		par[c] = p;
		depth[c] = p == -1 ? 0 : depth[p] + 1;
		for (auto itr : g.g[c])
			if (itr.first != p)
				dfs(itr.first, c);
	};
	dfs(0, -1);

	vector<pii> num(n, {INT_MAX, INT_MAX});
	vector<int> ev(n, -1);

	int m;
	cin >> m;
	vector<int> in(n), ot(n);
	priority_queue<pii>> pq;
	REP(i, m) {
		int a, b;
		scanf("%d %d", &a, &b);
		--a;
		num[a] = { b,b };
		pq.push(MP(depth[a], a));
		ev[a] = b % 2;
		in[a]++;
	}


	while (pq.size()) {
		auto cur = pq.top();
		pq.pop();
		ot[cur.second]++;

		if (cur.second == 0)
			continue;

		if (num[par[cur.second]] == MP(INT_MAX, INT_MAX)) {
			num[par[cur.second]] = MP(num[cur.second].first - 1, num[cur.second].second + 1);
		}
		else {
			int cl = num[cur.second].first - 1, cr = num[cur.second].second + 1, l = num[par[cur.second]].first, r = num[par[cur.second]].second;

			num[par[cur.second]] = { max(cl,l), min(cr,r) };
			if (num[par[cur.second]].first > num[par[cur.second]].second) {
				printf("No\n");
				return 0;
			}
		}
		if (ev[par[cur.second]] != -1) {
			if (ev[par[cur.second]] != (1 - ev[cur.second])) {
				printf("No\n");
				return 0;
			}
		}
		ev[par[cur.second]] = 1 - ev[cur.second];
		if (par[cur.second] != 0 && in[cur.second] == ot[cur.second]) {
			pq.push(MP(depth[par[cur.second]], par[cur.second]));
			in[par[cur.second]]++;
		}
	}

	printf("Yes\n");
	vector<int> out(n);
	function<void(int, int)> ans = [&](int c, int p) {
		
		if (p == -1) 
			out[c] = num[c].first;
		else 
			out[c] = num[c].first <= (out[p]-1) ? out[p]-1 : out[p]+1;
		

		for (auto itr : g.g[c]) {
			if(itr.first != p)
				ans(itr.first, c);
		}
	};
	ans(0, -1);
	for (auto itr : out)
		printf("%d\n", itr);

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:122:27: error: expected unqualified-id before '>' token
  122 |         priority_queue<pii>> pq;
      |                           ^~
a.cc:128:17: error: 'pq' was not declared in this scope
  128 |                 pq.push(MP(depth[a], a));
      |                 ^~
a.cc:134:16: error: 'pq' was not declared in this scope
  134 |         while (pq.size()) {
      |                ^~
a.cc:148:57: error: 'l' was not declared in this scope; did you mean 'cl'?
  148 |                         num[par[cur.second]] = { max(cl,l), min(cr,r) };
      |                                                         ^
      |                                                         cl
a.cc:148:65: error: 'cr' was not declared in this scope; did you mean 'cl'?
  148 |                         num[par[cur.second]] = { max(cl,l), min(cr,r) };
      |                                                                 ^~
      |                                                                 cl
a.cc:148:68: error: 'r' was not declared in this scope
  148 |                         num[par[cur.second]] = { max(cl,l), min(cr,r) };
      |                                                                    ^