Problem p03949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <limits.h>
#include <map>
#include <math.h>
#include <nmmintrin.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

class Graph {
public:
  int vn;
  int sumcost = 0;
  vector<vector<pii>> g;

  Graph(int n) {
    vn = n;
    g.resize(n);
  }
  virtual void con(int a, int b, int w) = 0;
  int getWeight(int f, int t) {
    auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
    if (itr != g[f].end())
      return itr->second;
    return INT_MIN;
  }
  int Costsum() { return sumcost; }
  void scan(int edcount, bool oindexed, bool w) {
    REP(i, edcount) {
      int a, b, c = 1;
      scanf("%d %d", &a, &b);
      if (w)
        scanf("%d", &c);
      con(a - oindexed, b - oindexed, c);
    }
  }
};
class BiDGraph : public Graph { // 無向
public:
  BiDGraph(int n) : Graph(n) {}

  void con(int a, int b, int w = 1) {
    g[a].push_back({b, w});
    g[b].push_back({a, w});
    sumcost++;
  }
};

signed main() {
  int n;
  cin >> n;
  BiDGraph g(n);
  REP(i, n - 1) {
    int a, b;
    scanf("%d %d", &a, &b);
    g.con(a - 1, b - 1);
  }

  vector<int> depth(n), par(n);
  function<void(int, int)> dfs = [&](int c, int p) {
    par[c] = p;
    depth[c] = p == -1 ? 0 : depth[p] + 1;
    for (auto itr : g.g[c])
      if (itr.first != p)
        dfs(itr.first, c);
  };
  dfs(0, -1);

  vector<pii> num(n, {INT_MAX, INT_MAX});
  vector<int> ev(n, -1);

  int m;
  cin >> m;
  vector<int> in(n), ot(n);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  REP(i, m) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a;
    num[a] = {b, b};
    pq.push(MP(depth[a], a));
    ev[a] = b % 2;
    in[a]++;
  }

  while (pq.size()) {
    auto cur = pq.top();
    pq.pop();
    ot[cur.second]++;

    if (cur.second == 0)
      continue;

    if (num[par[cur.second]] == MP(INT_MAX, INT_MAX)) {
      num[par[cur.second]] =
          MP(num[cur.second].first - 1, num[cur.second].second + 1);
    } else {
      int cl = num[cur.second].first - 1, cr = num[cur.second].second + 1,
          l = num[par[cur.second]].first, r = num[par[cur.second]].second;

      num[par[cur.second]] = {max(cl, l), min(cr, r)};
      if (num[par[cur.second]].first > num[par[cur.second]].second) {
        printf("No\n");
        return 0;
      }
    }
    if (ev[par[cur.second]] != -1) {
      if (ev[par[cur.second]] != (1 - ev[cur.second])) {
        printf("No\n");
        return 0;
      }
    }
    ev[par[cur.second]] = 1 - ev[cur.second];
    if (par[cur.second] != 0 && in[cur.second] == ot[cur.second]) {
      pq.push(MP(depth[par[cur.second]], par[cur.second]));
      in[par[cur.second]]++;
    }
  }

  printf("Yes\n");
  vector<int> out(n);
  function<void(int, int)> ans = [&](int c, int p) {
    if (p == -1)
      out[c] = num[c].first;
    else
      out[c] = num[c].first <= (out[p] - 1) ? out[p] - 1 : out[p] + 1;

    for (auto itr : g.g[c]) {
      if (itr.first != p)
        ans(itr.first, c);
    }
  };
  ans(0, -1);
  for (auto itr : out)
    printf("%d\n", itr);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <limits.h>
#include <map>
#include <math.h>
#include <nmmintrin.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

class Graph {
public:
  int vn;
  int sumcost = 0;
  vector<vector<pii>> g;

  Graph(int n) {
    vn = n;
    g.resize(n);
  }
  virtual void con(int a, int b, int w) = 0;
  int getWeight(int f, int t) {
    auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
    if (itr != g[f].end())
      return itr->second;
    return INT_MIN;
  }
  int Costsum() { return sumcost; }
  void scan(int edcount, bool oindexed, bool w) {
    REP(i, edcount) {
      int a, b, c = 1;
      scanf("%d %d", &a, &b);
      if (w)
        scanf("%d", &c);
      con(a - oindexed, b - oindexed, c);
    }
  }
};
class BiDGraph : public Graph { // 無向
public:
  BiDGraph(int n) : Graph(n) {}

  void con(int a, int b, int w = 1) {
    g[a].push_back({b, w});
    g[b].push_back({a, w});
    sumcost++;
  }
};

signed main() {
  int n;
  cin >> n;
  BiDGraph g(n);
  REP(i, n - 1) {
    int a, b;
    scanf("%d %d", &a, &b);
    g.con(a - 1, b - 1);
  }

  vector<int> depth(n), par(n);
  function<void(int, int)> dfs = [&](int c, int p) {
    par[c] = p;
    depth[c] = p == -1 ? 0 : depth[p] + 1;
    for (auto itr : g.g[c])
      if (itr.first != p)
        dfs(itr.first, c);
  };
  dfs(0, -1);

  vector<pii> num(n, {INT_MAX, INT_MAX});
  vector<int> ev(n, -1);

  int m;
  cin >> m;
  vector<int> in(n), ot(n);
  priority_queue<pii> pq;
  REP(i, m) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a;
    num[a] = {b, b};
    pq.push(MP(depth[a], a));
    ev[a] = b % 2;
    in[a]++;
  }

  while (pq.size()) {
    auto cur = pq.top();
    pq.pop();
    ot[cur.second]++;

    if (cur.second == 0)
      continue;

    if (num[par[cur.second]] == MP(INT_MAX, INT_MAX)) {
      num[par[cur.second]] =
          MP(num[cur.second].first - 1, num[cur.second].second + 1);
    } else {
      int cl = num[cur.second].first - 1, cr = num[cur.second].second + 1,
          l = num[par[cur.second]].first, r = num[par[cur.second]].second;

      num[par[cur.second]] = {max(cl, l), min(cr, r)};
      if (num[par[cur.second]].first > num[par[cur.second]].second) {
        printf("No\n");
        return 0;
      }
    }
    if (ev[par[cur.second]] != -1) {
      if (ev[par[cur.second]] != (1 - ev[cur.second])) {
        printf("No\n");
        return 0;
      }
    }
    ev[par[cur.second]] = 1 - ev[cur.second];
    if (par[cur.second] != 0 && in[cur.second] == ot[cur.second]) {
      pq.push(MP(depth[par[cur.second]], par[cur.second]));
      in[par[cur.second]]++;
    }
  }

  printf("Yes\n");
  vector<int> out(n);
  function<void(int, int)> ans = [&](int c, int p) {
    if (p == -1)
      out[c] = num[c].first;
    else
      out[c] = num[c].first <= (out[p] - 1) ? out[p] - 1 : out[p] + 1;

    for (auto itr : g.g[c]) {
      if (itr.first != p)
        ans(itr.first, c);
    }
  };
  ans(0, -1);
  for (auto itr : out)
    printf("%d\n", itr);

  return 0;
}