Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<int, int> P;
struct Edge {
  ll from, to, cost;
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}

ll N, Q;
llvec v[20001];
ll c[20001];

ll dfs(ll from, ll to) {
  c[to] += c[from];
  rep(i, v[to].size()) {
    if (v[to][i] != from) {
      dfs(to, v[to][i]);
    }
  }
}

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  llvec count(N + 1, 0);

  rep(i, Q) {
    ll q, a;
    cin >> q >> a;
    c[q] += a;
  }

  dfs(0, 1);

  rep(i, N) { cout << c[i + 1] << endl; }

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<int, int> P;
struct Edge {
  ll from, to, cost;
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}

ll N, Q;
llvec v[200001];
ll c[200001];

ll dfs(ll from, ll to) {
  c[to] += c[from];
  rep(i, v[to].size()) {
    if (v[to][i] != from) {
      dfs(to, v[to][i]);
    }
  }
}

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  llvec count(N + 1, 0);

  rep(i, Q) {
    ll q, a;
    cin >> q >> a;
    c[q] += a;
  }

  dfs(0, 1);

  rep(i, N) { cout << c[i + 1] << endl; }

  return 0;
}