Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
// #define int ll
// #define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 2e5 + 5;
vector<int> g[N], w[N];
int c[N];
int32_t main() {
  int n;
  cin >> n;
  memset(c, -1, sizeof c);
  forn(i, n - 1) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  int q;
  cin >> q;
  while (q--) {
    int v, j;
    cin >> v >> j;
    c[v] = j;
    w[j].pb(v);
  }
  forn(j, N) {
    for (int v : w[j]) {
      for (int to : g[v]) {
        if (c[to] == -1) {
          c[to] = j + 1;
          w[j + 1].pb(to);
        } else if (abs(c[to] - c[v]) != 1) {
          cout << "No\n";
          return 0;
        }
      }
    }
  }
  cout << "Yes\n";
  for (int i = 1; i <= n; ++i)
    cout << c[i] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
// #define int ll
// #define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int N = 2e6 + 6;
vector<int> g[N], w[N];
int c[N];
int32_t main() {
  int n;
  cin >> n;
  memset(c, -1, sizeof c);
  forn(i, n - 1) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  int q;
  cin >> q;
  while (q--) {
    int v, j;
    cin >> v >> j;
    c[v] = j;
    w[j].pb(v);
  }
  forn(j, N) {
    for (int v : w[j]) {
      for (int to : g[v]) {
        if (c[to] == -1) {
          c[to] = j + 1;
          w[j + 1].pb(to);
        } else if (abs(c[to] - c[v]) != 1) {
          cout << "No\n";
          return 0;
        }
      }
    }
  }
  cout << "Yes\n";
  for (int i = 1; i <= n; ++i)
    cout << c[i] << '\n';
  return 0;
}