prithivMLmods/Coder-Stat huggingface dataset:
Problem p02936 (C++)

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

using namespace std;

int n, q;
vector<int> adjList[112123];
long long v[112123];
bool seen[112123];

void dfs(int x) {
  seen[x] = true;
  for (int y : adjList[x]) {
    if (seen[y])
      continue;
    v[y] += v[x];
    dfs(y);
  }
}

int main() {
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adjList[x].push_back(y);
    adjList[y].push_back(x);
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    v[x] += y;
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << v[i] << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, q;
vector<int> adjList[212123];
long long v[212123];
bool seen[212123];

void dfs(int x) {
  seen[x] = true;
  for (int y : adjList[x]) {
    if (seen[y])
      continue;
    v[y] += v[x];
    dfs(y);
  }
}

int main() {
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adjList[x].push_back(y);
    adjList[y].push_back(x);
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    v[x] += y;
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << v[i] << '\n';
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// typedef long long unsigned int ll;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define MAX_N 100000

int n, q;
vector<int> a[MAX_N];
int temp_a, temp_b;
int c[MAX_N];

void solver(int i, int parent) {
  for (int j : a[i]) {
    if (j != parent) {
      c[j] += c[i];
      solver(j, i);
    }
  }
  return;
}

int main(void) {
  int i, p, x;
  cin >> n >> q;
  REP(i, n - 1) {
    cin >> temp_a >> temp_b;
    temp_a--;
    temp_b--;
    a[temp_a].push_back(temp_b);
    a[temp_b].push_back(temp_a);
  }
  REP(i, n) { c[i] = 0; }
  REP(i, q) {
    cin >> p >> x;
    c[p - 1] += x;
  }

  solver(0, -1);

  REP(i, n) { cout << c[i] << " "; }
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// typedef long long unsigned int ll;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define MAX_N 200000

int n, q;
vector<int> a[MAX_N];
int temp_a, temp_b;
int c[MAX_N];

void solver(int i, int parent) {
  for (int j : a[i]) {
    if (j != parent) {
      c[j] += c[i];
      solver(j, i);
    }
  }
  return;
}

int main(void) {
  int i, p, x;
  cin >> n >> q;
  REP(i, n - 1) {
    cin >> temp_a >> temp_b;
    temp_a--;
    temp_b--;
    a[temp_a].push_back(temp_b);
    a[temp_b].push_back(temp_a);
  }
  REP(i, n) { c[i] = 0; }
  REP(i, q) {
    cin >> p >> x;
    c[p - 1] += x;
  }

  solver(0, -1);

  REP(i, n) { cout << c[i] << " "; }
  cout << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/*
 * atcoder/abc138_2nd/d.cpp
 */

// C++ 14
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring> // memset
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <class T> ostream &operator<<(ostream &out, vector<T> const &v) {
  for (auto &&a : v)
    out << a << " ";
  out << endl;
  return out;
}
template <class T> void dump(T &a) { cout << a << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX = 100001;
int N, Q;
vector<int> G[MAX];
vector<int> A(MAX, 0);
vector<int> B(MAX, 0);
void dfs(int n = 0, int prev = -1, ll acc = 0) {
  acc += A[n];
  B[n] = acc;

  for (auto &&u : G[n]) {
    if (u == prev)
      continue;
    dfs(u, n, acc);
  }
}
void solve() {
  cin >> N >> Q;
  int a, b, p, x;
  loop(n, 0, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  loop(n, 0, Q) {
    cin >> p >> x;
    p--;
    A[p] += x;
  }
  dfs();
  loop(n, 0, N) cout << B[n] << " ";
  cout << endl;
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}


Fixed submission:
/*
 * atcoder/abc138_2nd/d.cpp
 */

// C++ 14
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring> // memset
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <class T> ostream &operator<<(ostream &out, vector<T> const &v) {
  for (auto &&a : v)
    out << a << " ";
  out << endl;
  return out;
}
template <class T> void dump(T &a) { cout << a << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX = 200001;
int N, Q;
vector<int> G[MAX];
vector<int> A(MAX, 0);
vector<int> B(MAX, 0);
void dfs(int n = 0, int prev = -1, ll acc = 0) {
  acc += A[n];
  B[n] = acc;

  for (auto &&u : G[n]) {
    if (u == prev)
      continue;
    dfs(u, n, acc);
  }
}
void solve() {
  cin >> N >> Q;
  int a, b, p, x;
  loop(n, 0, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  loop(n, 0, Q) {
    cin >> p >> x;
    p--;
    A[p] += x;
  }
  dfs();
  loop(n, 0, N) cout << B[n] << " ";
  cout << endl;
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const long long INF = 1LL << 60;
const int MOD = 1000000000 + 7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// cout << fixed << setprecision(10) << ans << endl; 有効桁数指定
// *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// ~ は、-1の時だけfalse

int n, q;
vector<int> tree[20005];
int cnt[20005];
ll sum[20005];
int seen[20005];

void dfs(int n) {
  for (int i = 0; i < int(tree[n].size()); i++) {
    if (!seen[tree[n][i]]) {
      seen[tree[n][i]] = 1;
      sum[tree[n][i]] = cnt[tree[n][i]] + sum[n];
      dfs(tree[n][i]);
    }
  }
}

int main() {
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    cnt[p - 1] += x;
  }
  seen[0] = 1;
  sum[0] = cnt[0];
  dfs(0);
  rep(i, n) {
    cout << sum[i] << " ";
    if (i == n - 1)
      cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<ll, ll, ll> T;
const long long INF = 1LL << 60;
const int MOD = 1000000000 + 7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
};
// cout << fixed << setprecision(10) << ans << endl; 有効桁数指定
// *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// ~ は、-1の時だけfalse

int n, q;
vector<int> tree[200005];
int cnt[200005];
ll sum[200005];
int seen[200005];

void dfs(int n) {
  for (int i = 0; i < int(tree[n].size()); i++) {
    if (!seen[tree[n][i]]) {
      seen[tree[n][i]] = 1;
      sum[tree[n][i]] = cnt[tree[n][i]] + sum[n];
      dfs(tree[n][i]);
    }
  }
}

int main() {
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    cnt[p - 1] += x;
  }
  seen[0] = 1;
  sum[0] = cnt[0];
  dfs(0);
  rep(i, n) {
    cout << sum[i] << " ";
    if (i == n - 1)
      cout << endl;
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces1038D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ll x, ll y, ll a, ll b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (int k = 0; k < n; k++) {
    cout << a[k] << " ";
  }
  cerr << "\n";
}
ll n, q;
vector<ll> g[111111];
ll c[111111];
bool vis[111111];
void dfs(ll v, ll x) {
  if (vis[v])
    return;
  vis[v] = true;
  c[v] += x;
  for (ll e : g[v]) {
    dfs(e, c[v]);
  }
}

int main() {
  FASTIO;
  cin >> n >> q;
  ll x, y;
  for (int k = 0; k < n - 1; k++) {
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  for (int k = 0; k < q; k++) {
    cin >> x >> y;
    c[x] += y;
  }
  dfs(1, 0);
  for (int k = 1; k <= n; k++)
    cout << c[k] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces1038D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ll x, ll y, ll a, ll b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (int k = 0; k < n; k++) {
    cout << a[k] << " ";
  }
  cerr << "\n";
}
ll n, q;
vector<ll> g[222222];
ll c[222222];
bool vis[222222];
void dfs(int v, ll x) {
  if (vis[v])
    return;
  vis[v] = true;
  c[v] += x;
  for (ll e : g[v]) {
    dfs(e, c[v]);
  }
}

int main() {
  FASTIO;
  cin >> n >> q;
  ll x, y;
  for (int k = 0; k < n - 1; k++) {
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  for (int k = 0; k < q; k++) {
    cin >> x >> y;
    c[x] += y;
  }
  dfs(1, 0);
  for (int k = 1; k <= n; k++)
    cout << c[k] << " ";
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll a[maxn], b[maxn];
vector<int> v[maxn];
void f(int n) {

  for (int i = 0; i < v[n].size(); i++) {
    int k = v[n][i];
    a[k] += (b[n] + b[k]);
    b[k] += b[n];
    for (int i = 0; i < v[k].size(); i++) {
      int l = v[k][i];
      a[l] += (b[l] + b[k]);
      b[l] += b[k];
      f(l);
    }
  }
}
int main() {
  int n, q;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    int from, to;
    scanf("%d %d", &from, &to);
    v[from].push_back(to);
  }
  for (int i = 0; i < q; i++) {
    int w, l;
    scanf("%d %d", &w, &l);
    b[w] += l;
  }
  a[1] = b[1];
  f(1);
  printf("%lld", a[1]);
  for (int i = 2; i <= n; i++)
    printf(" %lld", a[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn], b[maxn];
vector<int> v[maxn];
void f(int n) {

  for (int i = 0; i < v[n].size(); i++) {
    int k = v[n][i];
    a[k] += (b[n] + b[k]);
    b[k] += b[n];
    for (int i = 0; i < v[k].size(); i++) {
      int l = v[k][i];
      a[l] += (b[l] + b[k]);
      b[l] += b[k];
      f(l);
    }
  }
}
int main() {
  int n, q;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    int from, to;
    scanf("%d %d", &from, &to);
    v[from].push_back(to);
  }
  for (int i = 0; i < q; i++) {
    int w, l;
    scanf("%d %d", &w, &l);
    b[w] += l;
  }
  a[1] = b[1];
  f(1);
  printf("%lld", a[1]);
  for (int i = 2; i <= n; i++)
    printf(" %lld", a[i]);
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll a[maxn];
ll ans[maxn];
int ru[maxn];
int tot = 0, head[maxn], ne[maxn], to[maxn];
vector<int> s[maxn];
void add(int x, int y) {
  tot++;
  to[tot] = y;
  ne[tot] = head[x];
  head[x] = tot;
}
void dfs(int x, int pre) {
  a[x] += a[pre];
  for (int i = head[x]; i; i = ne[i]) {
    if (to[i] != pre) {
      dfs(to[i], x);
    }
  }
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x] += y;
  }
  int pos = 1;
  dfs(pos, 0);
  for (int i = 1; i <= n; i++)
    printf("%lld%c", a[i], i == n ? '\n' : ' ');
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10;
ll a[maxn];
ll ans[maxn];
int ru[maxn];
int tot = 0, head[maxn], ne[maxn], to[maxn];
vector<int> s[maxn];
void add(int x, int y) {
  tot++;
  to[tot] = y;
  ne[tot] = head[x];
  head[x] = tot;
}
void dfs(int x, int pre) {
  a[x] += a[pre];
  for (int i = head[x]; i; i = ne[i]) {
    if (to[i] != pre) {
      dfs(to[i], x);
    }
  }
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x] += y;
  }
  int pos = 1;
  dfs(pos, 0);
  for (int i = 1; i <= n; i++)
    printf("%lld%c", a[i], i == n ? '\n' : ' ');
  return 0;
}
Problem p02936 (Python)

Buggy submission (status: Runtime Error):
def c(i, p, f, g, k, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, k, vis)


def main():
    import sys

    sys.setrecursionlimit(200000)
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[: 2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1) :])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, k, vis)
    ans = " ".join(map(str, k[1:]))
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def c(i, p, f, g, k, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, k, vis)


def main():
    import sys

    sys.setrecursionlimit(300000)
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[: 2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1) :])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, k, vis)
    ans = " ".join(map(str, k[1:]))
    print(ans)


if __name__ == "__main__":
    main()
Problem p02936 (Python)

Buggy submission (status: Runtime Error):
from collections import deque


def solve(string):
    n, q, *abpx = map(int, string.split())
    ab, px = abpx[: 2 * n - 2], abpx[2 * n - 2 :]
    check = [True] * (n + 1)
    t = {i: [] for i in range(n + 1)}
    c = [0] * (n + 1)
    d = deque([1])
    for a, b in zip(*[iter(ab)] * 2):
        t[a].append(b)
        t[b].append(a)
    for p, x in zip(*[iter(px)] * 2):
        c[p] += x
    while len(d) > 0:
        curr = d.pop()
        check[curr] = False
        next_ = [i for i in t[curr] if check[i]]
        for n_ in next_:
            c[n_] += c[curr]
        d.extend(next_)
    return " ".join([str(_c) for _c in c[1:]])


if __name__ == "__main__":
    n, m = map(int, input().split())
    print(solve("{} {}\n".format(n, m) + "\n".join([input() for _ in range(n + m)])))


Fixed submission:
from collections import deque


def solve(string):
    n, q, *abpx = map(int, string.split())
    ab, px = abpx[: 2 * n - 2], abpx[2 * n - 2 :]
    check = [True] * (n + 1)
    t = {i: [] for i in range(n + 1)}
    c = [0] * (n + 1)
    d = deque([1])
    for a, b in zip(*[iter(ab)] * 2):
        t[a].append(b)
        t[b].append(a)
    for p, x in zip(*[iter(px)] * 2):
        c[p] += x
    while len(d) > 0:
        curr = d.pop()
        check[curr] = False
        next_ = [i for i in t[curr] if check[i]]
        for n_ in next_:
            c[n_] += c[curr]
        d.extend(next_)
    return " ".join([str(_c) for _c in c[1:]])


if __name__ == "__main__":
    n, m = map(int, input().split())
    print(
        solve("{} {}\n".format(n, m) + "\n".join([input() for _ in range(n + m - 1)]))
    )
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef long long ll;
typedef pair<ll, ll> P;

const int MAX = 2 * 1e5 + 10;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int N, Q;
vector<int> G[MAX];
vector<int> ans(MAX);

void dfs(int n, int x, map<int, int> m) {
  ans[n] = x + m[n];
  for (auto i : G[n]) {
    dfs(i, ans[n], m);
  }
}

int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }
  map<int, int> m;
  rep(i, Q) {
    int p, x;
    cin >> p >> x;
    p--;
    m[p] += x;
  }

  dfs(0, 0, m);

  rep(i, N) cout << ans[i] << " ";
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

typedef long long ll;
typedef pair<ll, ll> P;

const int MAX = 2 * 1e5 + 10;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int N, Q;
vector<int> G[MAX];
vector<int> ans(MAX);

void dfs(int n, int x, map<int, int> &m) {
  ans[n] = x + m[n];
  for (auto i : G[n]) {
    dfs(i, ans[n], m);
  }
}

int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }
  map<int, int> m;
  rep(i, Q) {
    int p, x;
    cin >> p >> x;
    p--;
    m[p] += x;
  }

  dfs(0, 0, m);

  rep(i, N) cout << ans[i] << " ";
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
#define M 1000000007
int n, q, a, b, x, y;
vector<vector<int>> d(100010);
vector<int> t(100010, 0), v(100010);
int c = 0;
void dfs(int p) {
  c += t[p];
  v[p] = c;
  for (auto au : d[p]) {
    dfs(au);
  }
  c -= t[p];
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    d[a].push_back(b);
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    t[x] += y;
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << v[i] << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
#define M 1000000007
int n, q, a, b, x, y;
vector<vector<int>> d(200010);
vector<int> t(200010, 0), v(200010);
int c = 0;
void dfs(int p) {
  c += t[p];
  v[p] = c;
  for (auto au : d[p]) {
    dfs(au);
  }
  c -= t[p];
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    d[a].push_back(b);
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    t[x] += y;
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << v[i] << " ";
  }
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> t(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t[a].push_back(b);
  }

  vector<int> point(N);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    queue<int> que;
    que.push(p);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      point[u] += x;
      for (auto v : t[u]) {
        que.push(v);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << point[i];
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> t(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t[a].push_back(b);
  }

  vector<int> point(N);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    point[p] += x;
  }
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    int last = point[u];
    for (auto v : t[u]) {
      point[v] += last;
      que.push(v);
    }
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << point[i];
  }
  cout << endl;
}
Problem p02936 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

vector<int> to[20005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;

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

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

  dfs(0);
  rep(i, N) { cout << ans[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;

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

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

  dfs(0);
  rep(i, N) { cout << ans[i] << endl; }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int increment(vector<vector<int>> &c, int parent, int start, int x,
              vector<ll> steps, vector<ll> &scores) {
  x += steps.at(start);
  scores.at(start) = x;
  for (int node : c.at(start)) {
    if (node == parent) {
      continue;
    }
    increment(c, start, node, x, steps, scores);
  }
  return 0;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> c(N);
  vector<ll> scores(N);
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    c.at(a).push_back(b);
    c.at(b).push_back(a);
  }

  int p, x;
  vector<ll> steps(N);
  for (int i = 0; i < Q; i++) {
    cin >> p >> x;
    p--;
    steps.at(p) += x;
  }

  increment(c, -1, 0, 0, steps, scores);

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << scores.at(i);
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int increment(vector<vector<int>> &c, int parent, int start, int x,
              vector<ll> &steps, vector<ll> &scores) {
  x += steps.at(start);
  scores.at(start) = x;
  for (int node : c.at(start)) {
    if (node == parent) {
      continue;
    }
    increment(c, start, node, x, steps, scores);
  }
  return 0;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> c(N);
  vector<ll> scores(N);
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    c.at(a).push_back(b);
    c.at(b).push_back(a);
  }

  int p, x;
  vector<ll> steps(N);
  for (int i = 0; i < Q; i++) {
    cin >> p >> x;
    p--;
    steps.at(p) += x;
  }

  increment(c, -1, 0, 0, steps, scores);

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << scores.at(i);
  }
  cout << endl;

  return 0;
}
Problem p02936 (C++)

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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

struct node {
  vector<ll> child;
  ll counter = 0;
};

vector<node> tree(100010);

void dfs(ll crnt_node, ll parent) {
  tree[crnt_node].counter += tree[parent].counter;

  rep(i, tree[crnt_node].child.size()) {
    ll next_node = tree[crnt_node].child[i];
    if (next_node == parent)
      continue;
    dfs(next_node, crnt_node);
  }
}

int main() {
  ll n, q;
  cin >> n >> q;

  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;

    tree[a].child.push_back(b);
    tree[b].child.push_back(a);
  }

  rep(i, q) {
    ll p, x;
    cin >> p >> x;
    p--;

    tree[p].counter += x;
  }

  rep(i, tree[0].child.size()) {
    ll next_node = tree[0].child[i];
    dfs(next_node, 0);
  }

  rep(i, n) cout << tree[i].counter << " ";
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

struct node {
  vector<ll> child;
  ll counter = 0;
};

vector<node> tree(200020);

void dfs(ll crnt_node, ll parent) {
  tree[crnt_node].counter += tree[parent].counter;

  rep(i, tree[crnt_node].child.size()) {
    ll next_node = tree[crnt_node].child[i];
    if (next_node == parent)
      continue;
    dfs(next_node, crnt_node);
  }
}

int main() {
  ll n, q;
  cin >> n >> q;

  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;

    tree[a].child.push_back(b);
    tree[b].child.push_back(a);
  }

  rep(i, q) {
    ll p, x;
    cin >> p >> x;
    p--;

    tree[p].counter += x;
  }

  rep(i, tree[0].child.size()) {
    ll next_node = tree[0].child[i];
    dfs(next_node, 0);
  }

  rep(i, n) cout << tree[i].counter << " ";
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <cctype>
#include <cstdio>
#define getc                                                                   \
  (l == r && (r = (l = c) + fread(c, 1, 1 << 21, stdin), l == r) ? EOF : *l++)

char c[1 << 21], *l = c, *r = c;
inline int read() {
  int x = 0;
  char ch = getc;
  while (!isdigit(ch))
    ch = getc;
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getc;
  }
  return x;
}

const int N = 100010;
int n, q, cnt, last[N], s[N];
struct Edge {
  int y, pre;
} E[N << 1];
void dfs(int x, int fa, int sum) {
  s[x] += sum;
  for (int j = last[x]; j; j = E[j].pre)
    if (E[j].y != fa)
      dfs(E[j].y, x, s[x]);
}
int main() {
  n = read();
  q = read();
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read();
    E[++cnt] = (Edge){y, last[x]};
    last[x] = cnt;
    E[++cnt] = (Edge){x, last[y]};
    last[y] = cnt;
  }
  while (q--) {
    int p = read(), x = read();
    s[p] += x;
  }
  dfs(1, -1, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d ", s[i]);
  return 0;
}

Fixed submission:
#include <cctype>
#include <cstdio>
#define getc                                                                   \
  (l == r && (r = (l = c) + fread(c, 1, 1 << 21, stdin), l == r) ? EOF : *l++)

char c[1 << 21], *l = c, *r = c;
inline int read() {
  int x = 0;
  char ch = getc;
  while (!isdigit(ch))
    ch = getc;
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getc;
  }
  return x;
}

const int N = 200010;
int n, q, cnt, last[N], s[N];
struct Edge {
  int y, pre;
} E[N << 1];
void dfs(int x, int fa, int sum) {
  s[x] += sum;
  for (int j = last[x]; j; j = E[j].pre)
    if (E[j].y != fa)
      dfs(E[j].y, x, s[x]);
}
int main() {
  n = read();
  q = read();
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read();
    E[++cnt] = (Edge){y, last[x]};
    last[x] = cnt;
    E[++cnt] = (Edge){x, last[y]};
    last[y] = cnt;
  }
  while (q--) {
    int p = read(), x = read();
    s[p] += x;
  }
  dfs(1, -1, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d ", s[i]);
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;

vector<int> edges[MAXN];
long long a[MAXN], add[MAXN];
bool vis[MAXN];

void dfs(int u, long long qtt) {
  vis[u] = true;
  a[u] = add[u] + qtt;

  for (int v : edges[u]) {
    if (!vis[v])
      dfs(v, qtt + add[u]);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, q;
  cin >> n >> q;

  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  while (q--) {
    int u, x;
    cin >> u >> x;
    add[u] += x;
  }

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 5;

vector<int> edges[MAXN];
long long a[MAXN], add[MAXN];
bool vis[MAXN];

void dfs(int u, long long qtt) {
  vis[u] = true;
  a[u] = add[u] + qtt;

  for (int v : edges[u]) {
    if (!vis[v])
      dfs(v, qtt + add[u]);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, q;
  cin >> n >> q;

  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  while (q--) {
    int u, x;
    cin >> u >> x;
    add[u] += x;
  }

  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void solve(int v, const vector<vector<int>> G, vector<bool> &seen,
           vector<long long> &cost) {
  seen[v] = 1;
  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    cost[nv] += cost[v];
    solve(nv, G, seen, cost);
  }
}
int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);
  vector<vector<int>> G(N, vector<int>());
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<long long> cost(N);
  for (int i = 0; i < Q; i++) {
    int a;
    long long b;
    scanf("%d%lld", &a, &b);
    a--;
    cost[a] += b;
  }
  vector<bool> seen(N);
  solve(0, G, seen, cost);
  for (int i = 0; i < N; i++) {
    if (i)
      printf(" ");
    printf("%lld", cost[i]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void solve(int v, const vector<vector<int>> &G, vector<bool> &seen,
           vector<long long> &cost) {
  seen[v] = 1;
  for (auto nv : G[v]) {
    if (seen[nv])
      continue;
    cost[nv] += cost[v];
    solve(nv, G, seen, cost);
  }
}
int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);
  vector<vector<int>> G(N, vector<int>());
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<long long> cost(N);
  for (int i = 0; i < Q; i++) {
    int a;
    long long b;
    scanf("%d%lld", &a, &b);
    a--;
    cost[a] += b;
  }
  vector<bool> seen(N);
  solve(0, G, seen, cost);
  for (int i = 0; i < N; i++) {
    if (i)
      printf(" ");
    printf("%lld", cost[i]);
  }
  printf("\n");
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007;
#define INF 1000000000000000001;
#define ll long long
vector<int> g[200005];
vector<pair<int, int>> wg[123456];
int level[123456];
void dfs(int s, int pre) {
  for (auto i : g[s]) {
    if (i != pre) {
      level[i] += level[s];
      dfs(i, s);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (size_t i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 0; i < m; ++i) {
    int p, q;
    cin >> p >> q;
    level[p] += q;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << level[i] << " ";

  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007;
#define INF 1000000000000000001;
#define ll long long
vector<int> g[200005];
vector<pair<int, int>> wg[123456];
int level[200005];
void dfs(int s, int pre) {
  for (auto i : g[s]) {
    if (i != pre) {
      level[i] += level[s];
      dfs(i, s);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (size_t i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 0; i < m; ++i) {
    int p, q;
    cin >> p >> q;
    level[p] += q;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << level[i] << " ";

  cout << endl;
  return 0;
}
Problem p02936 (C++)

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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

vector<vector<ll>> tree(100010);
vector<ll> point(100010, 0);

void dfs(ll crr, ll parent) {
  rep(i, tree[crr].size()) {
    if (tree[crr][i] == parent)
      continue;

    point[tree[crr][i]] += point[crr];
    dfs(tree[crr][i], crr);
  }
}

int main() {
  ll n, q;
  cin >> n >> q;

  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;

    a--, b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }

  rep(i, q) {
    ll p, x;
    cin >> p >> x;

    p--;
    point[p] += x;
  }

  dfs(0, 0);

  rep(i, n) cout << point[i] << " ";
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

vector<vector<ll>> tree(200010);
vector<ll> point(200010, 0);

void dfs(ll crr, ll parent) {
  rep(i, tree[crr].size()) {
    if (tree[crr][i] == parent)
      continue;

    point[tree[crr][i]] += point[crr];
    dfs(tree[crr][i], crr);
  }
}

int main() {
  ll n, q;
  cin >> n >> q;

  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;

    a--, b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }

  rep(i, q) {
    ll p, x;
    cin >> p >> x;

    p--;
    point[p] += x;
  }

  dfs(0, 0);

  rep(i, n) cout << point[i] << " ";
}
Problem p02936 (C++)

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

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000

ll N, Q;
vector<vector<ll>> G(200010);
vector<ll> cost(200010, 0);
vector<bool> visited(200010, false);

void dfs(ll i, ll prev_i) {
  if (!visited[i]) {
    visited[i] = true;
    cost[i] += cost[prev_i];
  }
  for (auto next_i : G[i])
    dfs(next_i, i);
}

signed main() {
  cin >> N >> Q;
  FOR(i, 1, N - 1) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  REP(j, Q) {
    ll p, x;
    cin >> p >> x;
    cost[p] += x;
  }
  dfs(1, 0);
  FOR(i, 1, N) cout << cost[i] << " ";
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000

ll N, Q;
vector<vector<ll>> G(200010);
vector<ll> cost(200010, 0);
vector<bool> visited(200010, false);

void dfs(ll i, ll prev_i) {
  if (visited[i])
    return;
  visited[i] = true;
  cost[i] += cost[prev_i];
  for (auto next_i : G[i])
    dfs(next_i, i);
}

signed main() {
  cin >> N >> Q;
  FOR(i, 1, N - 1) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  REP(j, Q) {
    ll p, x;
    cin >> p >> x;
    cost[p] += x;
  }
  dfs(1, 0);
  FOR(i, 1, N) cout << cost[i] << " ";
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll MOD = 1000000007;

vector<int> to[20005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u = 0; u < int(to[v].size()); u++) {
    int x = to[v][u];
    if (x == p)
      continue;
    ans[x] += ans[v];
    dfs(x, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll MOD = 1000000007;

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u = 0; u < int(to[v].size()); u++) {
    int x = to[v][u];
    if (x == p)
      continue;
    ans[x] += ans[v];
    dfs(x, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
typedef long double ld;
#define maxn 100001

int N, Q;
vi adj[maxn];
int st[maxn], en[maxn];

int fw[maxn];
int cur = 1;

void upd(int p, int v) { // update position p by +=v
  for (int i = p; i <= N; i += (i & (-i))) {
    fw[i] += v;
  }
}

void update_range(int a, int b, int c) { // update position a to b by +=c
  upd(a, c);                             // upd function from the PURQ fenwick
  upd(b + 1, -c);
}

int qry(int p) { // query p
  int ans = 0;
  for (int i = p; i > 0; i -= (i & (-i))) {
    ans += fw[i];
  }
  return ans;
}

void dfs(int x, int p) {
  st[x] = cur++;
  aFOR(i, adj[x]) if (i != p) dfs(i, x);
  en[x] = cur - 1;
}
int32_t main() {
  cin >> N >> Q;

  FOR(i, 1, N - 1) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, -1);

  FOR(i, 1, Q) {
    int p, x;
    cin >> p >> x;

    update_range(st[p], en[p], x);
  }

  FOR(i, 1, N) cout << qry(st[i]) << ' ';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
typedef long double ld;
#define maxn 200001

int N, Q;
vi adj[maxn];
int st[maxn], en[maxn];

int fw[maxn];
int cur = 1;

void upd(int p, int v) { // update position p by +=v
  for (int i = p; i <= N; i += (i & (-i))) {
    fw[i] += v;
  }
}

void update_range(int a, int b, int c) { // update position a to b by +=c
  upd(a, c);                             // upd function from the PURQ fenwick
  upd(b + 1, -c);
}

int qry(int p) { // query p
  int ans = 0;
  for (int i = p; i > 0; i -= (i & (-i))) {
    ans += fw[i];
  }
  return ans;
}

void dfs(int x, int p) {
  st[x] = cur++;
  aFOR(i, adj[x]) if (i != p) dfs(i, x);
  en[x] = cur - 1;
}
int32_t main() {
  cin >> N >> Q;

  FOR(i, 1, N - 1) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, -1);

  FOR(i, 1, Q) {
    int p, x;
    cin >> p >> x;

    update_range(st[p], en[p], x);
  }

  FOR(i, 1, N) cout << qry(st[i]) << ' ';
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const long long INF = (long long)1e18 + 1;
#define DIV 1000000007
vector<ll> counter(2000001);

void countup(unordered_map<ll, vector<ll>> tree, ll prev, ll next) {
  counter[next] += counter[prev];
  for (auto elem : tree[next]) {
    if (elem == prev)
      continue;
    countup(tree, next, elem);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long idx = 0;
  long N, Q;
  cin >> N >> Q;
  unordered_map<ll, vector<ll>> tree;
  for (size_t i = 1; i <= N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  for (size_t i = 0; i < Q; i++) {
    long p, x;
    cin >> p >> x;
    counter[p] += x;
  }
  countup(tree, 0L, 1L);
  for (size_t i = 1; i <= N; i++) {
    cout << counter[i] << " ";
  }
  cout << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const long long INF = (long long)1e18 + 1;
#define DIV 1000000007
vector<ll> counter(2000001);

void countup(unordered_map<ll, vector<ll>> &tree, ll prev, ll next) {
  counter[next] += counter[prev];
  for (auto elem : tree[next]) {
    if (elem == prev)
      continue;
    countup(tree, next, elem);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long idx = 0;
  long N, Q;
  cin >> N >> Q;
  unordered_map<ll, vector<ll>> tree;
  for (size_t i = 1; i <= N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  for (size_t i = 0; i < Q; i++) {
    long p, x;
    cin >> p >> x;
    counter[p] += x;
  }
  countup(tree, 0L, 1L);
  for (size_t i = 1; i <= N; i++) {
    cout << counter[i] << " ";
  }
  cout << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

vector<vector<int>> g;
vector<bool> v;
vector<int64_t> c;
vector<int64_t> p;

void dfs(const int s, int64_t x) {
  if (v[s])
    return;
  v[s] = true;
  x += p[s];
  c[s] += x;

  for (int elm : g[s]) {
    if (not v[elm])
      dfs(elm, x);
  }
}

int main() {
  int n, q;
  cin >> n >> q;

  g.assign(n, vector<int>(0));
  for (auto i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }

  for (auto i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    p[a - 1] += b;
  }

  v.assign(n, false);
  c.assign(n, 0);
  dfs(0, 0);

  for (auto i = 0; i < n; i++) {
    cout << c[i] << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

vector<vector<int>> g;
vector<bool> v;
vector<int64_t> c;
vector<int64_t> p;

void dfs(const int s, int64_t x) {
  if (v[s])
    return;
  v[s] = true;
  x += p[s];
  c[s] += x;

  for (int elm : g[s]) {
    if (not v[elm])
      dfs(elm, x);
  }
}

int main() {
  int n, q;
  cin >> n >> q;

  g.assign(n, vector<int>(0));
  for (auto i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }

  p.assign(n, 0);
  for (auto i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    p[a - 1] += b;
  }

  v.assign(n, false);
  c.assign(n, 0);
  dfs(0, 0);

  for (auto i = 0; i < n; i++) {
    cout << c[i] << " ";
  }
  cout << endl;
  return 0;
}