Language: C++

w#include <bits/stdc++.h>
using namespace std;

bool chmax(int &a, int b) {if (a < b) { a = b; return true; } else return false; }
bool chmin(int &a, int b) {if (a > b) { a = b; return true; } else return false; }

const int MAX = 110000;
int N;
vector<int> tree[MAX];

int K;
int P[MAX];
int lo[MAX], hi[MAX];
int res[MAX];

bool dfs(int v, int p) {
  bool can = true;
  lo[v] = hi[v] = P[v];
  for (int i = 0; i < tree[v].size(); ++i) {
    int to = tree[v][i];
    if (to == p) continue;
    if (!dfs(to, v)) return false;
  }
  for (int i = 0; i < tree[v].size(); ++i) {
    int to = tree[v][i];
    if (to == p) continue;
    if (lo[to] == -1) continue;
    int tlo = lo[to] - 1;
    int thi = hi[to] + 1;
    if (lo[v] == -1) {
      lo[v] = tlo;
      hi[v] = thi;
    }
    else {
      if ( abs(lo[v] - tlo) % 2 != 0 ) return false;
      chmax(lo[v], tlo);
      chmin(hi[v], thi);
      if (lo[v] > hi[v]) return false;
    }
  }
  //cout << v << ": " << lo[v] << ", " << hi[v] << endl;
  return true;
}

void reconstruct(int v, int p) {
  if (p == -1) {
    if (lo[v] != -1) res[v] = hi[v];
    else res[v] = 0;
  }
  else {
    if (lo[v] != -1) res[v] = min(hi[v], res[p]+1);
    else res[v] = res[p]+1;
  }
  for (auto to : tree[v]) {
    if (to == p) continue;
    reconstruct(to, v);
  }
}
			    

int main() {
  while (cin >> N) {
    for (int i = 0; i < MAX; ++i) tree[i].clear(), P[i] = -1;
    for (int i = 0; i < N-1; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      --a, --b;
      tree[a].push_back(b);
      tree[b].push_back(a);
    }
    cin >> K;
    for (int i = 0; i < K; ++i) {
      int v, p;
      scanf("%d %d", &v, &p);
      --v;
      P[v] = p;
    }
    bool can = dfs(0, -1);
    reconstruct(0, -1);

    if (can) {
      puts("Yes");
      for (int i = 0; i < N; ++i) cout << res[i] << endl;
    }
    else puts("No");
  }
}


Compiler output:
a.cc:1:2: error: stray '#' in program
    1 | w#include <bits/stdc++.h>
      |  ^
a.cc:1:1: error: 'w' does not name a type
    1 | w#include <bits/stdc++.h>
      | ^
a.cc:9:1: error: 'vector' does not name a type
    9 | vector<int> tree[MAX];
      | ^~~~~~
a.cc: In function 'bool dfs(int, int)':
a.cc:19:23: error: 'tree' was not declared in this scope
   19 |   for (int i = 0; i < tree[v].size(); ++i) {
      |                       ^~~~
a.cc:24:23: error: 'tree' was not declared in this scope
   24 |   for (int i = 0; i < tree[v].size(); ++i) {
      |                       ^~~~
a.cc:35:12: error: 'abs' was not declared in this scope
   35 |       if ( abs(lo[v] - tlo) % 2 != 0 ) return false;
      |            ^~~
a.cc: In function 'void reconstruct(int, int)':
a.cc:51:31: error: 'min' was not declared in this scope
   51 |     if (lo[v] != -1) res[v] = min(hi[v], res[p]+1);
      |                               ^~~
a.cc:54:18: error: 'tree' was not declared in this scope
   54 |   for (auto to : tree[v]) {
      |                  ^~~~
a.cc: In function 'int main()':
a.cc:62:10: error: 'cin' was not declared in this scope
   62 |   while (cin >> N) {
      |          ^~~
a.cc:63:35: error: 'tree' was not declared in this scope
   63 |     for (int i = 0; i < MAX; ++i) tree[i].clear(), P[i] = -1;
      |                                   ^~~~
a.cc:66:7: error: 'scanf' was not declared in this scope
   66 |       scanf("%d %d", &a, &b);
      |       ^~~~~
a.cc:68:7: error: 'tree' was not declared in this scope
   68 |       tree[a].push_back(b);
      |       ^~~~
a.cc:74:7: error: 'scanf' was not declared in this scope
   74 |       scanf("%d %d", &v, &p);
      |       ^~~~~
a.cc:82:7: error: 'puts' was not declared in this scope
   82 |       puts("Yes");
      |       ^~~~
a.cc:83:35: error: 'cout' was not declared in this scope
   83 |       for (int i = 0; i < N; ++i) cout << res[i] << endl;
      |                                   ^~~~
a.cc:83:53: error: 'endl' was not declared in this scope
   83 |       for (int i = 0; i < N; ++i) cout << res[i] << endl;
      |                                                     ^~~~
a.cc:85:10: error: 'puts' was not declared in this scope
   85 |     else puts("No");
      |          ^~~~