Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

vector<int> from[100010];
int L[100010], R[100010];
int id[100010];
bool ans;

void solve(int u = 0, int p = -1) {
  L[u] = -INF - 1, R[u] = INF + 1;
  if (id[u] != -1)
    L[u] = R[u] = id[u];
  for (int i = 0; i < from[u].size(); i++) {
    int v = from[u][i];
    if (v == p)
      continue;
    solve(v, u);
    if (L[v] < -INF / 2)
      continue;
    if (L[u] < -INF / 2)
      L[u] = L[v] - 1, R[u] = R[v] + 1;
    else {
      if (abs(L[v]) % 2 == abs(L[u]) % 2) {
        ans = 0;
        return;
      }
      L[u] = max(L[u], L[v] - 1);
      R[u] = min(R[u], R[v] + 1);
    }
  }
  if (L[u] > R[u])
    ans = 0;
}

void make(int v = 0, int p = -1) {
  if (v == 0 && id[v] == -1)
    id[v] = L[v];
  for (int i = 0; i < from[v].size(); i++) {
    int to = from[v][i];
    if (to == p)
      continue;
    if (id[to] == -1) {
      if (L[to] <= id[v] - 1 && id[v] - 1 <= R[to])
        id[to] = id[v] - 1;
      else
        id[to] = id[v] + 1;
    }
    make(to, v);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    from[a].push_back(b);
    from[b].push_back(a);
  }
  memset(id, -1, sizeof(id));
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int v, p;
    scanf("%d%d", &v, p);
    v--, id[v] = p;
  }
  ans = 1;
  solve();
  if (!ans) {
    puts("No");
    return 0;
  }
  make();
  puts("Yes");
  for (int i = 0; i < n; i++) {
    printf("%d\n", id[i]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

vector<int> from[100010];
int L[100010], R[100010];
int id[100010];
bool ans;

void solve(int u = 0, int p = -1) {
  L[u] = -INF - 1, R[u] = INF + 1;
  if (id[u] != -1)
    L[u] = R[u] = id[u];
  for (int i = 0; i < from[u].size(); i++) {
    int v = from[u][i];
    if (v == p)
      continue;
    solve(v, u);
    if (L[v] < -INF / 2)
      continue;
    if (L[u] < -INF / 2)
      L[u] = L[v] - 1, R[u] = R[v] + 1;
    else {
      if (abs(L[v]) % 2 == abs(L[u]) % 2) {
        ans = 0;
        return;
      }
      L[u] = max(L[u], L[v] - 1);
      R[u] = min(R[u], R[v] + 1);
    }
  }
  if (L[u] > R[u])
    ans = 0;
}

void make(int v = 0, int p = -1) {
  if (v == 0 && id[v] == -1)
    id[v] = L[v];
  for (int i = 0; i < from[v].size(); i++) {
    int to = from[v][i];
    if (to == p)
      continue;
    if (id[to] == -1) {
      if (L[to] <= id[v] - 1 && id[v] - 1 <= R[to])
        id[to] = id[v] - 1;
      else
        id[to] = id[v] + 1;
    }
    make(to, v);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    from[a].push_back(b);
    from[b].push_back(a);
  }
  memset(id, -1, sizeof(id));
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int v, p;
    scanf("%d%d", &v, &p);
    v--, id[v] = p;
  }
  ans = 1;
  solve();
  if (!ans) {
    puts("No");
    return 0;
  }
  make();
  puts("Yes");
  for (int i = 0; i < n; i++) {
    printf("%d\n", id[i]);
  }
  return 0;
}