Language: C++

#include<cstdio>
#include<utility>
#include<cstring>
#include<vector>
#include<string>
using namespace std;
typedef pair < int , int > pii;
template < typename T > T MAX(T a,T b){
	return a<b?b:a;
}
template < typename T > T MIN(T a,T b){
	return a<b?a:b;
}
template < typename T > T ABS(T a){
	return a>0?a:-a;
}
int n,rank[100005],val[100005],fa[100005],mv[100005],m,dlt[100005],base=-1;
bool mark[100005];// 0:-1 1:+1
vector < int > G[100005];
inline int enc(int v,int rk){
	if(v==1e9) return 1e9;
	//printf("%d \t%d\n",v,rk);
	if(base!=-1 && ((v+rk+base)&1)){
		printf("No");
		exit(0);
	}
	else if(base==-1){
		if((v+rk)&1) base=1; else base=0;
	}
	return ((v+rk+base)>>1);
}
inline int dec(int v,int rk){
	return (v<<1)-rk;
}
void dfs(int ver,int f){
	fa[ver]=f;
	if(f>=0)rank[ver]=rank[f]+1;
	int xb;
	mv[ver]=enc(val[ver],rank[ver]);
	for(xb=0;xb<G[ver].size();xb++){
		if(G[ver][xb]==f) continue;
		dfs(G[ver][xb],ver);
		mv[ver]=MIN(mv[G[ver][xb]],mv[ver]);
	}
	if(mv[ver]>0){
		mark[ver]=1;
	//	printf("Go %d\n",ver+1);
		mv[ver]--;
	}
}
void getdlt(int ver,int f){
	//printf("Getting dlt %d\n",ver);
	if(f>=0){
	//	printf("%d\n",mark[ver]);
		if(mark[ver]) dlt[ver]=dlt[f]+1; else dlt[ver]=dlt[f]-1;
	}
	else dlt[ver]=0;
	int xb;
	for(xb=0;xb<G[ver].size();xb++){
	//	printf("atp %d\n",G[ver][xb]+1);
		if(G[ver][xb]!=f) getdlt(G[ver][xb],ver);
	}
}
int main(){
	scanf("%d",&n);
	int xb,x2,a,b;
	fill(val,val+100005,1e9);
	for(xb=0;xb<n-1;xb++){
		scanf("%d%d",&a,&b);
		a--;
		b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	scanf("%d",&m);
	for(xb=0;xb<m;xb++){
		scanf("%d%d",&a,&b);
		val[a-1]=b;
	}
	dfs(0,-1);
	if(mv[0]<1e8){
		getdlt(0,-1);
		//for(xb=0;xb<n;xb++) printf("\t%d %d\n",xb+1,dlt[xb]);
		int v0=-1e9;
		for(xb=0;xb<n;xb++) {
			if(val[xb]!=1e9){
				if(v0!=-1e9 && v0!=val[xb]-dlt[xb]){
					printf("No");
					return 0;
				}
				v0=val[xb]-dlt[xb];
			}
		}
		printf("Yes\n");
		for(xb=0;xb<n;xb++) printf("%d\n",dlt[xb]+v0);
	}
	else{
		printf("Yes\n");
		getdlt(0,-1);
		for(xb=0;xb<n;xb++) printf("%d\n",dlt[xb]);
	}
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:37:17: error: reference to 'rank' is ambiguous
   37 |         if(f>=0)rank[ver]=rank[f]+1;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/utility:69,
                 from a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:7: note:                 'int rank [100005]'
   17 | int n,rank[100005],val[100005],fa[100005],mv[100005],m,dlt[100005],base=-1;
      |       ^~~~
a.cc:37:27: error: reference to 'rank' is ambiguous
   37 |         if(f>=0)rank[ver]=rank[f]+1;
      |                           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:7: note:                 'int rank [100005]'
   17 | int n,rank[100005],val[100005],fa[100005],mv[100005],m,dlt[100005],base=-1;
      |       ^~~~
a.cc:39:30: error: reference to 'rank' is ambiguous
   39 |         mv[ver]=enc(val[ver],rank[ver]);
      |                              ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:17:7: note:                 'int rank [100005]'
   17 | int n,rank[100005],val[100005],fa[100005],mv[100005],m,dlt[100005],base=-1;
      |       ^~~~