Language: C++

#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>

using namespace std;

#define Set(a, b) memset(a, b, sizeof a)

const int N = 2e5 + 10;
const int Inf = INT_MAX / 2;

int n, m, a[N];
int tms, en[N], next[N], to[N];

void Link(int u, int v) {
	to[++tms] = v;
	next[tms] = en[u];
	en[u] = tms;
}

int fa[N], b[N], c[N];

void Dfs(int u) {
	if (~a[u]) 
		c[u] = (b[u] - a[u]) / 2;
	else
		c[u] = Inf;
	for (int p = en[u]; p; p = next[p]) {
		int v = to[p];
		if (!fa[v]) {
			fa[v] = u;
			b[v] = b[u] + 1;
			Dfs(v);
		}
	}
	if (~fa[u])
		c[fa[u]] = min(c[fa[u]], c[u]);
}

void Dfs(int u, int lst) {
	if ((~a[u] && a[u] != b[u] - lst * 2) || (a[u] == -1 && lst < c[u])) 
		++lst;
	b[u] -= lst * 2;
	for (int p = en[u]; p; p = next[p]) {
		int v = to[p];
		if (fa[v] == u) 
			Dfs(v, lst);
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		int u, v;
		scanf("%d %d", &u, &v);
		Link(u, v), Link(v, u);
	}
	scanf("%d", &m);
	int rt;
	Set(a, 255);
	for (int i = 1; i <= m; ++i) {
		int u, v;
		scanf("%d %d", &u, &v);
		a[u] = v;
		rt = u;
	}
	fa[rt] = -1;
	b[rt] = a[rt];
	Dfs(rt);
	Dfs(rt, 0);
	bool flag = 1;
	for (int i = 1; i <= n; ++i)
		if (~a[i] && a[i] != b[i]) {
			flag = 0;
			break;
		}
	if (!flag) 
		puts("No");
	else {
		puts("Yes");
		for (int i = 1; i <= n; ++i)
			printf("%d\n", b[i]);
	}

	return 0;
}


Compiler output:
a.cc: In function 'void Link(int, int)':
a.cc:19:9: error: reference to 'next' is ambiguous
   19 |         next[tms] = en[u];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:15:17: note:                 'int next [200010]'
   15 | int tms, en[N], next[N], to[N];
      |                 ^~~~
a.cc: In function 'void Dfs(int)':
a.cc:30:36: error: reference to 'next' is ambiguous
   30 |         for (int p = en[u]; p; p = next[p]) {
      |                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:15:17: note:                 'int next [200010]'
   15 | int tms, en[N], next[N], to[N];
      |                 ^~~~
a.cc: In function 'void Dfs(int, int)':
a.cc:46:36: error: reference to 'next' is ambiguous
   46 |         for (int p = en[u]; p; p = next[p]) {
      |                                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:15:17: note:                 'int next [200010]'
   15 | int tms, en[N], next[N], to[N];
      |                 ^~~~