stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<cstdio>

#include<queue>

#include<vector>

#include<algorithm>

using namespace std;

int n,m,ans[100005];

vector<int>g[100005];

#define INF 0x3f3f3f3f

struct mzls

{

	int a,b;

	bool operator < (const mzls &x) const

	{

		return x.a<a;

	}

}as;

priority_queue<mzls>que;

int main()

{

	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].push_back(y);

		g[y].push_back(x);

		ans[i]=INF;

	}

	ans[n]=INF;

	scanf("%d",&m);

	for(int i=1;i<=m;i++)

	{

		scanf("%d%d",&as.b,&as.a);

		que.push(as);

		ans[as.b]=as.a;

	}

	while(!que.empty())

	{

		as=que.top();

		que.pop();

		int u=as.b;

		for(int i=0;i<g[u].size();i++)

		{

			int v=g[u][i];

			if(ans[v]==INF)

			{

				ans[v]=ans[u]+1;

				as.b=v,as.a=ans[v];

				que.push(as);

			}

			if(!((ans[v]-ans[u])==1||(ans[v]-ans[u])==-1))

			{

				printf("No");

				return 0;

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

		printf("%d\n",ans[i]);

}

// Faster C++ solution for the same problem (measured speedup: 3.4541298903797277x):
#include<bits/stdc++.h>

  using namespace std;



#define Abigail inline void

typedef long long LL;



const int N=100000;



int n,a[N+9],vis[N+9];

struct side{

  int y,next;

}e[N*2+9];

int lin[N+9],cs;



void Ins(int x,int y){e[++cs].y=y;e[cs].next=lin[x];lin[x]=cs;}

void Ins2(int x,int y){Ins(x,y);Ins(y,x);}



struct state{

  int x,v;

  state(int X=0,int V=0){x=X;v=V;}

  bool operator > (const state &p)const{return v>p.v;}

};

priority_queue<state,vector<state>,greater<state> >q;



bool Bfs_ans(){

  for (int i=1;i<=n;++i)

    if (vis[i]) q.push(state(i,a[i]));

  for (;!q.empty();){

  	int t=q.top().x;q.pop();

  	for (int i=lin[t];i;i=e[i].next)

  	  if (vis[e[i].y]){

  	  	if (abs(a[t]-a[e[i].y])^1) return 0;

  	  }else{

  	    vis[e[i].y]=1;

	    q.push(state(e[i].y,a[e[i].y]=a[t]+1));

  	  }

  }

  return 1;

}



Abigail into(){

  scanf("%d",&n);

  for (int i=1;i<n;++i){

  	int x,y;

  	scanf("%d%d",&x,&y);

  	Ins2(x,y);

  }

  int m;

  scanf("%d",&m);

  for (int i=1;i<=m;++i){

  	int x,v;

  	scanf("%d%d",&x,&v);

  	a[x]=v;vis[x]=1;

  }

}



Abigail outo(){

  if (Bfs_ans()){

  	puts("Yes");

  	for (int i=1;i<=n;++i)

  	  printf("%d\n",a[i]);

  }else puts("No");

}



int main(){

  into();

  outo();

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



struct Node{

	int x, id;

	Node(){}

	Node(int _x, int _id){x = _x, id = _id;}

	bool operator < (const Node &A) const{

		return id > A.id;

	}

};



vector <int> from[100010];

priority_queue <Node> q;

int id[100010];



int main(){

	int n;

	scanf("%d", &n);

	for (int i = 1; i < n; i++){

		int u, v;

		scanf("%d%d", &u, &v);

		from[u].push_back(v);

		from[v].push_back(u);

	}

	int m;

	scanf("%d", &m);

	memset(id, -1, sizeof(id));

	for (int i = 0; i < m; i++){

		int a, b;

		scanf("%d%d", &a, &b);

		id[a] = b;

		q.push(Node(a, b));

	}

	while (!q.empty()){

		Node t = q.top(); q.pop();

		int u = t.x;

		for (int i = 0; i < from[u].size(); i++){

			int v = from[u][i];

			if (id[v] == -1) id[v] = t.id + 1, q.push(Node(v, t.id + 1));

		}

	}

	bool pd = 0;

	for (int i = 1; i <= n; i++){

		for (int j = 0; j < from[i].size(); j++){

			int v = from[i][j];

			if (abs(id[i] - id[v]) != 1){

				pd = 1;

				break;

			}

		}

		if (pd) break;

	}

	if (pd){

		printf("No\n");

		return 0;

	}

	printf("Yes\n");

	for (int i = 1; i <= n; i++){

		printf("%d\n", id[i]);

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.9315294336067357x):
#include<bits/stdc++.h>

  using namespace std;



#define Abigail inline void

typedef long long LL;



const int N=100000;



int n,a[N+9],vis[N+9];

struct side{

  int y,next;

}e[N*2+9];

int lin[N+9],cs;



void Ins(int x,int y){e[++cs].y=y;e[cs].next=lin[x];lin[x]=cs;}

void Ins2(int x,int y){Ins(x,y);Ins(y,x);}



struct state{

  int x,v;

  state(int X=0,int V=0){x=X;v=V;}

  bool operator > (const state &p)const{return v>p.v;}

};

priority_queue<state,vector<state>,greater<state> >q;



bool Bfs_ans(){

  for (int i=1;i<=n;++i)

    if (vis[i]) q.push(state(i,a[i]));

  for (;!q.empty();){

  	int t=q.top().x;q.pop();

  	for (int i=lin[t];i;i=e[i].next)

  	  if (vis[e[i].y]){

  	  	if (abs(a[t]-a[e[i].y])^1) return 0;

  	  }else{

  	    vis[e[i].y]=1;

	    q.push(state(e[i].y,a[e[i].y]=a[t]+1));

  	  }

  }

  return 1;

}



Abigail into(){

  scanf("%d",&n);

  for (int i=1;i<n;++i){

  	int x,y;

  	scanf("%d%d",&x,&y);

  	Ins2(x,y);

  }

  int m;

  scanf("%d",&m);

  for (int i=1;i<=m;++i){

  	int x,v;

  	scanf("%d%d",&x,&v);

  	a[x]=v;vis[x]=1;

  }

}



Abigail outo(){

  if (Bfs_ans()){

  	puts("Yes");

  	for (int i=1;i<=n;++i)

  	  printf("%d\n",a[i]);

  }else puts("No");

}



int main(){

  into();

  outo();

  return 0;

}
// Slower C++ solution:
#include <vector>

#include <queue>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>



using namespace std;

const int N = 200000;



int n;

int ans[N];

vector <int> a[N];

priority_queue< pair<int,int> > q;



int main(void)

{

	scanf("%d", &n);

	for (int i=1;i<n;++i)

	{

		int x, y; scanf("%d %d", &x, &y);

		a[x].push_back(y);

		a[y].push_back(x);

	}

	memset(ans,-1,sizeof ans);

	int m; scanf("%d", &m);

	for (int i=1;i<=m;++i)

	{

		int x, v; scanf("%d %d", &x, &v);

		ans[x] = v; q.push(make_pair(-v,x));

	}

	while (q.size())

	{

		int x = q.top().second;

		int v = -q.top().first;

		q.pop();

		for (int i=0;i<a[x].size();++i)

		{

			int y = a[x][i];

			if (ans[y] >= 0 and abs(ans[x]-ans[y]) ^ 1) {

				puts("No");

				return 0;

			}

			else if (ans[y] < 0) {

				ans[y] = v + 1;

				q.push(make_pair(-ans[y],y));

			}

		}

	}

	puts("Yes"); 

	for (int i=1;i<=n;++i)

		printf("%d\n", ans[i]);

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 7.3915687234150615x):
#include<bits/stdc++.h>

  using namespace std;



#define Abigail inline void

typedef long long LL;



const int N=100000;



int n,a[N+9],vis[N+9];

struct side{

  int y,next;

}e[N*2+9];

int lin[N+9],cs;



void Ins(int x,int y){e[++cs].y=y;e[cs].next=lin[x];lin[x]=cs;}

void Ins2(int x,int y){Ins(x,y);Ins(y,x);}



struct state{

  int x,v;

  state(int X=0,int V=0){x=X;v=V;}

  bool operator > (const state &p)const{return v>p.v;}

};

priority_queue<state,vector<state>,greater<state> >q;



bool Bfs_ans(){

  for (int i=1;i<=n;++i)

    if (vis[i]) q.push(state(i,a[i]));

  for (;!q.empty();){

  	int t=q.top().x;q.pop();

  	for (int i=lin[t];i;i=e[i].next)

  	  if (vis[e[i].y]){

  	  	if (abs(a[t]-a[e[i].y])^1) return 0;

  	  }else{

  	    vis[e[i].y]=1;

	    q.push(state(e[i].y,a[e[i].y]=a[t]+1));

  	  }

  }

  return 1;

}



Abigail into(){

  scanf("%d",&n);

  for (int i=1;i<n;++i){

  	int x,y;

  	scanf("%d%d",&x,&y);

  	Ins2(x,y);

  }

  int m;

  scanf("%d",&m);

  for (int i=1;i<=m;++i){

  	int x,v;

  	scanf("%d%d",&x,&v);

  	a[x]=v;vis[x]=1;

  }

}



Abigail outo(){

  if (Bfs_ans()){

  	puts("Yes");

  	for (int i=1;i<=n;++i)

  	  printf("%d\n",a[i]);

  }else puts("No");

}



int main(){

  into();

  outo();

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5;

const int inf = 0x3f3f3f3f;



vector <int> g[N];

int n, k, dist[N];

priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;



void input(){

    scanf("%d", &n);

    for (int i = 1; i <= n - 1; i++) {

        int u, v;

        scanf("%d %d", &u, &v);

        g[u].push_back(v);

        g[v].push_back(u);

    }



    scanf("%d", &k);

    memset(dist, 0x3f, sizeof(dist));

    for (int i = 1; i <= k; i++) {

        int u, x;

        scanf("%d %d", &u, &x);

        dist[u] = x;

        pq.push(make_pair(dist[u], u));

    }

}



void dijkstra(){

    while (!pq.empty()) {

        int d = pq.top().first;

        int u = pq.top().second;

        pq.pop();



        for (int v: g[u]) {

            if (dist[v] == inf) {

                dist[v] = dist[u] + 1;

                pq.push(make_pair(dist[v], v));

            }

            else if (abs(dist[v] - dist[u]) != 1) {

                printf("No");

                return;

            }

        }

    }



    puts("Yes");

    for (int i = 1; i <= n; i++) printf("%d\n", dist[i]);

}



void solve(){

    dijkstra();

}



signed main(){

    input();

    solve();

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.5096248998216677x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define ii pair<int,int>

#define mp make_pair

int INF=3e5;

vector<int> adjlist[100010];

int ans[100010];

int main()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

	priority_queue<ii,vector<ii>,greater<ii> > pq;

	int n,q;

	cin >> n;

	for(int i=1;i<=n-1;i++)

	{

		int u,v;

		cin >> u >> v;

		adjlist[u].push_back(v);

		adjlist[v].push_back(u);

	}

	for(int i=1;i<=n;i++)

    {

        ans[i]=INF;

    }

	cin >> q;

	for(int i=1;i<=q;i++)

	{

		int u,num;

		cin >> u >> num;

		ans[u]=num;

		pq.push(ii(num,u));

	}

	while(!pq.empty())

	{

		int u=pq.top().second,num=pq.top().first;

		pq.pop();

		for(int i=0;i<adjlist[u].size();i++)

		{

			int v=adjlist[u][i];

			if(ans[v]==INF)

			{

				ans[v]=num+1;

				pq.push(ii(ans[v],v));

			}

			if(abs(ans[u]-ans[v])!=1)

			{

				cout << "No" << "\n";

				return 0;

			}

		}

	}

	cout << "Yes" << "\n";

	for(int i=1;i<=n;i++)

	{

		cout << ans[i] << "\n";

	}

}


// Faster C++ solution for the same problem (measured speedup: 1.3718313733775238x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e5 + 5;

const int INF = 1e18;

#define pb push_back

#define ii pair<int,int>

int ans[N];

bool check[N];

vector<int> g[N];

priority_queue<ii,vector<ii>,greater<ii> > pq;

signed main(){

	int n; cin >> n;

	for(int i = 1 ; i < n ; ++i)

	{

		int a, b; cin >> a >> b;

		g[a].pb(b); g[b].pb(a);

	}

	int k; cin >> k;

	for(int i = 1 ; i <= n ; ++i) ans[i] = INF;

	for(int i = 1 ; i <= k ; ++i)

	{

		int u , num; cin >> u >> num;

		pq.push(ii(num , u));

		ans[u] = num; 

	}

	while(!pq.empty())

	{

		int u = pq.top().second;

		int num = pq.top().first;

		pq.pop();

		for(int i = 0 ; i < g[u].size() ; ++i)

		{

			int v = g[u][i];

			if(ans[v] == INF)

			{

				ans[v] = num + 1;

				pq.push(ii(ans[v] , v));

			}

			if(abs(ans[u]-ans[v])!=1)

			{

				cout << "No" << "\n";

				return 0;

			}

		}

	}

	cout << "Yes\n";

	for(int i = 1 ; i <= n ; ++i) cout << ans[i] << "\n";

	

}

// Faster C++ solution for the same problem (measured speedup: 1.3603571119490039x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include<cstdio>

#include<queue>

#include<vector>

#include<algorithm>

using namespace std;

int n,m,ans[100005];

vector<int>g[100005];

#define INF 0x3f3f3f3f

struct mzls

{

	int a,b;

	bool operator < (const mzls &x) const

	{

		return x.a<a;

	}

}as;

priority_queue<mzls>que;

int main()

{

	scanf("%d",&n);

	for(int i=1;i<n;i++)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].push_back(y);

		g[y].push_back(x);

		ans[i]=INF;

	}

	ans[n]=INF;

	scanf("%d",&m);

	for(int i=1;i<=m;i++)

	{

		scanf("%d%d",&as.b,&as.a);

		que.push(as);

		ans[as.b]=as.a;

	}

	while(!que.empty())

	{

		as=que.top();

		que.pop();

		int u=as.b;

		for(int i=0;i<g[u].size();i++)

		{

			int v=g[u][i];

			if(ans[v]==INF)

			{

				ans[v]=ans[u]+1;

				as.b=v,as.a=ans[v];

				que.push(as);

			}

			if(!((ans[v]-ans[u])==1||(ans[v]-ans[u])==-1))

			{

				printf("No");

				return 0;

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

		printf("%d\n",ans[i]);

}

// Faster C++ solution for the same problem (measured speedup: 1.3261414531830646x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



struct Node{

	int x, id;

	Node(){}

	Node(int _x, int _id){x = _x, id = _id;}

	bool operator < (const Node &A) const{

		return id > A.id;

	}

};



vector <int> from[100010];

priority_queue <Node> q;

int id[100010];



int main(){

	int n;

	scanf("%d", &n);

	for (int i = 1; i < n; i++){

		int u, v;

		scanf("%d%d", &u, &v);

		from[u].push_back(v);

		from[v].push_back(u);

	}

	int m;

	scanf("%d", &m);

	memset(id, -1, sizeof(id));

	for (int i = 0; i < m; i++){

		int a, b;

		scanf("%d%d", &a, &b);

		id[a] = b;

		q.push(Node(a, b));

	}

	while (!q.empty()){

		Node t = q.top(); q.pop();

		int u = t.x;

		for (int i = 0; i < from[u].size(); i++){

			int v = from[u][i];

			if (id[v] == -1) id[v] = t.id + 1, q.push(Node(v, t.id + 1));

		}

	}

	bool pd = 0;

	for (int i = 1; i <= n; i++){

		for (int j = 0; j < from[i].size(); j++){

			int v = from[i][j];

			if (abs(id[i] - id[v]) != 1){

				pd = 1;

				break;

			}

		}

		if (pd) break;

	}

	if (pd){

		printf("No\n");

		return 0;

	}

	printf("Yes\n");

	for (int i = 1; i <= n; i++){

		printf("%d\n", id[i]);

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.5094290955404852x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include <vector>

#include <queue>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>



using namespace std;

const int N = 200000;



int n;

int ans[N];

vector <int> a[N];

priority_queue< pair<int,int> > q;



int main(void)

{

	scanf("%d", &n);

	for (int i=1;i<n;++i)

	{

		int x, y; scanf("%d %d", &x, &y);

		a[x].push_back(y);

		a[y].push_back(x);

	}

	memset(ans,-1,sizeof ans);

	int m; scanf("%d", &m);

	for (int i=1;i<=m;++i)

	{

		int x, v; scanf("%d %d", &x, &v);

		ans[x] = v; q.push(make_pair(-v,x));

	}

	while (q.size())

	{

		int x = q.top().second;

		int v = -q.top().first;

		q.pop();

		for (int i=0;i<a[x].size();++i)

		{

			int y = a[x][i];

			if (ans[y] >= 0 and abs(ans[x]-ans[y]) ^ 1) {

				puts("No");

				return 0;

			}

			else if (ans[y] < 0) {

				ans[y] = v + 1;

				q.push(make_pair(-ans[y],y));

			}

		}

	}

	puts("Yes"); 

	for (int i=1;i<=n;++i)

		printf("%d\n", ans[i]);

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 2.8378393399370783x):
#include<bits/stdc++.h>

using namespace std;

#define pa pair<int,int>

#define N 100011

int head[N<<2],cnt,vis[N],val[N],a,b,n,k;

priority_queue<pa> p;

typedef struct{

	int to,nxt;

}edge;

edge e[N<<2];

void add(int a,int b){

	cnt++;

	e[cnt].to=b;

	e[cnt].nxt=head[a];

	head[a]=cnt;

}

int main(){

	memset(head,-1,sizeof(head));

	scanf("%d",&n);

	for(int i=1;i<=n-1;i++){

		scanf("%d%d",&a,&b);

		add(a,b);add(b,a);

	}

	scanf("%d",&k);

	while(k--){

		scanf("%d%d",&a,&b);

		val[a]=b;

		vis[a]=1;

		p.push(pa(-b,a));

	}

	while(!p.empty()){

		int a=p.top().first;int b=p.top().second;p.pop();

		for(int i=head[b];i!=-1;i=e[i].nxt){

			int v=e[i].to;

			if(vis[v]){

				if(abs(val[v]-val[b])!=1){

					printf("No\n");

					return 0;

				}

			}else{

				vis[v]=1;

				val[v]=-a+1;

				p.push(pa(-val[v],v));

			}

		}

	}

	printf("Yes\n");

	for(int i=1;i<=n;i++)

	  printf("%d\n",val[i]);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5;

const int inf = 0x3f3f3f3f;



vector <int> g[N];

int n, k, dist[N];

priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;



void input(){

    scanf("%d", &n);

    for (int i = 1; i <= n - 1; i++) {

        int u, v;

        scanf("%d %d", &u, &v);

        g[u].push_back(v);

        g[v].push_back(u);

    }



    scanf("%d", &k);

    memset(dist, 0x3f, sizeof(dist));

    for (int i = 1; i <= k; i++) {

        int u, x;

        scanf("%d %d", &u, &x);

        dist[u] = x;

        pq.push(make_pair(dist[u], u));

    }

}



void dijkstra(){

    while (!pq.empty()) {

        int d = pq.top().first;

        int u = pq.top().second;

        pq.pop();



        for (int v: g[u]) {

            if (dist[v] == inf) {

                dist[v] = dist[u] + 1;

                pq.push(make_pair(dist[v], v));

            }

            else if (abs(dist[v] - dist[u]) != 1) {

                printf("No");

                return;

            }

        }

    }



    puts("Yes");

    for (int i = 1; i <= n; i++) printf("%d\n", dist[i]);

}



void solve(){

    dijkstra();

}



signed main(){

    input();

    solve();

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1142824609032598x):
#include <bits/stdc++.h>

#define maxn 100086



using namespace std;



int n, k;

int x, y;

vector<int> v[maxn];

int a[maxn];

bool vis[maxn];



priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;



int main(){

	scanf("%d", &n);

	for(int i = 1;i < n;i++){

		scanf("%d%d", &x, &y);

		v[x].push_back(y), v[y].push_back(x);

	}

	scanf("%d", &k);

	while(k--){

		scanf("%d%d", &x, &y);

		a[x] = y;

		vis[x] = true;

		q.push({y, x});

	}

	while(!q.empty()){

		pair<int, int> p = q.top();q.pop();

		x = p.second;

		for(int i = 0;i < v[x].size();i++){

			int to = v[x][i];

			if(!vis[to]){

				a[to] = a[x] + 1;

				q.push({a[to], to});

				vis[to] = true; 

			}else if(abs(a[to] - a[x]) != 1) return printf("No"), 0;

		}

	}

	puts("Yes");

	for(int i = 1;i <= n;i++) printf("%d\n", a[i]);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



struct Node{

	int x, id;

	Node(){}

	Node(int _x, int _id){x = _x, id = _id;}

	bool operator < (const Node &A) const{

		return id > A.id;

	}

};



vector <int> from[100010];

priority_queue <Node> q;

int id[100010];



int main(){

	int n;

	scanf("%d", &n);

	for (int i = 1; i < n; i++){

		int u, v;

		scanf("%d%d", &u, &v);

		from[u].push_back(v);

		from[v].push_back(u);

	}

	int m;

	scanf("%d", &m);

	memset(id, -1, sizeof(id));

	for (int i = 0; i < m; i++){

		int a, b;

		scanf("%d%d", &a, &b);

		id[a] = b;

		q.push(Node(a, b));

	}

	while (!q.empty()){

		Node t = q.top(); q.pop();

		int u = t.x;

		for (int i = 0; i < from[u].size(); i++){

			int v = from[u][i];

			if (id[v] == -1) id[v] = t.id + 1, q.push(Node(v, t.id + 1));

		}

	}

	bool pd = 0;

	for (int i = 1; i <= n; i++){

		for (int j = 0; j < from[i].size(); j++){

			int v = from[i][j];

			if (abs(id[i] - id[v]) != 1){

				pd = 1;

				break;

			}

		}

		if (pd) break;

	}

	if (pd){

		printf("No\n");

		return 0;

	}

	printf("Yes\n");

	for (int i = 1; i <= n; i++){

		printf("%d\n", id[i]);

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1141379340897994x):
#include <bits/stdc++.h>

#define maxn 100086



using namespace std;



int n, k;

int x, y;

vector<int> v[maxn];

int a[maxn];

bool vis[maxn];



priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;



int main(){

	scanf("%d", &n);

	for(int i = 1;i < n;i++){

		scanf("%d%d", &x, &y);

		v[x].push_back(y), v[y].push_back(x);

	}

	scanf("%d", &k);

	while(k--){

		scanf("%d%d", &x, &y);

		a[x] = y;

		vis[x] = true;

		q.push({y, x});

	}

	while(!q.empty()){

		pair<int, int> p = q.top();q.pop();

		x = p.second;

		for(int i = 0;i < v[x].size();i++){

			int to = v[x][i];

			if(!vis[to]){

				a[to] = a[x] + 1;

				q.push({a[to], to});

				vis[to] = true; 

			}else if(abs(a[to] - a[x]) != 1) return printf("No"), 0;

		}

	}

	puts("Yes");

	for(int i = 1;i <= n;i++) printf("%d\n", a[i]);

}