Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int l[N], r[N], odd[N];
vector<int> g[N];
int col[N];
int n;
void dfs(int x, int fa) {
  int la = -1, av = 1;
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    dfs(i, x);
    if (odd[i] != -1) {
      av &= (la == -1 || odd[i] == la);
      la = odd[i];
    }
  }
  if (!av) {
    puts("No");
    exit(0);
  }
  l[x] = -1e9;
  r[x] = 1e9;
  odd[x] = -1;
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    l[x] = max(l[x], l[i] - 1);
    r[x] = min(r[x], r[i] + 1);
    if (odd[i] != -1)
      odd[x] = odd[i] ^ 1;
  }
  if (l[x] > r[x]) {
    puts("No");
    exit(0);
  }
  if (col[x] != -1) {
    if (odd[x] == -1) {
      odd[x] = col[x] & 1;
    }
    if ((col[x] & 1) != odd[x]) {
      puts("No");
      exit(0);
    }
    if (l[x] > col[x] || r[x] < col[x]) {
      puts("No");
      exit(0);
    }
    l[x] = r[x] = col[x];
  }
}
int m;
int val[N];
void color(int x, int fa) {
  if (fa == 0) {
    for (int i = l[x]; i <= r[x]; ++i)
      if (odd[i] == -1 || (i & 1) == odd[i]) {
        val[x] = i;
        break;
      }
  } else {
    if (l[x] <= val[fa] - 1 && val[fa] - 1 <= r[x])
      val[x] = val[fa] - 1;
    else if (l[x] <= val[fa] + 1 && val[fa] + 1 <= r[x])
      val[x] = val[fa] + 1;
  }
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    color(i, x);
  }
}
int main() {
  memset(col, -1, sizeof(col));
  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);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    col[x] = y;
  }
  dfs(1, 0);
  puts("Yes");
  color(1, 0);
  for (int i = 1; i <= n; ++i)
    cout << val[i] << '\n';
}

Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int l[N], r[N], odd[N];
vector<int> g[N];
int col[N];
int n;
void dfs(int x, int fa) {
  int la = -1, av = 1;
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    dfs(i, x);
    if (odd[i] != -1) {
      av &= (la == -1 || odd[i] == la);
      la = odd[i];
    }
  }
  if (!av) {
    puts("No");
    exit(0);
  }
  l[x] = -1e9;
  r[x] = 1e9;
  odd[x] = -1;
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    l[x] = max(l[x], l[i] - 1);
    r[x] = min(r[x], r[i] + 1);
    if (odd[i] != -1)
      odd[x] = odd[i] ^ 1;
  }
  if (l[x] > r[x]) {
    puts("No");
    exit(0);
  }
  if (col[x] != -1) {
    if (odd[x] == -1) {
      odd[x] = col[x] & 1;
    }
    if ((col[x] & 1) != odd[x]) {
      puts("No");
      exit(0);
    }
    if (l[x] > col[x] || r[x] < col[x]) {
      puts("No");
      exit(0);
    }
    l[x] = r[x] = col[x];
  }
}
int m;
int val[N];
void color(int x, int fa) {
  if (fa == 0) {
    for (int i = l[x]; i <= r[x]; ++i)
      if (odd[x] == -1 || (i & 1) == odd[x]) {
        val[x] = i;
        break;
      }
  } else {
    if (l[x] <= val[fa] - 1 && val[fa] - 1 <= r[x])
      val[x] = val[fa] - 1;
    else if (l[x] <= val[fa] + 1 && val[fa] + 1 <= r[x])
      val[x] = val[fa] + 1;
  }
  for (auto i : g[x]) {
    if (i == fa)
      continue;
    color(i, x);
  }
}
int main() {
  memset(col, -1, sizeof(col));
  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);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    col[x] = y;
  }
  dfs(1, 0);
  puts("Yes");
  color(1, 0);
  for (int i = 1; i <= n; ++i)
    cout << val[i] << '\n';
}