Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
#define ll long long
#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)

#define pb emplace_back
#define mp make_pair

using namespace std;

const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;

#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif

typedef pair<ll, ll> P;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll R, C, N, M, K, Q;

vector<ll> v[200000];
vector<ll> cost(100000, 0);
vector<ll> res(200000, -1);

void dfs(ll node, ll lastCost = 0) {
  PRINT(node)
  PRINT(res[node])
  if (res[node] != -1) {
    PRINT("FINISH")
    return;
  }
  lastCost += cost[node];
  res[node] = lastCost;
  irep(itr, v[node]) {
    PRINT(*itr)
    dfs(*itr, lastCost);
  }
  return;
}

signed main() {
  // cout << fixed << setprecision(10);
  cin >> N >> Q;

  rep(i, N - 1) {
    ll from, to;
    cin >> from >> to;
    from--;
    to--;
    v[from].pb(to);
    v[to].pb(from);
  }
  rep(i, Q) {
    ll node, val;
    cin >> node >> val;
    cost[--node] += val;
  }

  dfs(0);

  rep(i, N) { cout << res[i] << endl; }

  // cout << max << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
#define ll long long
#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)

#define pb emplace_back
#define mp make_pair

using namespace std;

const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;

#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif

typedef pair<ll, ll> P;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll R, C, N, M, K, Q;

vector<ll> v[300000];
vector<ll> cost(300000, 0);
vector<ll> res(300000, -1);

void dfs(ll node, ll lastCost = 0) {
  PRINT(node)
  PRINT(res[node])
  if (res[node] != -1) {
    PRINT("FINISH")
    return;
  }
  lastCost += cost[node];
  res[node] = lastCost;
  irep(itr, v[node]) {
    PRINT(*itr)
    dfs(*itr, lastCost);
  }
  return;
}

signed main() {
  // cout << fixed << setprecision(10);
  cin >> N >> Q;

  rep(i, N - 1) {
    ll from, to;
    cin >> from >> to;
    from--;
    to--;
    v[from].pb(to);
    v[to].pb(from);
  }
  rep(i, Q) {
    ll node, val;
    cin >> node >> val;
    cost[--node] += val;
  }

  dfs(0);

  rep(i, N) { cout << res[i] << endl; }

  // cout << max << endl;
}