Problem p03949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
pair<int, int> r[100005];
int dep[100005], sz[100005];
bool del[100005];
void get(int node, int p) {
  dep[node] = dep[p] + 1;
  r[node] = {-1e9, 1e9};
  for (int u : v[node]) {
    if (u != p)
      get(u, node);
  }
}
int pre(int node, int p) {
  sz[node] = 1;
  for (int u : v[node]) {
    if (u != p && !del[u])
      sz[node] += sz[u];
  }
  return sz[node];
}
int find(int node, int p, int nn) {
  for (int u : v[node]) {
    if (u != p && !del[u] && sz[u] > nn / 2)
      return find(u, node, nn);
  }
  return node;
}
void update1(int node, int p) {
  for (int u : v[node]) {
    if (u != p && !del[u]) {
      update1(u, node);
      r[node].first = max(r[node].first, r[u].first - 1);
      r[node].second = min(r[node].second, r[u].second + 1);
    }
  }
  if (r[node].first > r[node].second) {
    printf("No");
    exit(0);
  }
}
void update2(int node, int p) {
  if (p) {
    r[node].first = max(r[node].first, r[p].first - 1);
    r[node].second = min(r[node].second, r[p].second + 1);
  }
  for (int u : v[node]) {
    if (u != p && !del[u])
      update2(u, node);
  }
}
void dfs(int node) {
  int c = find(node, 0, pre(node, 0));
  update1(c, 0);
  r[c].second = r[c].first;
  update2(c, 0);
  del[c] = 1;
  for (int u : v[c]) {
    if (!del[u])
      dfs(u);
  }
}
int main() {
  int n, q;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  get(1, 0);
  scanf("%d", &q);
  set<int> s;
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    r[a] = {b, b};
    s.insert((dep[a] + b) % 2);
  }
  if (s.size() != 1) {
    printf("No");
    return 0;
  }
  dfs(1);
  printf("Yes");
  for (int i = 1; i <= n; i++)
    printf("\n%d", r[i].first);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
pair<int, int> r[100005];
int dep[100005], sz[100005];
bool del[100005];
void get(int node, int p) {
  dep[node] = dep[p] + 1;
  r[node] = {-1e9, 1e9};
  for (int u : v[node]) {
    if (u != p)
      get(u, node);
  }
}
int pre(int node, int p) {
  sz[node] = 1;
  for (int u : v[node]) {
    if (u != p && !del[u])
      sz[node] += pre(u, node);
  }
  return sz[node];
}
int find(int node, int p, int nn) {
  for (int u : v[node]) {
    if (u != p && !del[u] && sz[u] > nn / 2)
      return find(u, node, nn);
  }
  return node;
}
void update1(int node, int p) {
  for (int u : v[node]) {
    if (u != p && !del[u]) {
      update1(u, node);
      r[node].first = max(r[node].first, r[u].first - 1);
      r[node].second = min(r[node].second, r[u].second + 1);
    }
  }
  if (r[node].first > r[node].second) {
    printf("No");
    exit(0);
  }
}
void update2(int node, int p) {
  if (p) {
    r[node].first = max(r[node].first, r[p].first - 1);
    r[node].second = min(r[node].second, r[p].second + 1);
  }
  for (int u : v[node]) {
    if (u != p && !del[u])
      update2(u, node);
  }
}
void dfs(int node) {
  int c = find(node, 0, pre(node, 0));
  update1(c, 0);
  r[c].second = r[c].first;
  update2(c, 0);
  del[c] = 1;
  for (int u : v[c]) {
    if (!del[u])
      dfs(u);
  }
}
int main() {
  int n, q;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  get(1, 0);
  scanf("%d", &q);
  set<int> s;
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    r[a] = {b, b};
    s.insert((dep[a] + b) % 2);
  }
  if (s.size() != 1) {
    printf("No");
    return 0;
  }
  dfs(1);
  printf("Yes");
  for (int i = 1; i <= n; i++)
    printf("\n%d", r[i].first);
}