Problem p03949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int inf = 1e9;
inline int read() {
  int p = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    p = p * 10 + (ch - '0');
    ch = getchar();
  }
  return p * f;
}
struct node {
  int x, y, next;
} edge[N];
int len, first[N];
void ins(int x, int y) {
  len++;
  edge[len].x = x;
  edge[len].y = y;
  edge[len].next = first[x];
  first[x] = len;
}
int n, kk;
int l[N], r[N];
int op[N], val[N];
bool bkk = 1;

void dfs(int x, int f) {
  if (val[x] != -1)
    l[x] = r[x] = val[x], op[x] = val[x] & 1;
  else
    l[x] = -inf, r[x] = inf, op[x] = -1;
  for (int k = first[x]; k != -1; k = edge[k].next) {
    int y = edge[k].y;
    if (y == f)
      continue;
    dfs(y, x);
    if ((op[x] == -1) || ((op[x] ^ 1) == op[y]) || (op[y] == -1)) {
      l[x] = max(l[x], l[y] - 1);
      r[x] = min(r[x], r[y] + 1);
      if (~op[y])
        op[x] = op[y] ^ 1;
    } else {
      bkk = 0;
      return;
    }
  }
}

void dfs2(int x, int fa) {
  if (val[x] == -1) {
    if (fa == 0) {
      for (int i = l[x]; i <= r[x]; i++)
        if ((i & 1) == op[x]) {
          val[x] = i;
          break;
        }
    } else {
      if (l[x] <= val[fa] - 1 && val[fa] - 1 <= r[x])
        val[x] = val[fa] - 1;
      else
        val[x] = val[fa] + 1;
    }
  }
  for (int k = first[x]; k != -1; k = edge[k].next) {
    int y = edge[k].y;
    if (y == fa)
      continue;
    dfs2(y, x);
  }
}

int main() {
  n = read();
  len = 0;
  memset(first, -1, sizeof(first));
  for (int i = 1; i < n; i++) {
    int x, y;
    x = read();
    y = read();
    ins(x, y);
    ins(y, x);
  }
  kk = read();
  for (int i = 1; i <= n; i++)
    val[i] = -1;
  for (int i = 1; i <= kk; i++) {
    int x = read(), v = read();
    val[x] = v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (l[i] > r[i]) {
      bkk = 0;
      break;
    }
    if ((op[i] != -1) && (l[i] == r[i] && (l[i] & 1) != op[i])) {
      bkk = 0;
      break;
    }
  }
  if (!bkk)
    printf("No\n");
  else {
    printf("Yes\n");
    dfs2(1, 0);
    for (int i = 1; i <= n; i++)
      printf("%d\n", val[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int inf = 1e6;
inline int read() {
  int p = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    p = p * 10 + (ch - '0');
    ch = getchar();
  }
  return p * f;
}
struct node {
  int x, y, next;
} edge[N];
int len, first[N];
void ins(int x, int y) {
  len++;
  edge[len].x = x;
  edge[len].y = y;
  edge[len].next = first[x];
  first[x] = len;
}
int n, kk;
int l[N], r[N];
int op[N], val[N];
bool bkk = 1;

void dfs(int x, int f) {
  if (val[x] != -1)
    l[x] = r[x] = val[x], op[x] = val[x] & 1;
  else
    l[x] = -inf, r[x] = inf, op[x] = -1;
  for (int k = first[x]; k != -1; k = edge[k].next) {
    int y = edge[k].y;
    if (y == f)
      continue;
    dfs(y, x);
    if ((op[x] == -1) || ((op[x] ^ 1) == op[y]) || (op[y] == -1)) {
      l[x] = max(l[x], l[y] - 1);
      r[x] = min(r[x], r[y] + 1);
      if (~op[y])
        op[x] = op[y] ^ 1;
    } else {
      bkk = 0;
      return;
    }
  }
}

void dfs2(int x, int fa) {
  if (val[x] == -1) {
    if (fa == 0) {
      for (int i = l[x]; i <= r[x]; i++)
        if ((i & 1) == op[x]) {
          val[x] = i;
          break;
        }
    } else {
      if (l[x] <= val[fa] - 1 && val[fa] - 1 <= r[x])
        val[x] = val[fa] - 1;
      else
        val[x] = val[fa] + 1;
    }
  }
  for (int k = first[x]; k != -1; k = edge[k].next) {
    int y = edge[k].y;
    if (y == fa)
      continue;
    dfs2(y, x);
  }
}

int main() {
  n = read();
  len = 0;
  memset(first, -1, sizeof(first));
  for (int i = 1; i < n; i++) {
    int x, y;
    x = read();
    y = read();
    ins(x, y);
    ins(y, x);
  }
  kk = read();
  for (int i = 1; i <= n; i++)
    val[i] = -1;
  for (int i = 1; i <= kk; i++) {
    int x = read(), v = read();
    val[x] = v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (l[i] > r[i]) {
      bkk = 0;
      break;
    }
    if ((op[i] != -1) && (l[i] == r[i] && (l[i] & 1) != op[i])) {
      bkk = 0;
      break;
    }
  }
  if (!bkk)
    printf("No\n");
  else {
    printf("Yes\n");
    dfs2(1, 0);
    for (int i = 1; i <= n; i++)
      printf("%d\n", val[i]);
  }
  return 0;
}