stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.5455334169237904x):
#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;

  }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.055362038626839x):
#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;

}
// Slower C++ solution:
#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1699334211685681x):
#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;

  }

}
// Slower C++ solution:
#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.2889448663048357x):
#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;

  }

}
// Slower C++ solution:
#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1017249725350995x):
#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;

  }

}
// 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.100444944705477x):
#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";

	}

}
// 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.1097269140298063x):
#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";

	

}
// 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.1383588803428155x):
#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]);

}
// 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: 3.932039434571319x):
#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;

#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: 3.5731359878468885x):
#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;

#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: 3.543249591282516x):
#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;

}