Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n = 0, m = 0;
int fir[100500], nxt[200500], to[200500], rez[100500][2];
bool vis[100500];
void dfs1(int x, int fa) {
  for (int i = fir[x]; i; i = nxt[i]) {
    int t = to[i];
    if (t == fa)
      continue;
    dfs1(t, x);
    if (not vis[t])
      continue;
    if (not vis[x]) {
      rez[x][0] = rez[t][0] - 1;
      rez[x][1] = rez[t][1] + 1;
      vis[x] = true;
    } else {
      if (rez[x][0] > rez[t][1] + 1 || rez[x][1] < rez[t][0] - 1 ||
          not((rez[x][0] ^ rez[t][0]) & 1))
        cout << "No" << endl, exit(0);
      else
        rez[x][0] = max(rez[x][0], rez[t][0] - 1),
        rez[x][1] = min(rez[x][1], rez[t][1] + 1);
    }
  }
}
int ans[100500];
void dfs2(int x, int fa) {
  if (not fa)
    ans[x] = rez[x][1];
  else if (not vis[x] || ans[fa] + 1 <= rez[x][1])
    ans[x] = ans[fa] + 1;
  else
    ans[x] = ans[fa] - 1;
  for (int i = fir[x]; i; i = nxt[i]) {
    int t = to[i];
    if (t == fa)
      continue;
    dfs2(t, x);
  }
}
signed main() {
  freopen("kary_01.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1, j = 0; i < n; i++) {
    int a = 0, b = 0;
    cin >> a >> b;
    j++, nxt[j] = fir[a], fir[a] = j, to[j] = b;
    j++, nxt[j] = fir[b], fir[b] = j, to[j] = a;
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int x = 0, y = 0;
    cin >> x >> y;
    rez[x][0] = y, rez[x][1] = y, vis[x] = true;
  }
  dfs1(1, 0);
  dfs2(1, 0);
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n = 0, m = 0;
int fir[100500], nxt[200500], to[200500], rez[100500][2];
bool vis[100500];
void dfs1(int x, int fa) {
  for (int i = fir[x]; i; i = nxt[i]) {
    int t = to[i];
    if (t == fa)
      continue;
    dfs1(t, x);
    if (not vis[t])
      continue;
    if (not vis[x]) {
      rez[x][0] = rez[t][0] - 1;
      rez[x][1] = rez[t][1] + 1;
      vis[x] = true;
    } else {
      if (rez[x][0] > rez[t][1] + 1 || rez[x][1] < rez[t][0] - 1 ||
          not((rez[x][0] ^ rez[t][0]) & 1))
        cout << "No" << endl, exit(0);
      else
        rez[x][0] = max(rez[x][0], rez[t][0] - 1),
        rez[x][1] = min(rez[x][1], rez[t][1] + 1);
    }
  }
}
int ans[100500];
void dfs2(int x, int fa) {
  if (not fa)
    ans[x] = rez[x][1];
  else if (not vis[x] || ans[fa] + 1 <= rez[x][1])
    ans[x] = ans[fa] + 1;
  else
    ans[x] = ans[fa] - 1;
  for (int i = fir[x]; i; i = nxt[i]) {
    int t = to[i];
    if (t == fa)
      continue;
    dfs2(t, x);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1, j = 0; i < n; i++) {
    int a = 0, b = 0;
    cin >> a >> b;
    j++, nxt[j] = fir[a], fir[a] = j, to[j] = b;
    j++, nxt[j] = fir[b], fir[b] = j, to[j] = a;
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int x = 0, y = 0;
    cin >> x >> y;
    rez[x][0] = y, rez[x][1] = y, vis[x] = true;
  }
  dfs1(1, 0);
  dfs2(1, 0);
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}