stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define eps 1e-9

#define ALL(x) x.begin(),x.end()

#define INS(x) inserter(x,x.begin())

#define rep(i,j,k) for(int i=j;i<=k;i++)

#define MAXN 10005

#define MAXM 40005

#define INF 0x3fffffff

#define PB push_back

#define MP make_pair

#define X first

#define Y second

#define clr(x,y) memset(x,y,sizeof(x));

using namespace std;

typedef long long LL;

int i,j,k,n,m,x,y,T,ans,big,cas,num,len;

bool flag;



int edge,head[MAXN];

struct node

{

	int x,y,w;

}p[10005];



bool cmp(node a,node b)

{

	return a.w<b.w;

}



int pre[10005];   //??根?点，起始??pre[i]=i

int find(int u)   //?找u?点??集合的根?点

{

	int r=u;

	while (pre[r]!=r) r=pre[r];  //沿着?向上不断找father，直到找到根?点root (存入r中)

	int i=u,j;

	while (i!=r)   //路径??，即把向上找???的?个?点（重新沿?点向上找一遍），他?的father都??root

	{

		j=pre[i];

		pre[i]=r;

		i=j;

	}

	return r;  //返回根?点的?号

}



int main()

{

	while (scanf("%d%d",&n,&m),n+m)

	{

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

		edge=0;

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

		{

			scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].w);

		}

		sort(p,p+m,cmp);

		for (i=1;i<=n;i++) pre[i]=i;

		int t=n/2;num=0;

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

		{

			int fx=find(p[i].x);

			int fy=find(p[i].y);

			if (fx!=fy)

			{

				pre[fx]=fy;

				num++;

				if (num==t)

				{

					printf("%d\n",p[i].w);

					break;

				}

			}

		}

		

	}

	

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



const int MAXN = 100000;

struct node{

	int u,v,dist;

}a[MAXN + 10];

int n,m;

int fa[MAXN + 10];

int cnt;

int num[MAXN + 10];

int tot;



bool cmp(const node &x,const node &y){

	return x.dist < y.dist;

}



int find(int x){

	return x == fa[x] ? x : fa[x] = find(fa[x]);

}



int main(){

	while(scanf("%d%d",&n,&m) == 2 && n && m){

		cnt = 0;

		tot = 0;

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

			int u,v,dist;

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

			a[++cnt].u = u;

			a[cnt].v = v;

			a[cnt].dist = dist;

			a[++cnt].v = u;

			a[cnt].u = v;

			a[cnt].dist = dist;

		}

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

			fa[i] = i;

		sort(a + 1,a + cnt + 1,cmp);

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

			int x = find(a[i].u);

			int y = find(a[i].v);

			if(x == y)

				continue;

			num[++tot] = a[i].dist;

			fa[x] =y;

		}

		printf("%d\n",num[(tot + 1) / 2]);

	}

}//

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int MAXN=1005;

const int MAXM=10005;

int fa[MAXN];

pair< int, pair<int, int> > E[MAXM];

int find(int x){

    return fa[x]=fa[x]==x?x:find(fa[x]);

}

int main(){

    int n, m;

    while(cin>>n>>m&&(n||m)){

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

            fa[i]=i;

        }

        for(int i=0, x, y, w; i<m; i++){

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

            E[i]=make_pair(w,make_pair(x,y));

        }

        sort(E,E+m);

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

            int u=find(E[i].second.first);

            int v=find(E[i].second.second);

            int w=E[i].first;

            if(u!=v){

                fa[u]=v;

                cnt++;

                if(cnt*2>=n-1){

                    printf("%d\n",w);

                    break;

                }

            }

        }

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;



const int MAXN = 100000;

struct node{

	int u,v,dist;

}a[MAXN + 10];

int n,m;

int fa[MAXN + 10];

int cnt;

int num[MAXN + 10];

int tot;



bool cmp(const node &x,const node &y){

	return x.dist < y.dist;

}



int find(int x){

	return x == fa[x] ? x : fa[x] = find(fa[x]);

}



int main(){

	while(scanf("%d%d",&n,&m) == 2 && n && m){

		cnt = 0;

		tot = 0;

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

			int u,v,dist;

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

			a[++cnt].u = u;

			a[cnt].v = v;

			a[cnt].dist = dist;

			a[++cnt].v = u;

			a[cnt].u = v;

			a[cnt].dist = dist;

		}

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

			fa[i] = i;

		sort(a + 1,a + cnt + 1,cmp);

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

			int x = find(a[i].u);

			int y = find(a[i].v);

			if(x == y)

				continue;

			num[++tot] = a[i].dist;

			fa[x] =y;

		}

		printf("%d\n",num[(tot + 1) / 2]);

	}

}//

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<string>

using namespace std;

#define rep(i,n) for(int i = 0 ; i < n ; ++i)

#define INF ( 1 << 30 )

int C[301],V[3][301],N,M;

int solve(){

  int rec = 0,dp[301][301];

  rep(a,3){

    fill_n(dp[0],301*301,0);

    rep(i,N) rep(j,M+1){

      if(j < C[i]) dp[i+1][j] = dp[i][j];

      else dp[i+1][j] = max(dp[i][j],dp[i+1][j-C[i]] + V[a][i]);

    }

    rec = max(rec,dp[N][M]);

  }

  return rec;

}

