Problem p03949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define N 100000 + 5
#define inf 123456789
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define Next(i, u) for (int i = h[u]; i; i = e[i].next)
struct edge {
  int v, next;
} e[N << 1];
bool flag, book[N];
int n, m, u, v, tot, l[N], r[N], h[N], col[N], val[N];
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
void add(int u, int v) { e[++tot].v = v, e[tot].next = h[u], h[u] = tot; }
void dfs(int u, int fa) {
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    if (book[v]) {
      if (col[v] != (col[u] ^ 1))
        flag = true;
    } else
      col[v] = col[u] ^ 1, dfs(v, u);
  }
}
void dfs1(int u, int fa) {
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    dfs1(v, u), l[u] = max(l[v] - 1, l[u]), r[u] = min(r[v] + 1, r[u]);
  }
}
void dfs2(int u, int fa) {
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    l[v] = max(l[u] - 1, l[v]), r[v] = min(r[u] + 1, r[v]), dfs2(v, u);
  }
}
void dfs3(int u, int fa) {
  if (!fa) {
    if (r[u] < l[u])
      flag = true;
    if (l[u] == r[u] && ((l[u] & 1) != col[u]))
      flag = true;
    if ((l[u] & 1) != col[u])
      val[u] = l[u] + 1;
    else
      val[u] = l[u];
  }
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    if (r[v] < l[v])
      flag = true;
    if (val[u] + 1 >= l[v] && val[u] + 1 <= r[v])
      val[v] = val[u] + 1;
    else if (val[u] - 1 >= l[v] && val[u] - 1 <= r[v])
      val[v] = val[u] - 1;
    else
      flag = true;
    dfs3(v, u);
  }
}
int main() {
  n = read();
  rep(i, 1, n) l[i] = -inf, r[i] = inf;
  rep(i, 1, n - 1) u = read(), v = read(), add(u, v), add(v, u);
  m = read();
  rep(i, 1, m) u = read(), v = read(), l[u] = r[u] = v, book[u] = true,
               col[u] = (v & 1);
  rep(i, 1, n) if (l[i] == r[i]) dfs(i, 0);
  dfs1(1, 0), dfs2(1, 0), dfs3(1, 0);
  if (flag)
    puts("No");
  else {
    puts("Yes");
    rep(i, 1, n) printf("%d\n", val[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100000 + 5
#define inf 123456789
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define Next(i, u) for (int i = h[u]; i; i = e[i].next)
struct edge {
  int v, next;
} e[N << 1];
bool flag, book[N];
int n, m, u, v, tot, l[N], r[N], h[N], col[N], val[N];
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
void add(int u, int v) { e[++tot].v = v, e[tot].next = h[u], h[u] = tot; }
void dfs(int u, int fa) {
  book[u] = true;
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    if (book[v]) {
      if (col[v] != (col[u] ^ 1))
        flag = true;
    } else
      col[v] = col[u] ^ 1, dfs(v, u);
  }
}
void dfs1(int u, int fa) {
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    dfs1(v, u), l[u] = max(l[v] - 1, l[u]), r[u] = min(r[v] + 1, r[u]);
  }
}
void dfs2(int u, int fa) {
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    l[v] = max(l[u] - 1, l[v]), r[v] = min(r[u] + 1, r[v]), dfs2(v, u);
  }
}
void dfs3(int u, int fa) {
  if (!fa) {
    if (r[u] < l[u])
      flag = true;
    if (l[u] == r[u] && ((l[u] & 1) != col[u]))
      flag = true;
    if ((l[u] & 1) != col[u])
      val[u] = l[u] + 1;
    else
      val[u] = l[u];
  }
  Next(i, u) {
    int v = e[i].v;
    if (v == fa)
      continue;
    if (r[v] < l[v])
      flag = true;
    if (val[u] + 1 >= l[v] && val[u] + 1 <= r[v])
      val[v] = val[u] + 1;
    else if (val[u] - 1 >= l[v] && val[u] - 1 <= r[v])
      val[v] = val[u] - 1;
    else
      flag = true;
    dfs3(v, u);
  }
}
int main() {
  n = read();
  rep(i, 1, n) l[i] = -inf, r[i] = inf;
  rep(i, 1, n - 1) u = read(), v = read(), add(u, v), add(v, u);
  m = read();
  rep(i, 1, m) u = read(), v = read(), l[u] = r[u] = v, book[u] = true,
               col[u] = (v & 1);
  rep(i, 1, n) if (l[i] == r[i]) dfs(i, 0);
  dfs1(1, 0), dfs2(1, 0), dfs3(1, 0);
  if (flag)
    puts("No");
  else {
    puts("Yes");
    rep(i, 1, n) printf("%d\n", val[i]);
  }
  return 0;
}