Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define inf 1e9
#define eps 1e-10
#define mod 9901
#define mkp make_pair
using namespace std;
int i,j,k,m,s,t,n,ans1,ans2,n1,nn,m1,mx;
int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int kabs(int a)
{
   if (a<0) return -a;
   return a; 
}
void add(int &x,int y)
{
   x+=y;
   if (x>=mod) x-=mod;
}
void dec(int &x,int y)
{
	x-=y;
	if (x<0) x+=mod;
}
int bo,first[101000],next[201000],zhi[201000],a[101000],p[101000],l1[101000],r1[101000];
void insert(int u,int v)
{
	next[++m1]=first[u];
	first[u]=m1;
	zhi[m1]=v;
}
void dfs(int x,int fa)
{
	if (!bo) return;
	int k,v,bo2=0;
	for (k=first[x];k;k=next[k])
	{
		v=zhi[k];
		if (v==fa) continue;
		dfs(v,x);
		p[x]|=p[v];
	}
	if (!bo) return;
	if (!p[x]) return;
	if (a[x]!=-1)
		bo2=1,l1[x]=r1[x]=a[x];
	for (k=first[x];k;k=next[k])
	{
		v=zhi[k];
		if (v==fa) continue;
		if (!p[v]) continue;
		if (!bo2)
		{
			bo2=1;
			l1[x]=l1[v]-1;
			r1[x]=r1[v]+1;
		} else
		{
			if (l1[x]%2!=(l1[v]-1)%2) {bo=0;return;}
			l1[x]=max(l1[x],l1[v]-1);
			r1[x]=min(r1[x],r1[v]+1);
			if (l1[x]>r1[x]) {bo=0;return;}
		}
	}
}
void solve(int x,int fa)
{
	int k,v;
	if (x==1) a[x]=l1[x]; else
	if (!p[x]||(a[fa]+1>=l1[x]&&a[fa]+1<=r1[x])) a[x]=a[fa]+1; else a[x]=a[fa]-1;
	for (k=first[x];k;k=next[k])
	{
		v=zhi[k];
		if (v==fa) continue;
		solve(v,x);
	}
}
int main()
{
	scanf("%d",&n);
	for (i=1;i<n;i++)
		scanf("%d%d",&s,&t),insert(s,t),insert(t,s);
	scanf("%d",&m);
	memset(a,-1,sizeof(a));
	for (i=1;i<=m;i++)
		scanf("%d%d",&s,&t),a[s]=t,p[s]=1;
	bo=1;
	dfs(1,0);
	if (!bo)
	{
		puts("NO");
		return 0;
	}
	solve(1,0);
	puts("YES");
	for (i=1;i<=n;i++)
		printf("%d\n",a[i]);
}

Compiler output:
a.cc: In function 'void insert(int, int)':
a.cc:43:9: error: reference to 'next' is ambiguous
   43 |         next[++m1]=first[u];
      |         ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:40:22: note:                 'int next [201000]'
   40 | int bo,first[101000],next[201000],zhi[201000],a[101000],p[101000],l1[101000],r1[101000];
      |                      ^~~~
a.cc: In function 'void dfs(int, int)':
a.cc:51:29: error: reference to 'next' is ambiguous
   51 |         for (k=first[x];k;k=next[k])
      |                             ^~~~
/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:40:22: note:                 'int next [201000]'
   40 | int bo,first[101000],next[201000],zhi[201000],a[101000],p[101000],l1[101000],r1[101000];
      |                      ^~~~
a.cc:62:29: error: reference to 'next' is ambiguous
   62 |         for (k=first[x];k;k=next[k])
      |                             ^~~~
/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:40:22: note:                 'int next [201000]'
   40 | int bo,first[101000],next[201000],zhi[201000],a[101000],p[101000],l1[101000],r1[101000];
      |                      ^~~~
a.cc: In function 'void solve(int, int)':
a.cc:86:29: error: reference to 'next' is ambiguous
   86 |         for (k=first[x];k;k=next[k])
      |                             ^~~~
/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:40:22: note:                 'int next [201000]'
   40 | int bo,first[101000],next[201000],zhi[201000],a[101000],p[101000],l1[101000],r1[101000];
      |                      ^~~~