Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;

typedef vector<vector<ll>> vvll;

typedef pair<ll, ll> Pair;
#define mp(x, y) make_pair(x, y)

typedef queue<int> qi;
typedef queue<string> qs;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)

#define all(v) v.begin(), v.end()
// sort( all(v) )　などと使える

// nが素数かをO(√n)で判断
bool is_prime(int n) {
  bool res = true;

  if (n == 1)
    return false;
  else if (n == 2)
    return true;

  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        res = false;
        break;
      }
    }
  }

  return res;
}

int perm(int n, int r) {
  if (r == 1)
    return n;
  return n * perm(n - 1, r - 1);
}

int fact(int n) {
  if (n == 1)
    return 1;
  return fact(n - 1) * n;
}

int comb(int n, int r) {
  int a = min(n, r), b = max(n, r);
  return perm(a, b) / fact(a);
}

/*
ll pow_mod(ll a, ll n, ll mod) {
    ll res = 1;

    ll base = a;
    while (n) {
        if (n & 1) res = res * base % mod;
        base = base * base % mod;
        n >>= 1;
    }
    return res;
}
*/

// aの逆元　（a^(-1)）を求める
ll inv_mod(ll a, ll mod) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

/*
ll fact_mod(ll n, ll mod) {
    if (n == 1) return 1;
    return n * fact_mod(n - 1, mod) * mod;
}
*/

// 階乗 (mod とりバージョン)
ll fact_mod(ll n, ll mod) {
  ll f = 1;
  for (int i = 2; i <= n; i++)
    f = f * (i % mod) % mod;
  return f;
}

ll pow_mod(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % mod; // ビット演算(最下位ビットが1のとき)
    x = (x * x) % mod;
    n >>= 1; // 右シフト(n = n >> 1)
  }
  return res;
}

// 組み合わせ nCr を求める (modあり)
ll comb_mod(ll n, ll r, ll mod) {
  if (r > n - r)
    r = n - r;
  if (r == 0)
    return 1;
  ll a = 1;
  for (int i = 0; i < r; i++)
    a = a * ((n - i) % mod) % mod;
  ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
  return (a % mod) * (b % mod) % mod;
}

int N, Q;
vvi G;
vll c;

vector<bool> seen;

void dfs(int v) {
  seen[v] = true;

  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    else {
      c[nv] += c[v];
      dfs(nv);
    }
  }
}

int main() {
  cin >> N >> Q;
  G.resize(N);
  c.resize(N, 0);
  seen.assign(N, false);

  rep(i, N) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  rep(i, Q) {
    int x;
    ll p;
    cin >> x >> p;
    x--;
    c[x] += p;
  }

  // DFS
  dfs(0);

  // output
  rep(i, N) cout << c[i] << " ";
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;

typedef vector<vector<ll>> vvll;

typedef pair<ll, ll> Pair;
#define mp(x, y) make_pair(x, y)

typedef queue<int> qi;
typedef queue<string> qs;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)

#define all(v) v.begin(), v.end()
// sort( all(v) )　などと使える

// nが素数かをO(√n)で判断
bool is_prime(int n) {
  bool res = true;

  if (n == 1)
    return false;
  else if (n == 2)
    return true;

  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        res = false;
        break;
      }
    }
  }

  return res;
}

int perm(int n, int r) {
  if (r == 1)
    return n;
  return n * perm(n - 1, r - 1);
}

int fact(int n) {
  if (n == 1)
    return 1;
  return fact(n - 1) * n;
}

int comb(int n, int r) {
  int a = min(n, r), b = max(n, r);
  return perm(a, b) / fact(a);
}

/*
ll pow_mod(ll a, ll n, ll mod) {
    ll res = 1;

    ll base = a;
    while (n) {
        if (n & 1) res = res * base % mod;
        base = base * base % mod;
        n >>= 1;
    }
    return res;
}
*/

// aの逆元　（a^(-1)）を求める
ll inv_mod(ll a, ll mod) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

/*
ll fact_mod(ll n, ll mod) {
    if (n == 1) return 1;
    return n * fact_mod(n - 1, mod) * mod;
}
*/

// 階乗 (mod とりバージョン)
ll fact_mod(ll n, ll mod) {
  ll f = 1;
  for (int i = 2; i <= n; i++)
    f = f * (i % mod) % mod;
  return f;
}

ll pow_mod(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % mod; // ビット演算(最下位ビットが1のとき)
    x = (x * x) % mod;
    n >>= 1; // 右シフト(n = n >> 1)
  }
  return res;
}

// 組み合わせ nCr を求める (modあり)
ll comb_mod(ll n, ll r, ll mod) {
  if (r > n - r)
    r = n - r;
  if (r == 0)
    return 1;
  ll a = 1;
  for (int i = 0; i < r; i++)
    a = a * ((n - i) % mod) % mod;
  ll b = pow_mod(fact_mod(r, mod), mod - 2, mod);
  return (a % mod) * (b % mod) % mod;
}

int N, Q;
vvi G;
vll c;

vector<bool> seen;

void dfs(int v) {
  seen[v] = true;

  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    else {
      c[nv] += c[v];
      dfs(nv);
    }
  }
}

int main() {
  cin >> N >> Q;
  G.resize(N);
  c.resize(N, 0);
  seen.assign(N, false);

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  rep(i, Q) {
    int x;
    ll p;
    cin >> x >> p;
    x--;
    c[x] += p;
  }

  // DFS
  dfs(0);

  // output
  rep(i, N) cout << c[i] << " ";
}