int main(){

  string nop;

  while(cin >> N >> M){

    rep(i,N){

      rep(j,2)getline(cin,nop);

      cin >> C[i] >> V[0][i] >> V[1][i] >> V[2][i];

    }

    cout << solve() << endl;

  }

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

#include<algorithm>

#include<string>

using namespace std;



int main() {

  int N, M, cost[300], exp[300][3], dp[301];

  while(cin >> N >> M){

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

      string s;

      getline(cin, s), getline(cin, s);

      cin >> cost[i] >> exp[i][0] >> exp[i][1] >> exp[i][2];

    }

    int ret = 0;

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

      fill_n( dp, 301, 0);

      for(int j = 0 ; j < N ; j++ ){

        for(int k = 0 ; k <= M ; k++ ){

          if(k - cost[j] >= 0 ){

            dp[k] = max( dp[k], dp[k-cost[j]] + exp[j][i]);

            ret = max( ret, dp[k]);

          }

        }

      }

    }

    cout << ret << endl;

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<algorithm>



using namespace std;



int memo[3][301];



int main(){

	for(int n,m;cin>>n>>m;){

		for(int i=0;i<3;i++)for(int j=0;j<=m;j++)	memo[i][j]=(j?-1:0);



		int ans=0;

		while(n--){

			getchar();	while(getchar()!='\n');

			int cost,prm[3];

			cin>>cost>>prm[0]>>prm[1]>>prm[2];

//			scanf("%d%d%d%d ",&cost,prm,prm+1,prm+2);

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

				if(memo[i][j]==-1)	continue;

				memo[i][j+cost]=max(memo[i][j+cost],memo[i][j]+prm[i]);

				ans=max(ans,memo[i][j+cost]);

			}

		}

		printf("%d\n",ans);

	}



	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>



using namespace std;



int memo[3][301];



int main(){

	for(int n,m;~scanf("%d%d",&n,&m);){

		for(int i=0;i<3;i++)for(int j=0;j<=m;j++)	memo[i][j]=(j?-1:0);



		int ans=0;

		while(n--){

			getchar();	while(getchar()!='\n');

			int cost,prm[3];

			scanf("%d%d%d%d",&cost,prm,prm+1,prm+2);

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

				if(memo[i][j]==-1)	continue;

				memo[i][j+cost]=max(memo[i][j+cost],memo[i][j]+prm[i]);

				ans=max(ans,memo[i][j+cost]);

			}

		}

		printf("%d\n",ans);

	}



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;



int main(){

  int n,m;

  int d[300][4];

  int dp[3][301];

  string tmp;



  while(cin >> n >> m){

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

      cin.ignore();

      getline(cin,tmp);

      for(int j=0;j<4;j++)cin >> d[i][j];

    }



    



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

      for(int j=0;j<=m;j++)dp[i][j] = 0;

    }



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

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

	for(int k=0;k<=j;k++){

	  dp[i][j] = max(dp[i][j],dp[i][k]+dp[i][j-k]);

	  for(int l=0;l<n;l++){

	    if(d[l][0] == j-k)dp[i][j] = max(dp[i][j],dp[i][k]+d[l][i+1]);

	  }

	}

      }

    }



    int ans = 0;

    for(int i=0;i<3;i++)ans = max(ans,dp[i][m]);

    cout << ans << endl;

  }

}

// Optimized solution to the same problem (faster):
#include<iostream>

using namespace std;



int main(){

  int n,m;

  int d[300][4];

  int dp[3][301];

  string tmp;



  while(cin >> n >> m){

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

      cin.ignore();

      getline(cin,tmp);

      for(int j=0;j<4;j++)cin >> d[i][j];

    }



    



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

      for(int j=0;j<=m;j++)dp[i][j] = 0;

    }



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

      for(int j=0;j<3;j++){

	dp[j][d[i][0]] = max(dp[j][d[i][0]],d[i][j+1]);

      }

    }



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

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

	for(int k=0;k<=j/2;k++){

	  dp[i][j] = max(dp[i][j],dp[i][k]+dp[i][j-k]);

	}

      }

    }



    int ans = 0;

    for(int i=0;i<3;i++)ans = max(ans,dp[i][m]);

    cout << ans << endl;

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;



int main(){

  int n,m;

  int d[300][4];

  int dp[3][301];

  string tmp;



  while(cin >> n >> m){

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

      cin.ignore();

      getline(cin,tmp);

      for(int j=0;j<4;j++)cin >> d[i][j];

    }



    



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

      for(int j=0;j<=m;j++)dp[i][j] = 0;

    }



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

      for(int j=0;j<3;j++){

	dp[j][d[i][0]] = max(dp[j][d[i][0]],d[i][j+1]);

      }

    }



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

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

	for(int k=0;k<=j;k++){

	  dp[i][j] = max(dp[i][j],dp[i][k]+dp[i][j-k]);

	}

      }

    }



    int ans = 0;

    for(int i=0;i<3;i++)ans = max(ans,dp[i][m]);

    cout << ans << endl;

  }

}

// Optimized solution to the same problem (faster):
#include<iostream>

using namespace std;



int main(){

  int n,m;

  int d[300][4];

  int dp[3][301];

  string tmp;



  while(cin >> n >> m){

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

      cin.ignore();

      getline(cin,tmp);

      for(int j=0;j<4;j++)cin >> d[i][j];

    }



    



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

      for(int j=0;j<=m;j++)dp[i][j] = 0;

    }



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

      for(int j=0;j<3;j++){

	dp[j][d[i][0]] = max(dp[j][d[i][0]],d[i][j+1]);

      }

    }



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

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

	for(int k=0;k<=j/2;k++){

	  dp[i][j] = max(dp[i][j],dp[i][k]+dp[i][j-k]);

	}

      }

    }



    int ans = 0;

    for(int i=0;i<3;i++)ans = max(ans,dp[i][m]);

    cout << ans << endl;

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

	}

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

	

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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]);

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem: