prithivMLmods/Coder-Stat huggingface dataset:
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;
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; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
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; }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

struct youso {
  int parent = -1;
  int count = 0;
  std::vector<int> edge;
  // std::vector<int> child;
};
youso y[100000];
void maketree(int parent, int ima) {
  y[ima].parent = parent;
  for (int i = 0; i < y[ima].edge.size(); i++) {
    if (parent == y[ima].edge[i])
      continue;
    else {
      maketree(ima, y[ima].edge[i]);
      // y[ima].child.push_back(y[ima].edge[i]);
    }
  }
}
void countup(int ima, int x) {
  y[ima].count += x;
  for (int i = 0; i < y[ima]./*child*/ edge.size(); i++) {
    if (y[ima].parent != y[ima].edge[i]) {
      countup(y[ima]./*child*/ edge[i], y[ima].count);
    }
  }
}

int main() {
  int n, q, a, b, p, x; // a,b,pは引く１する必要がある
  std::cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    std::cin >> a >> b;
    y[a - 1].edge.push_back(b - 1);
    y[b - 1].edge.push_back(a - 1);
  }
  maketree(-1, 0);
  for (int i = 0; i < q; i++) {
    std::cin >> p >> x;
    y[p - 1].count += x;
  }
  countup(0, 0);
  std::cout << y[0].count;
  for (int i = 1; i < n; i++) {
    std::cout << ' ' << y[i].count;
  }
  std::cout << std::endl;
}


Fixed submission:
#include <iostream>
#include <vector>

struct youso {
  int parent = -1;
  int count = 0;
  std::vector<int> edge;
  // std::vector<int> child;
};
youso y[200000];
void maketree(int parent, int ima) {
  y[ima].parent = parent;
  for (int i = 0; i < y[ima].edge.size(); i++) {
    if (parent == y[ima].edge[i])
      continue;
    else {
      maketree(ima, y[ima].edge[i]);
      // y[ima].child.push_back(y[ima].edge[i]);
    }
  }
}
void countup(int ima, int x) {
  y[ima].count += x;
  for (int i = 0; i < y[ima]./*child*/ edge.size(); i++) {
    if (y[ima].parent != y[ima].edge[i]) {
      countup(y[ima]./*child*/ edge[i], y[ima].count);
    }
  }
}

int main() {
  int n, q, a, b, p, x; // a,b,pは引く１する必要がある
  std::cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    std::cin >> a >> b;
    y[a - 1].edge.push_back(b - 1);
    y[b - 1].edge.push_back(a - 1);
  }
  maketree(-1, 0);
  for (int i = 0; i < q; i++) {
    std::cin >> p >> x;
    y[p - 1].count += x;
  }
  countup(0, 0);
  std::cout << y[0].count;
  for (int i = 1; i < n; i++) {
    std::cout << ' ' << y[i].count;
  }
  std::cout << std::endl;
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N, Q;

int main() {
  cin >> N >> Q;
  vector<vector<int>> e(N);
  vector<int> par(N, -1); // 親
  for (int i = 0; i < N - 1; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    e[from].push_back(to);
    e[to].push_back(from);
    par[to] = from;
  }
  vector<int> cnt(N, 0);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    cnt[p] += x;
  }
  par[0] = -1;
  for (int i = 0; i < N; i++) {
    int ans = cnt[i];
    int p = par[i];
    while (p != -1) {
      ans += cnt[p];
      p = par[p];
    }
    cout << ans << " " << flush;
  }
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N, Q;

int main() {
  cin >> N >> Q;
  vector<vector<int>> e(N);
  vector<int> par(N, -1); // 親
  for (int i = 0; i < N - 1; i++) {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    e[from].push_back(to);
    e[to].push_back(from);
    par[to] = from;
  }
  vector<int> cnt(N, 0);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    cnt[p] += x;
  }
  par[0] = -1;
  vector<int> ans(N);
  ans[0] = cnt[0];
  cout << ans[0] << " " << flush;
  for (int i = 1; i < N; i++) {
    ans[i] = cnt[i] + ans[par[i]];
    cout << ans[i] << " " << flush;
  }
  cout << endl;
}
Problem p02936 (C++)

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

using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(), c.end()

const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;

vector<ll> g[202020];
ll score[202020];

void dfs(int node, ll prev) {
  score[node] += score[prev];
  for (int i = 0; i < g[node].size(); i++) {
    if (g[node][i] == prev)
      continue;
    dfs(g[node][i], node);
  }
}

int main() {
  fill(score, score + 202020, 0);
  ll n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p;
    ll x;
    cin >> p >> x;
    score[p] += x;
  }

  dfs(1, score[1]);

  for (int i = 1; i <= n; i++)
    cout << score[i] << " ";
  cout << endl;
  // cout << ans << endl;
}

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

using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(), c.end()

const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;

vector<ll> g[202020];
ll score[202020];

void dfs(int node, ll prev) {
  score[node] += score[prev];
  for (int i = 0; i < g[node].size(); i++) {
    if (g[node][i] == prev)
      continue;
    dfs(g[node][i], node);
  }
}

int main() {
  fill(score, score + 202020, 0);
  ll n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p;
    ll x;
    cin >> p >> x;
    score[p] += x;
  }

  dfs(1, 0);

  for (int i = 1; i <= n; i++)
    cout << score[i] << " ";
  cout << endl;
  // cout << ans << endl;
}
Problem p02936 (C++)

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

typedef pair<int, int> P;
struct edge {
  int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007

using graph = vector<vector<ll>>;
int jikan;
bool seen[100010];
ll sum[100010];
ll ans[100010];
void dfs(const graph &G, int v, ll parent, ll val) {
  // seen[v] = true;
  // root[v].push_back(v);
  sum[v] += val;

  for (auto next_v : G[v]) {
    // ans[next_v] = v+1;
    // first[next_v] = jikan;
    // ans[next_v] += ans[v];

    if (next_v == parent)
      continue;
    dfs(G, next_v, v, sum[v]);
    // root[v].push_back(next_v);
    /*for(auto hoge : root[next_v]){
      root[v].push_back(hoge);
    }*/
  }
}

int main() {
  ll n, q;
  cin >> n >> q;
  graph g(n);
  graph root(n);
  ll ans[n];
  rep(i, n) {
    seen[i] = false;
    ans[i] = 0;
  }
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    sum[a - 1] += b;
  }
  dfs(g, 0, -1, 0);
  rep(i, n) {
    if (i < n - 1)
      cout << sum[i] << ' ';
    else
      cout << sum[i] << endl;
  }
}

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

typedef pair<int, int> P;
struct edge {
  int to, id;
};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007

using graph = vector<vector<ll>>;
int jikan;
bool seen[200010];
ll sum[200010];
ll ans[200010];
void dfs(const graph &G, int v, ll parent, ll val) {
  // seen[v] = true;
  // root[v].push_back(v);
  sum[v] += val;

  for (auto next_v : G[v]) {
    // ans[next_v] = v+1;
    // first[next_v] = jikan;
    // ans[next_v] += ans[v];

    if (next_v == parent)
      continue;
    dfs(G, next_v, v, sum[v]);
    // root[v].push_back(next_v);
    /*for(auto hoge : root[next_v]){
      root[v].push_back(hoge);
    }*/
  }
}

int main() {
  ll n, q;
  cin >> n >> q;
  graph g(n);
  graph root(n);
  ll ans[n];
  rep(i, n) {
    seen[i] = false;
    ans[i] = 0;
  }
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    sum[a - 1] += b;
  }
  dfs(g, 0, -1, 0);
  rep(i, n) {
    if (i < n - 1)
      cout << sum[i] << ' ';
    else
      cout << sum[i] << endl;
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 100005;
vector<int> v[N];
ll ans[N];
void dfs(int x, ll add, int p) {
  if (x)
    ans[x] += add;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != p)
      dfs(v[x][i], ans[x], x);
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  while (q--) {
    int par, val;
    cin >> par >> val;
    ans[par - 1] += val;
  }
  dfs(0, ans[0], -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200005;
vector<int> v[N];
ll ans[N];
void dfs(int x, ll add, int p) {
  if (x)
    ans[x] += add;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != p)
      dfs(v[x][i], ans[x], x);
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  while (q--) {
    int par, val;
    cin >> par >> val;
    ans[par - 1] += val;
  }
  dfs(0, ans[0], -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
  return 0;
}
Problem p02936 (C++)

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

#define N 200123
#define mod 1000000007

long long weight[N];
vector<int> adj[N];

void dfs(int u, int par) {
  weight[u] += weight[par];
  for (int v : adj[u])
    if (v != par)
      dfs(v, u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  int n, q, a;
  long long b;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  fill(weight, weight + N, 0);

  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    weight[a] += b;
  }
  dfs(1, 0);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << weight[i + 1];
  }
  return 0;
}

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

#define N 200123
#define mod 1000000007

long long weight[N];
vector<int> adj[N];

void dfs(int u, int par) {
  weight[u] += weight[par];
  for (int v : adj[u])
    if (v != par)
      dfs(v, u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q, a;
  long long b;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  fill(weight, weight + N, 0);

  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    weight[a] += b;
  }
  dfs(1, 0);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << weight[i + 1];
  }
  return 0;
}
Problem p02936 (C++)

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

vector<bool> seen(200000, false);

void dfs(vector<vector<int>> con, int num, vector<int> &ope) {
  seen.at(num) = true;
  for (int con_ele : con.at(num)) {
    if (seen.at(con_ele) == false) {
      ope.at(con_ele) += ope.at(num);
      dfs(con, con_ele, ope);
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> points(n);
  for (int i = 0; i < n - 1; i++) {
    int a_in, b_in;
    cin >> a_in >> b_in;
    points.at(a_in - 1).push_back(b_in - 1);
    points.at(b_in - 1).push_back(a_in - 1);
  }
  vector<int> ope(n, 0);
  for (int i = 0; i < q; i++) {
    int p_in, x_in;
    cin >> p_in >> x_in;
    ope.at(p_in - 1) += x_in;
  }

  dfs(points, 0, ope);

  for (int ope_ele : ope) {
    cout << ope_ele << " ";
  }
  cout << endl;
}


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

vector<bool> seen(200000, false);

void dfs(vector<vector<int>> &con, int num, vector<int> &ope) {
  seen.at(num) = true;
  for (int con_ele : con.at(num)) {
    if (seen.at(con_ele) == false) {
      ope.at(con_ele) += ope.at(num);
      dfs(con, con_ele, ope);
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> points(n);
  for (int i = 0; i < n - 1; i++) {
    int a_in, b_in;
    cin >> a_in >> b_in;
    points.at(a_in - 1).push_back(b_in - 1);
    points.at(b_in - 1).push_back(a_in - 1);
  }
  vector<int> ope(n, 0);
  for (int i = 0; i < q; i++) {
    int p_in, x_in;
    cin >> p_in >> x_in;
    ope.at(p_in - 1) += x_in;
  }

  dfs(points, 0, ope);

  for (int ope_ele : ope) {
    cout << ope_ele << " ";
  }
  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 FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v[200010];
vector<int> ans(20010, 0);
void dfs(int i, int p = -1) {
  for (int j : v[i]) {
    if (j == p)
      continue;
    ans[j] += ans[i];
    dfs(j, i);
  }
}

int main() {
  // vector<int> v[200010];
  int q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v[200010];
vector<int> ans(200100, 0);
void dfs(int i, int p = -1) {
  for (int j : v[i]) {
    if (j == p)
      continue;
    ans[j] += ans[i];
    dfs(j, i);
  }
}

int main() {
  // vector<int> v[200010];
  int q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string.h>

using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL head[N], cnt = 0, sum[N], pre_sum[N];
struct edge {
  LL to, next;
} p[N * 2];
void add(LL x, LL y) {
  p[++cnt].to = y;
  p[cnt].next = head[x];
  head[x] = cnt;
}
void spread(LL x) {
  for (LL i = head[x]; i; i = p[i].next) {
    pre_sum[p[i].to] += pre_sum[x];
  }
  sum[x] += pre_sum[x];
  pre_sum[x] = 0;
}
int main() {
  LL n, q;
  cin >> n >> q;
  memset(sum, 0, sizeof(sum));
  memset(pre_sum, 0, sizeof(pre_sum));
  for (LL i = 1; i < n; i++) {
    LL x, y;
    cin >> x >> y;
    add(x, y);
    add(y, x);
  }
  for (LL i = 1; i <= q; i++) {
    LL x, v;
    cin >> x >> v;
    pre_sum[x] += v;
    // spread(x);
  }
  for (LL i = 1; i <= n; i++)
    spread(i);
  for (LL i = 1; i <= n; i++)
    i != n ? cout << sum[i] << ' ' : cout << sum[i] << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string.h>

using namespace std;
const int N = 2e5 + 10;
typedef long long LL;
LL head[N], cnt = 0, sum[N], pre_sum[N];
struct edge {
  LL to, next;
} p[N * 2];
void add(LL x, LL y) {
  p[++cnt].to = y;
  p[cnt].next = head[x];
  head[x] = cnt;
}
void spread(LL x) {
  for (LL i = head[x]; i; i = p[i].next) {
    pre_sum[p[i].to] += pre_sum[x];
  }
  sum[x] += pre_sum[x];
  pre_sum[x] = 0;
}
int main() {
  LL n, q;
  cin >> n >> q;
  memset(sum, 0, sizeof(sum));
  memset(pre_sum, 0, sizeof(pre_sum));
  for (LL i = 1; i < n; i++) {
    LL x, y;
    cin >> x >> y;
    add(x, y);
    add(y, x);
  }
  for (LL i = 1; i <= q; i++) {
    LL x, v;
    cin >> x >> v;
    pre_sum[x] += v;
    // spread(x);
  }
  for (LL i = 1; i <= n; i++)
    spread(i);
  for (LL i = 1; i <= n; i++)
    i != n ? cout << sum[i] << ' ' : cout << sum[i] << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 1e5 + 5;
vector<LL> GRAPH[MAX];
LL a[MAX];
bool vis[MAX];
LL b[MAX];

void DFS(LL x) {
  if (vis[x])
    return;
  vis[x] = true;
  for (int i = 0; i < GRAPH[x].size(); i++) {
    if (!vis[GRAPH[x][i]]) {
      b[GRAPH[x][i]] += b[x];
      DFS(GRAPH[x][i]);
    }
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int m = n - 1;
  memset(vis, false, sizeof(vis));
  while (m--) {
    LL x, y;
    cin >> x >> y;
    GRAPH[x].push_back(y);
    GRAPH[y].push_back(x);
  }
  while (q--) {
    LL p, x;
    cin >> p >> x;
    a[p] += x;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
  }
  DFS(1);
  for (int i = 1; i <= n; i++) {
    cout << b[i] << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX = 2e5 + 5;
vector<LL> GRAPH[MAX];
LL a[MAX];
bool vis[MAX];
LL b[MAX];

void DFS(LL x) {
  if (vis[x])
    return;
  vis[x] = true;
  for (int i = 0; i < GRAPH[x].size(); i++) {
    if (!vis[GRAPH[x][i]]) {
      b[GRAPH[x][i]] += b[x];
      DFS(GRAPH[x][i]);
    }
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int m = n - 1;
  memset(vis, false, sizeof(vis));
  while (m--) {
    LL x, y;
    cin >> x >> y;
    GRAPH[x].push_back(y);
    GRAPH[y].push_back(x);
  }
  while (q--) {
    LL p, x;
    cin >> p >> x;
    a[p] += x;
  }
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
  }
  DFS(1);
  for (int i = 1; i <= n; i++) {
    cout << b[i] << " ";
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

int dd[100010] = {};
vector<vector<int>> d(100010);
int ans[100010] = {};
int n, q, a, b, p, x;
int tmp;

void dfs(int x, int tmp) {
  ans[x] += tmp;
  for (auto next : d[x]) {
    dfs(next, tmp + dd[next]);
  }
}

int main() {
  cin >> n >> q;
  rep(i, n - 1) {
    cin >> a >> b;
    d[a].emplace_back(b);
  }
  rep(i, q) {
    cin >> p >> x;
    dd[p] += x;
  }
  dfs(1, dd[1]);
  REP(i, 1, n) { cout << ans[i] << " "; }
  cout << ans[n] << endl;
  return 0;
}

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

int dd[200010] = {};
vector<vector<int>> d(200010);
int ans[200010] = {};
int n, q, a, b, p, x;
int tmp;

void dfs(int x, int tmp) {
  ans[x] += tmp;
  for (auto next : d[x]) {
    dfs(next, tmp + dd[next]);
  }
}

int main() {
  cin >> n >> q;
  rep(i, n - 1) {
    cin >> a >> b;
    d[a].emplace_back(b);
  }
  rep(i, q) {
    cin >> p >> x;
    dd[p] += x;
  }
  dfs(1, dd[1]);
  REP(i, 1, n) { cout << ans[i] << " "; }
  cout << ans[n] << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 1001001001;
// stoi(s) : string→int stoll(s) :string→longlong  int→string to_string(i)
const double PI = acos(-1.0);
// 小数点の表し方 cout << fixed << setprecision(5);
const int MAXN = 100100;
ll num[MAXN] = {0};
bool seen[MAXN] = {false};

void dfs(Graph &G, int n) {
  seen[n] = true;
  for (auto x : G[n]) {
    if (seen[x])
      continue;
    num[x] += num[n];
    dfs(G, x);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n - 1), b(n - 1);
  vector<int> p(q), x(q);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 0; i < q; i++) {
    cin >> p[i] >> x[i];
    p[i]--;
    num[p[i]] += x[i];
  }
  Graph G(n);
  for (int i = 0; i < n - 1; i++) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  dfs(G, 0);
  for (int i = 0; i < n; i++) {
    cout << num[i] << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 1001001001;
// stoi(s) : string→int stoll(s) :string→longlong  int→string to_string(i)
const double PI = acos(-1.0);
// 小数点の表し方 cout << fixed << setprecision(5);
const int MAXN = 200100;
ll num[MAXN] = {0};
bool seen[MAXN] = {false};

void dfs(Graph &G, int n) {
  seen[n] = true;
  for (auto x : G[n]) {
    if (seen[x])
      continue;
    num[x] += num[n];
    dfs(G, x);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n - 1), b(n - 1);
  vector<int> p(q), x(q);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 0; i < q; i++) {
    cin >> p[i] >> x[i];
    p[i]--;
    num[p[i]] += x[i];
  }
  Graph G(n);
  for (int i = 0; i < n - 1; i++) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  dfs(G, 0);
  for (int i = 0; i < n; i++) {
    cout << num[i] << " ";
  }
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;
vector<vector<int>> G(404040);
vector<int> score(404040, 0);
vector<int> ans(404040, -1);

int dfs(int x, int sum) {
  if (ans[x] != -1)
    return 0;
  sum += score[x];
  ans[x] = sum;
  for (auto nex : G[x]) {
    dfs(nex, sum);
  }
}

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

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;
vector<vector<int>> G(404040);
vector<int> score(404040, 0);
vector<int> ans(404040, -1);

int dfs(int x, int sum) {
  if (ans[x] != -1)
    return 0;
  sum += score[x];
  ans[x] = sum;
  for (auto nex : G[x]) {
    dfs(nex, sum);
  }
  return 0;
}

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

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

int main() {
  i64 n, q;
  cin >> n >> q;
  vector<i64> edge[n];
  for (i64 i = 0; i < n - 1; i++) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  vector<i64> ans(n);
  for (i64 i = 0; i < q; i++) {
    i64 p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  vector<bool> tmp(n);
  queue<i64> que;
  que.push(0);
  tmp[0] = true;
  while (que.size()) {
    i64 p = que.front();
    que.pop();
    for (i64 i : edge[p])
      if (!tmp[i]) {
        ans[i] += ans[p];
        que.push(i);
      }
  }
  for (i64 i = 0; i < n; i++)
    cout << ans[i] << (i + 1 == n ? "\n" : " ");
  return 0;
}


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

int main() {
  i64 n, q;
  cin >> n >> q;
  vector<i64> edge[n];
  for (i64 i = 0; i < n - 1; i++) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  vector<i64> ans(n);
  for (i64 i = 0; i < q; i++) {
    i64 p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  vector<bool> tmp(n);
  queue<i64> que;
  que.push(0);
  tmp[0] = true;
  while (que.size()) {
    i64 p = que.front();
    que.pop();
    for (i64 i : edge[p])
      if (!tmp[i]) {
        ans[i] += ans[p];
        que.push(i);
        tmp[i] = true;
      }
  }
  for (i64 i = 0; i < n; i++)
    cout << ans[i] << (i + 1 == n ? "\n" : " ");
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

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

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == v)
      continue;
    ans[u] += ans[v];
    dfs(u, 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 <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

// ki
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;
  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 <iostream>
#include <vector>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  int a, b;
  vector<vector<int>> AB(N + 5, vector<int>(N + 5));
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    AB[a].push_back(b);
    AB[b].push_back(a);
  }
  int p, x;
  int Score[200005] = {};
  for (int j = 0; j < Q; j++) {
    cin >> p >> x;
    Score[p] += x;
  }

  vector<int> Now;
  vector<int> Next{1};
  bool Exists[200005] = {};
  Exists[1] = true;

  while (!Next.empty()) {
    Now = Next;
    Next.clear();
    for (int nw : Now) {
      for (int nxt : AB[nw]) {
        if (!Exists[nxt]) {
          Exists[nxt] = true;
          Score[nxt] += Score[nw];
          Next.push_back(nxt);
        }
      }
    }
  }

  for (int i = 1; i <= N; i++)
    cout << Score[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  int a, b;
  vector<vector<int>> AB(N + 5, vector<int>());
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    AB[a].push_back(b);
    AB[b].push_back(a);
  }
  int p, x;
  int Score[200005] = {};
  for (int j = 0; j < Q; j++) {
    cin >> p >> x;
    Score[p] += x;
  }

  vector<int> Now;
  vector<int> Next{1};
  bool Exists[200005] = {};
  Exists[1] = true;

  while (!Next.empty()) {
    Now = Next;
    Next.clear();
    for (int nw : Now) {
      for (int nxt : AB[nw]) {
        if (!Exists[nxt]) {
          Exists[nxt] = true;
          Score[nxt] += Score[nw];
          Next.push_back(nxt);
        }
      }
    }
  }

  for (int i = 1; i <= N; i++)
    cout << Score[i] << " ";
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int maxn = 200000;
int n, m, p[maxn], add[maxn], a, b;
vector<int> v[maxn];

void dfs(int node, int pa) {
  for (int i : v[node]) {
    if (i != pa) {
      add[i] += add[node];
      dfs(i, node);
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    add[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      cout << " ";
    cout << add[i];
  }
  cout << "\n";
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int maxn = 200005;
int n, m, p[maxn], add[maxn], a, b;
vector<int> v[maxn];

void dfs(int node, int pa) {
  for (int i : v[node]) {
    if (i != pa) {
      add[i] += add[node];
      dfs(i, node);
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    add[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (i != 1)
      cout << " ";
    cout << add[i];
  }
  cout << "\n";
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>

// 入力受付用の変数
#define BUF 200002
char str[BUF];

// 入力された値
#define LIM_A 100001
int N, Q;
int a[LIM_A];
int b[LIM_A];
int p[LIM_A];
int x[LIM_A];

typedef struct {
  int root;
  int count;
  long ans;
} Tree;
Tree v[LIM_A];

void receive_input() {
  fgets(str, sizeof(str), stdin);
  sscanf(str, "%d %d", &N, &Q);

  for (size_t i = 1; i <= N - 1; i++) {
    scanf("%d %d", &a[i], &b[i]);
  }

  for (size_t i = 1; i <= Q; i++) {
    scanf("%d %d", &p[i], &x[i]);
  }
}

void solve() {
  for (size_t i = 1; i <= N - 1; i++) {
    v[b[i]].root = a[i];
  }

  for (size_t i = 1; i <= Q; i++) {
    v[p[i]].count += x[i];
  }

  for (size_t i = 1; i <= N; i++) {
    v[i].ans += v[i].count;
    if (v[i].root != 0) {
      v[i].ans += v[v[i].root].ans;
    }
    printf("%ld ", v[i].ans);
  }
  printf("\n");
}

int main(void) {
  receive_input();

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>

// 入力受付用の変数
#define BUF 200002
char str[BUF];

// 入力された値
#define LIM_A 200002
int N, Q;
int a[LIM_A];
int b[LIM_A];
int p[LIM_A];
int x[LIM_A];

typedef struct {
  int root;
  int count;
  long ans;
} Tree;
Tree v[LIM_A];

void receive_input() {
  fgets(str, sizeof(str), stdin);
  sscanf(str, "%d %d", &N, &Q);

  for (size_t i = 1; i <= N - 1; i++) {
    scanf("%d %d", &a[i], &b[i]);
  }

  for (size_t i = 1; i <= Q; i++) {
    scanf("%d %d", &p[i], &x[i]);
  }
}

void solve() {
  for (size_t i = 1; i <= N - 1; i++) {
    v[b[i]].root = a[i];
  }

  for (size_t i = 1; i <= Q; i++) {
    v[p[i]].count += x[i];
  }

  for (size_t i = 1; i <= N; i++) {
    v[i].ans += v[i].count;
    if (v[i].root != 0) {
      v[i].ans += v[v[i].root].ans;
    }
    printf("%ld ", v[i].ans);
  }
  printf("\n");
}

int main(void) {
  receive_input();

  solve();

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100010];
int n, i, j, x, y, a[100001], q, nod, val;
bool viz[100001];
void df(int nod) {
  int i;
  viz[nod] = 1;
  for (i = 0; i < v[nod].size(); i++)
    if (!viz[v[nod][i]]) {
      a[v[nod][i]] += a[nod];
      df(v[nod][i]);
    }
}
int main() {
  cin >> n >> q;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (i = 1; i <= q; i++) {
    cin >> nod >> val;
    a[nod] += val;
  }
  df(1);
  for (i = 1; i <= n; i++)
    cout << a[i] << ' ';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200010];
int n, i, j, x, y, a[200001], q, nod, val;
bool viz[200001];
void df(int nod) {
  int i;
  viz[nod] = 1;
  for (i = 0; i < v[nod].size(); i++)
    if (!viz[v[nod][i]]) {
      a[v[nod][i]] += a[nod];
      df(v[nod][i]);
    }
}
int main() {
  cin >> n >> q;
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (i = 1; i <= q; i++) {
    cin >> nod >> val;
    a[nod] += val;
  }
  df(1);
  for (i = 1; i <= n; i++)
    cout << a[i] << ' ';

  return 0;
}
Problem p02936 (C++)

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

void rec(int parent, int self, int cntsum, vector<set<int>> connect,
         vector<int> cntarr, vector<int> &ans) {
  connect[self].erase(parent);
  for (auto itr = connect[self].begin(); itr != connect[self].end(); itr++) {
    int child = *itr;
    int copycntsum = cntsum + cntarr[child];
    rec(self, child, copycntsum, connect, cntarr, ans);
  }
  ans[self] = cntsum;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<set<int>> connect(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    connect[a].insert(b);
    connect[b].insert(a);
  }
  vector<int> cntarr(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    cntarr[p] += x;
  }
  vector<int> ans(n);
  rec(-1, 0, cntarr[0], connect, cntarr, ans);
  rep(i, n) { cout << ans[i] << " "; }
  cout << endl;
}

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

void rec(int parent, int self, int cntsum, vector<set<int>> &connect,
         vector<int> &cntarr, vector<int> &ans) {
  connect[self].erase(parent);
  for (auto itr = connect[self].begin(); itr != connect[self].end(); itr++) {
    int child = *itr;
    int copycntsum = cntsum + cntarr[child];
    rec(self, child, copycntsum, connect, cntarr, ans);
  }
  ans[self] = cntsum;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<set<int>> connect(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    connect[a].insert(b);
    connect[b].insert(a);
  }
  vector<int> cntarr(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    cntarr[p] += x;
  }
  vector<int> ans(n);
  rec(-1, 0, cntarr[0], connect, cntarr, ans);
  rep(i, n) { cout << ans[i] << " "; }
  cout << endl;
}
Problem p02936 (C++)

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

#define F first
#define S second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 100010, M = 2 * N;

int n, m;
int h[N], e[M], ne[M], idx;
int w[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int u, int fa, int s) {
  for (int i = h[u]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;

    w[j] += s;
    dfs(j, u, w[j]);
  }
}

int main() {
  memset(h, -1, sizeof h);

  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }

  for (int i = 0; i < m; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    w[p] += x;
  }

  dfs(1, -1, w[1]);

  for (int i = 1; i <= n; i++)
    cout << w[i] << ' ';
  puts("");
  return 0;
}

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

#define F first
#define S second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010, M = 2 * N;

int n, m;
int h[N], e[M], ne[M], idx;
int w[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int u, int fa, int s) {
  for (int i = h[u]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;

    w[j] += s;
    dfs(j, u, w[j]);
  }
}

int main() {
  memset(h, -1, sizeof h);

  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }

  for (int i = 0; i < m; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    w[p] += x;
  }

  dfs(1, -1, w[1]);

  for (int i = 1; i <= n; i++)
    cout << w[i] << ' ';
  puts("");
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> g[100001];
ll score[100001];

void dfs(int v, int pa, ll sum) {
  score[v] += sum;
  for (int i = 0; i < g[v].size(); i++) {
    if (g[v][i] != pa) {
      dfs(g[v][i], v, score[v]);
    }
  }
}
int main() {
  int n, q, a, b, p;
  ll x;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    scanf("%d %lld", &p, &x);
    score[p] += x;
  }
  dfs(1, -1, 0LL);
  for (int i = 1; i <= n; i++) {
    printf("%lld ", score[i]);
  }
  printf("\n");
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
vector<int> g[200001];
ll score[200001];

void dfs(int v, int pa, ll sum) {
  score[v] += sum;
  for (int i = 0; i < g[v].size(); i++) {
    if (g[v][i] != pa) {
      dfs(g[v][i], v, score[v]);
    }
  }
}
int main() {
  int n, q, a, b, p;
  ll x;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    scanf("%d %lld", &p, &x);
    score[p] += x;
  }
  dfs(1, -1, 0LL);
  for (int i = 1; i <= n; i++) {
    printf("%lld ", score[i]);
  }
  printf("\n");
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

const int MAX_N = 100000;
int N, Q;
vector<int> table[MAX_N];
int cnt[MAX_N];
int ret[MAX_N];

void dfs(int node, int par, int score) {
  score += cnt[node];
  ret[node] = score;

  for (int next : table[node]) {
    if (next == par)
      continue;
    dfs(next, node, score);
  }
}

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

  for (int q = 0; q < Q; q++) {
    int p, x;
    cin >> p >> x;
    p--;
    cnt[p] += x;
  }

  dfs(0, -1, 0);

  for (int i = 0; i < N; i++) {
    cout << ret[i];
    if (i != N - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

const int MAX_N = 200000;
int N, Q;
vector<int> table[MAX_N];
int cnt[MAX_N];
int ret[MAX_N];

void dfs(int node, int par, int score) {
  score += cnt[node];
  ret[node] = score;

  for (int next : table[node]) {
    if (next == par)
      continue;
    dfs(next, node, score);
  }
}

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

  for (int q = 0; q < Q; q++) {
    int p, x;
    cin >> p >> x;
    p--;
    cnt[p] += x;
  }

  dfs(0, -1, 0);

  for (int i = 0; i < N; i++) {
    cout << ret[i];
    if (i != N - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}
Problem p02936 (C++)

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

const int maxn = 1e5 + 7;
int n, q, a, b, dfn[maxn], in[maxn], out[maxn], del[maxn], sum, ans[maxn];
vector<int> to[maxn];

void dfs(int x, int f) {
  dfn[++dfn[0]] = x;
  in[x] = dfn[0];
  for (int i = 0; i < to[x].size(); ++i) {
    int t = to[x][i];
    if (t == f)
      continue;
    dfs(t, x);
  }
  out[x] = dfn[0];
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i < n; ++i) {
    scanf("%d %d", &a, &b);
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(1, 0);
  for (int i = 1; i <= q; ++i) {
    scanf("%d %d", &a, &b);
    del[in[a]] += b;
    del[out[a] + 1] -= b;
  }
  for (int i = 1; i <= n; ++i) {
    sum += del[i];
    ans[dfn[i]] = sum;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", ans[i]);
}

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

const int maxn = 2e5 + 7;
int n, q, a, b, dfn[maxn], in[maxn], out[maxn], del[maxn], sum, ans[maxn];
vector<int> to[maxn];

void dfs(int x, int f) {
  dfn[++dfn[0]] = x;
  in[x] = dfn[0];
  for (int i = 0; i < to[x].size(); ++i) {
    int t = to[x][i];
    if (t == f)
      continue;
    dfs(t, x);
  }
  out[x] = dfn[0];
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i < n; ++i) {
    scanf("%d %d", &a, &b);
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(1, 0);
  for (int i = 1; i <= q; ++i) {
    scanf("%d %d", &a, &b);
    del[in[a]] += b;
    del[out[a] + 1] -= b;
  }
  for (int i = 1; i <= n; ++i) {
    sum += del[i];
    ans[dfn[i]] = sum;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", ans[i]);
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int n, q;
vector<vector<int>> G;
vector<ll> a, V;

void dfs(int v, ll val) {
  V[v] = a[v] + val;
  for (const auto nv : G[v])
    if (V[nv] == 0)
      dfs(nv, V[v]);
  return;
}

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

  cin >> n >> q;
  G.resize(n);
  V.assign(n, 0);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  a.resize(n, 0);
  rep(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    a[x] += y;
  }

  dfs(0, 0ll);

  for (const auto &v : V)
    cout << v << " ";
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int n, q;
vector<vector<int>> G;
vector<ll> a, V;

void dfs(int v, int from) {
  V[v] = a[v] + V[from];
  for (const auto nv : G[v]) {
    if (nv == from)
      continue;
    dfs(nv, v);
  }
  return;
}

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

  cin >> n >> q;
  G.resize(n);
  V.assign(n, 0);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  a.resize(n, 0);
  rep(i, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    a[x] += y;
  }

  dfs(0, 0ll);

  for (const auto &v : V)
    cout << v << " ";
  cout << endl;
}
Problem p02936 (C++)

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

vector<int> adj[100005];

int label[200005];
int smd[200005];

int n, q;

void dfs_out(int node, int last, int s) {
  smd[node] = label[node] + s;
  for (int i = 0; i < adj[node].size(); i++) {
    int other = adj[node][i];
    if (other == last)
      continue;
    dfs_out(other, node, smd[node]);
  }
}

int main() {

  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    int nodenum, val;
    cin >> nodenum >> val;
    label[nodenum] += val;
  }

  dfs_out(1, -1, 0);

  for (int i = 1; i <= n; i++) {
    cout << smd[i] << ' ';
  }
}

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

vector<int> adj[200005];

int label[200005];
int smd[200005];

int n, q;

void dfs_out(int node, int last, int s) {
  smd[node] = label[node] + s;
  for (int i = 0; i < adj[node].size(); i++) {
    int other = adj[node][i];
    if (other == last)
      continue;
    dfs_out(other, node, smd[node]);
  }
}

int main() {

  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    int nodenum, val;
    cin >> nodenum >> val;
    label[nodenum] += val;
  }

  dfs_out(1, -1, 0);

  for (int i = 1; i <= n; i++) {
    cout << smd[i] << ' ';
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
int N;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
  for (int e : to[v]) {
    if (p == e) {
      continue;
    }
    ans[e] += ans[v];
    dfs(e, v);
  }
}

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

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int N;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
  for (int e : to[v]) {
    if (p == e) {
      continue;
    }
    ans[e] += ans[v];
    dfs(e, v);
  }
}

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