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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << "-1" << endl;                                                        \
  return 0
#define MM << " " <<
#define Endl endl

signed main() {
  const int MEM = 60;
  int N, Q;
  int A[MEM], B[MEM];
  int p[MEM], x[MEM];
  vector<int> G[MEM];
  cin >> N >> Q;
  REP(i, N - 1) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  REP(i, Q) {
    cin >> p[i] >> x[i];
    p[i]--;
  }

  queue<int> P;
  P.push(0);
  int d[214514] = {};
  bool visit[214514] = {};
  REP(i, Q) { d[p[i]] += x[i]; }
  while (!P.empty()) {
    int T = P.front();
    P.pop();
    visit[T] = true;
    REP(i, G[T].size()) {
      if (!visit[G[T][i]]) {
        P.push(G[T][i]);
        d[G[T][i]] += d[T];
      }
    }
  }
  REP(i, N) {
    if (i != 0)
      cout << " ";
    cout << d[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << "-1" << endl;                                                        \
  return 0
#define MM << " " <<
#define Endl endl

signed main() {
  const int MEM = 214514;
  int N, Q;
  int A[MEM], B[MEM];
  int p[MEM], x[MEM];
  vector<int> G[MEM];
  cin >> N >> Q;
  REP(i, N - 1) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back(B[i]);
    G[B[i]].push_back(A[i]);
  }
  REP(i, Q) {
    cin >> p[i] >> x[i];
    p[i]--;
  }

  queue<int> P;
  P.push(0);
  int d[214514] = {};
  bool visit[214514] = {};
  REP(i, Q) { d[p[i]] += x[i]; }
  while (!P.empty()) {
    int T = P.front();
    P.pop();
    visit[T] = true;
    REP(i, G[T].size()) {
      if (!visit[G[T][i]]) {
        P.push(G[T][i]);
        d[G[T][i]] += d[T];
      }
    }
  }
  REP(i, N) {
    if (i != 0)
      cout << " ";
    cout << d[i];
  }
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

vector<int> graph[250000];
bool besucht[250000];
ll query[250000];
ll res[250000];

void dfs(int p, ll sum) {
  // if( besucht[p] ) return;
  besucht[p] = true;
  res[p] = sum;
  for (auto next : graph[p]) {
    dfs(next, sum + query[next]);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (int i = 0; i < q; ++i) {
    int p;
    ll x;
    cin >> p >> x;
    query[p - 1] += x;
  }
  dfs(0, query[0]);
  for (int i = 0; i < n; ++i) {
    cout << res[i] << ' ';
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

vector<int> graph[250000];
bool besucht[250000];
ll query[250000];
ll res[250000];

void dfs(int p, ll sum) {
  // if( besucht[p] ) return;
  besucht[p] = true;
  res[p] = sum;
  for (auto next : graph[p]) {
    dfs(next, sum + query[next]);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
  }
  for (int i = 0; i < q; ++i) {
    int p;
    ll x;
    cin >> p >> x;
    query[p - 1] += x;
  }
  dfs(0, query[0]);
  for (int i = 0; i < n; ++i) {
    cout << res[i] << ' ';
  }
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f3f;
const int maxn = 1e5 + 500;
ll T, n, m;
vector<ll> num[maxn];
ll numb[maxn];
ll f[maxn];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    num[a].push_back(b);
    f[b] = a;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    numb[a] += b;
  }
  ll sum = 0;
  for (int i = 1; i < n + 1; i++) {
    numb[i] += numb[f[i]];
    cout << numb[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f3f;
const int maxn = 2e5 + 500;
ll T, n, m;
vector<ll> num[maxn];
ll numb[maxn];
ll f[maxn];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    num[a].push_back(b);
    f[b] = a;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    numb[a] += b;
  }
  ll sum = 0;
  for (int i = 1; i < n + 1; i++) {
    numb[i] += numb[f[i]];
    cout << numb[i] << endl;
  }
  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;
vector<vector<int>> v(1000000);
vector<int> ans(1000000, 0);

void rec(int i, int j = -1) {
  for (int k : v[i]) {
    if (k == j)
      continue;
    ans[k] += ans[i];
    rec(k, j);
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  v.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  rec(0);
  rep(i, n) cout << ans[i] << " ";
}

int main() {
  solve();
  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<vector<int>> v(1000000);
vector<int> ans(1000000, 0);

void rec(int i, int j = -1) {
  for (int k : v[i]) {
    if (k == j)
      continue;
    ans[k] += ans[i];
    rec(k, i);
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  v.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  rec(0);
  rep(i, n) cout << ans[i] << " ";
}

int main() {
  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++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
const int MAX = 510000;

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
stack<int> ANSAN[200002];
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int ANS;

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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  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++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
const int MAX = 510000;

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
stack<int> ANSAN[200002];
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int ANS;

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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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] << " ";
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
using namespace std;
#include <bits/stdc++.h>
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it)                                                              \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

const int MAXN = 1e5 + 1;
long long f[MAXN];
vector<int> g[MAXN];
int n, m;
void dfs(int u, int parent) {
  TR(g[u], it) {
    int v = *it;
    if (v == parent)
      continue;
    f[v] += f[u];
    dfs(v, u);
  }
}

int main() {
#define TASK "NAME"
  // freopen(TASK".inp","r",stdin);
  // freopen(TASK".out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    f[i] = 0;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= m; ++i) {
    int u, p;
    cin >> u >> p;
    f[u] += p;
  }
  dfs(1, -1);
  for (int i = 1; i <= n; ++i)
    cout << f[i] << " ";

  return 0;
}


Fixed submission:
using namespace std;
#include <bits/stdc++.h>
#define st first
#define nd second
#define FORE(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define FORD(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define TR(c, it)                                                              \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

const int MAXN = 2e5 + 1;
long long f[MAXN];
vector<int> g[MAXN];
int n, m;
void dfs(int u, int parent) {
  TR(g[u], it) {
    int v = *it;
    if (v == parent)
      continue;
    f[v] += f[u];
    dfs(v, u);
  }
}

int main() {
#define TASK "NAME"
  // freopen(TASK".inp","r",stdin);
  // freopen(TASK".out","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    f[i] = 0;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= m; ++i) {
    int u, p;
    cin >> u >> p;
    f[u] += p;
  }
  dfs(1, -1);
  for (int i = 1; i <= n; ++i)
    cout << f[i] << " ";

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Code author: Saurabh Singhal
Code copy karne vaale tera muh kaala
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ppb pop_back

using namespace std;
typedef long long int ll;
typedef vector<int> vi_t;
typedef vector<ll> vll_t;
int t = 1;
vll_t a, b;
vector<int> grp[200001];
int val[100001];
int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  //    freopen("input.txt", "r", stdin);
  //    // for writing output to output.txt
  //    freopen("output.txt", "w", stdout);
  // #endif
  memset(val, 0, sizeof(val));
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    grp[u].push_back(v);
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    val[p] += x;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < grp[i].size(); j++) {
      val[grp[i][j]] += val[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << val[i] << " ";
  }
  cout << endl;
}

Fixed submission:
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Code author: Saurabh Singhal
Code copy karne vaale tera muh kaala
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ppb pop_back

using namespace std;
typedef long long int ll;
typedef vector<int> vi_t;
typedef vector<ll> vll_t;
int t = 1;
vll_t a, b;
vector<int> grp[200005];
int val[200005];
int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  //    freopen("input.txt", "r", stdin);
  //    // for writing output to output.txt
  //    freopen("output.txt", "w", stdout);
  // #endif
  memset(val, 0, sizeof(val));
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    grp[u].push_back(v);
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    val[p] += x;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < grp[i].size(); j++) {
      val[grp[i][j]] += val[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << val[i] << " ";
  }
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

vector<int> to[100005];
int cnt[100005], ans[100005];

void dfs(int x, int fa, int sum) {
  // cout<<sum<<' '<<x<<endl;
  sum += cnt[x];
  ans[x] = sum;
  if (to[x].size() == 1 && fa != 0)
    return;
  for (int i = 0; i < to[x].size(); i++) {
    int v = to[x][i];
    if (v == fa)
      continue;
    dfs(v, x, sum);
  }
  return;
}

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

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

void dfs(int x, int fa, int sum) {
  // cout<<sum<<' '<<x<<endl;
  sum += cnt[x];
  ans[x] = sum;
  if (to[x].size() == 1 && fa != 0)
    return;
  for (int i = 0; i < to[x].size(); i++) {
    int v = to[x][i];
    if (v == fa)
      continue;
    dfs(v, x, sum);
  }
  return;
}

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

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

using namespace std;

vector<int> g[20000 + 5];
int a[20000 + 5];

int f(int u, int p, int sum) {
  a[u] += sum;

  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == p) {
      continue;
    }
    f(v, u, a[u]);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    a[x - 1] += y;
  }

  f(0, -1, 0);
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}

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

using namespace std;

vector<int> g[200000 + 1];
int a[200000 + 1];

int f(int u, int p, int sum) {
  a[u] += sum;

  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == p) {
      continue;
    }
    f(v, u, a[u]);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    a[x - 1] += y;
  }

  f(0, -1, 0);
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define lli long long int
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q, i, j;
  cin >> n >> q;
  vector<vector<int>> adj;
  adj.resize(n);
  lli a, b;
  for (i = 0; i < n - 1; i++) {
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  vector<lli> val;
  val.resize(n);
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    val[a - 1] += b;
  }
  vector<int> par;
  for (i = 0; i < n; i++)
    par[i] = -1;
  par[0] = 0;
  queue<int> q1;
  q1.push(0);
  while (!q1.empty()) {
    int x = q1.front();
    q1.pop();
    for (i = 0; i < adj[x].size(); i++) {
      int y = adj[x][i];
      if (par[y] == -1) {
        par[y] = x;
        val[y] += val[x];
        q1.push(y);
      }
    }
  }
  for (i = 0; i < n; i++)
    cout << val[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define lli long long int
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q, i, j;
  cin >> n >> q;
  vector<vector<int>> adj;
  adj.resize(n);
  lli a, b;
  for (i = 0; i < n - 1; i++) {
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj[b - 1].push_back(a - 1);
  }
  vector<lli> val;
  val.resize(n);
  for (i = 0; i < q; i++) {
    cin >> a >> b;
    val[a - 1] += b;
  }
  vector<int> par;
  par.resize(n);
  for (i = 0; i < n; i++)
    par[i] = -1;
  par[0] = 0;
  queue<int> q1;
  q1.push(0);
  while (!q1.empty()) {
    int x = q1.front();
    q1.pop();
    for (i = 0; i < adj[x].size(); i++) {
      int y = adj[x][i];
      if (par[y] == -1) {
        par[y] = x;
        val[y] += val[x];
        q1.push(y);
      }
    }
  }
  for (i = 0; i < n; i++)
    cout << val[i] << " ";
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int SIZE = 100024;
int N, Q;
int p[SIZE];
long long cnt[SIZE];
long long sum[SIZE];

long long Solve(int t) {
  if (t == 1)
    return cnt[1];
  if (sum[t] < 0) {
    sum[t] = Solve(p[t]) + cnt[t];
  }
  return sum[t];
}

int main() {
  while (cin >> N >> Q) {
    memset(cnt, 0, sizeof(cnt));
    memset(sum, -1, sizeof(sum));
    for (int i = 0; i < N - 1; ++i) {
      int a, b;
      cin >> a >> b;
      p[b] = a;
    }
    for (int i = 0; i < Q; ++i) {
      int t, x;
      cin >> t >> x;
      cnt[t] += x;
    }
    for (int i = 1; i <= N; ++i) {
      if (i != 1)
        cout << " ";
      cout << Solve(i);
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int SIZE = 200024;
int N, Q;
int p[SIZE];
long long cnt[SIZE];
long long sum[SIZE];

long long Solve(int t) {
  if (t == 1)
    return cnt[1];
  if (sum[t] < 0) {
    sum[t] = Solve(p[t]) + cnt[t];
  }
  return sum[t];
}

int main() {
  while (cin >> N >> Q) {
    memset(cnt, 0, sizeof(cnt));
    memset(sum, -1, sizeof(sum));
    for (int i = 0; i < N - 1; ++i) {
      int a, b;
      cin >> a >> b;
      p[b] = a;
    }
    for (int i = 0; i < Q; ++i) {
      int t, x;
      cin >> t >> x;
      cnt[t] += x;
    }
    for (int i = 1; i <= N; ++i) {
      if (i != 1)
        cout << " ";
      cout << Solve(i);
    }
    cout << endl;
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
vi adj[100005];
int a[100005] = {0}, visited[100005] = {0};

void dfs(int node) {
  visited[node] = 1;

  for (int i = 0; i < adj[node].size(); i++) {
    if (visited[adj[node][i]] == 0) {
      a[adj[node][i]] += a[node];
      dfs(adj[node][i]);
    }
  }
}
int main() {
  ios

      int n,
      q, x, y;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  while (q--) {
    cin >> x >> y;
    a[x] += y;
  }

  dfs(1);

  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
vi adj[200005];
int a[200005] = {0}, visited[200005] = {0};

void dfs(int node) {
  visited[node] = 1;

  for (int i = 0; i < adj[node].size(); i++) {
    if (visited[adj[node][i]] == 0) {
      a[adj[node][i]] += a[node];
      dfs(adj[node][i]);
    }
  }
}
int main() {
  ios

      int n,
      q, x, y;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  while (q--) {
    cin >> x >> y;
    a[x] += y;
  }

  dfs(1);

  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
}
Problem p02936 (C++)

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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

int n, q;
vector<int> graph[MAX_N];

ll lazy[MAX_N];
ll actual[MAX_N];

int main(void) {
  // Here your code !
  scanf("%d %d", &n, &q);

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

    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  for (int i = 1; i <= q; ++i) {
    int p;
    ll x;
    scanf("%d %lld", &p, &x);

    lazy[p] += x;
  }

  queue<tuple<int, int, ll>> que;

  que.push(make_tuple(1, 0, 0LL));

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    int node = get<0>(cur);
    int parent = get<1>(cur);
    ll acc = get<2>(cur);

    acc += lazy[node];

    actual[node] += acc;

    for (auto &e : graph[node]) {
      if (e == parent)
        continue;

      que.push(make_tuple(e, node, acc));
    }
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld ", actual[i]);
  }
  printf("\n");

  return 0;
}


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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(2e5 + 5);

int n, q;
vector<int> graph[MAX_N];

ll lazy[MAX_N];
ll actual[MAX_N];

int main(void) {
  // Here your code !
  scanf("%d %d", &n, &q);

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

    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  for (int i = 1; i <= q; ++i) {
    int p;
    ll x;
    scanf("%d %lld", &p, &x);

    lazy[p] += x;
  }

  queue<tuple<int, int, ll>> que;

  que.push(make_tuple(1, 0, 0LL));

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    int node = get<0>(cur);
    int parent = get<1>(cur);
    ll acc = get<2>(cur);

    acc += lazy[node];

    actual[node] += acc;

    for (auto &e : graph[node]) {
      if (e == parent)
        continue;

      que.push(make_tuple(e, node, acc));
    }
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld ", actual[i]);
  }
  printf("\n");

  return 0;
}
Problem p02936 (C++)

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

using namespace std;

vector<int> graph[100000];
vector<int> counter(100000, 0);
vector<int> ans(100000, 0);
void dfs(int n, int c) {
  ans[n] = c + counter[n];
  for (auto t : graph[n]) {
    dfs(t, ans[n]);
  }
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
  }
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    counter[p] += x;
  }
  set<int> in;
  for (int i = 0; i < N; i++)
    in.insert(i);
  for (int i = 0; i < N; i++) {
    for (auto ee : graph[i]) {
      in.erase(ee);
    }
  }
  for (int i = 0; i < N; i++) {
    if (in.count(i) == 0)
      continue;
    dfs(i, 0);
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;

  return 0;
}


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

using namespace std;

vector<int> graph[200000];
vector<int> counter(200000, 0);
vector<int> ans(200000, 0);
void dfs(int n, int c) {
  ans[n] = c + counter[n];
  for (auto t : graph[n]) {
    dfs(t, ans[n]);
  }
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
  }
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    counter[p] += x;
  }
  set<int> in;
  for (int i = 0; i < N; i++)
    in.insert(i);
  for (int i = 0; i < N; i++) {
    for (auto ee : graph[i]) {
      in.erase(ee);
    }
  }
  for (int i = 0; i < N; i++) {
    if (in.count(i) == 0)
      continue;
    dfs(i, 0);
  }
  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;
typedef long long ll;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)

ll N, Q;
vector<ll> G[200000];
vector<ll> result(20000, 0);
vector<bool> F(200000, false);

void dfs(ll v, ll p = -1) {
  if (!F[v]) {
    F[v] = true;
    if (p != -1) {
      result[v] += result[p];
    }
    rep(i, G[v].size()) {
      if (!F[G[v][i]])
        dfs(G[v][i], v);
    }
  }
}

int main() {
  cin >> N >> Q;
  F.resize(N);
  rep(i, N - 1) {
    ll p, c;
    cin >> p >> c;
    G[p - 1].push_back(c - 1);
    G[c - 1].push_back(p - 1);
  }

  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    result[p - 1] += x;
  }
  dfs(0);
  rep(i, N) cout << result[i] << ' ';
  cout << endl;
  return 0;
}


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

ll N, Q;
vector<ll> G[200000];
vector<ll> result(200000, 0);
vector<bool> F(200000, false);

void dfs(ll v, ll p = -1) {
  if (!F[v]) {
    F[v] = true;
    if (p != -1) {
      result[v] += result[p];
    }
    rep(i, G[v].size()) {
      if (!F[G[v][i]])
        dfs(G[v][i], v);
    }
  }
}

int main() {
  cin >> N >> Q;
  F.resize(N);
  rep(i, N - 1) {
    ll p, c;
    cin >> p >> c;
    G[p - 1].push_back(c - 1);
    G[c - 1].push_back(p - 1);
  }

  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    result[p - 1] += x;
  }
  dfs(0);
  rep(i, N) cout << result[i] << ' ';
  cout << endl;
  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;

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);
  }
  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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, // less_equal
             tree_order_statistics_node_update>
    oset;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 2e3 + 2;
#define sz(x) (int)(x).size()
#define f first

vector<int> g[MAXN];
ll prop[MAXN], val[MAXN];

void dfs(int u, int p, ll cnt) {
  val[u] = cnt;

  for (auto &k : g[u]) {
    if (k != p) {
      dfs(k, u, cnt + prop[k]);
    }
  }
}

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

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

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    prop[p] += x;
  }
  dfs(1, -1, prop[1]);

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


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, // less_equal
             tree_order_statistics_node_update>
    oset;
const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int MAXK = 2e3 + 2;
#define sz(x) (int)(x).size()
#define f first

vector<int> g[MAXN];
ll prop[MAXN], val[MAXN];

void dfs(int u, int p, ll cnt) {
  val[u] = cnt;

  for (auto &k : g[u]) {
    if (k != p) {
      dfs(k, u, cnt + prop[k]);
    }
  }
}

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

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

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    prop[p] += x;
  }
  dfs(1, -1, prop[1]);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M (1000000007)
#define N (100010)
int n, Q, ans[N];
vector<int> G[N], q[N];

void dfs(int x, int fa, int sum) {
  for (int i = 0; i < q[x].size(); i++)
    sum += q[x][i];
  ans[x] = sum;
  for (int i = 0; i < G[x].size(); i++) {
    int y = G[x][i];
    if (y == fa)
      continue;
    dfs(y, x, sum);
  }
}

int main() {
  scanf("%d%d", &n, &Q);
  int x, y;
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= Q; ++i) {
    scanf("%d%d", &x, &y);
    q[x].push_back(y);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d ", ans[i]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M (1000000007)
#define N (200010)
int n, Q, ans[N];
vector<int> G[N], q[N];

void dfs(int x, int fa, int sum) {
  for (int i = 0; i < q[x].size(); i++)
    sum += q[x][i];
  ans[x] = sum;
  for (int i = 0; i < G[x].size(); i++) {
    int y = G[x][i];
    if (y == fa)
      continue;
    dfs(y, x, sum);
  }
}

int main() {
  scanf("%d%d", &n, &Q);
  int x, y;
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  for (int i = 1; i <= Q; ++i) {
    scanf("%d%d", &x, &y);
    q[x].push_back(y);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d ", ans[i]);
}
Problem p02936 (C++)

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

using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAXQ = 1e5 + 10;

int n, q;
int a[MAXN], b[MAXN], p[MAXQ], x[MAXQ];
bool visited[MAXN];
ll cnt[MAXN];
vector<int> graph[MAXN];

void dfs(int cur) {
  visited[cur] = true;
  for (int i = 0; i < graph[cur].size(); i++) {
    int next = graph[cur][i];
    if (!visited[next]) {
      cnt[next] += cnt[cur];
      dfs(next);
    }
  }
}

void solve() {
  for (int i = 0; i < n - 1; i++) {
    graph[a[i]].push_back(b[i]);
    graph[b[i]].push_back(a[i]);
  }
  for (int i = 0; i < q; i++) {
    cnt[p[i]] += x[i];
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << cnt[i];
  }
  cout << endl;
}

int main() {
  cin >> n >> 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]--;
  }
  solve();
}


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

using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
const int MAXQ = 2e5 + 10;

int n, q;
int a[MAXN], b[MAXN], p[MAXQ], x[MAXQ];
bool visited[MAXN];
ll cnt[MAXN];
vector<int> graph[MAXN];

void dfs(int cur) {
  visited[cur] = true;
  for (int i = 0; i < graph[cur].size(); i++) {
    int next = graph[cur][i];
    if (!visited[next]) {
      cnt[next] += cnt[cur];
      dfs(next);
    }
  }
}

void solve() {
  for (int i = 0; i < n - 1; i++) {
    graph[a[i]].push_back(b[i]);
    graph[b[i]].push_back(a[i]);
  }
  for (int i = 0; i < q; i++) {
    cnt[p[i]] += x[i];
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << cnt[i];
  }
  cout << endl;
}

int main() {
  cin >> n >> 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]--;
  }
  solve();
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long int ll;

#define EPS (1e-7)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<vector<ll>> graph(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<ll> cost(n, 0);
  for (ll i = 0; i < q; i++) {
    ll p, x;
    cin >> p >> x;
    p--;
    cost[p] += x;
  }
  vector<bool> seen(n, false);
  vector<ll> d(n, 0);
  stack<ll> st;
  st.push(0);
  d[0] = cost[0];
  while (!st.empty()) {
    ll now = st.top();
    seen[now] = true;
    st.pop();
    for (auto v : graph[now]) {
      if (!seen[v]) {
        st.push(v);
        d[v] += d[now] + cost[v];
      }
    }
    seen[now] = false;
  }
  for (auto ans : d) {
    cout << ans << ' ';
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long int ll;

#define EPS (1e-7)
#define INF (1 << 30)
#define LLINF (1LL << 60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<vector<ll>> graph(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<ll> cost(n, 0);
  for (ll i = 0; i < q; i++) {
    ll p, x;
    cin >> p >> x;
    p--;
    cost[p] += x;
  }
  vector<bool> seen(n, false);
  vector<ll> d(n, 0);
  stack<ll> st;
  st.push(0);
  d[0] = cost[0];
  while (!st.empty()) {
    ll now = st.top();
    seen[now] = true;
    st.pop();
    for (auto v : graph[now]) {
      if (!seen[v]) {
        st.push(v);
        d[v] += d[now] + cost[v];
      }
    }
  }
  for (auto ans : d) {
    cout << ans << ' ';
  }
  cout << endl;
}
Problem p02936 (C++)

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

using Graph = vector<vector<int>>;

Graph graph(20010);
vector<int> score(20010, 0);

void dfs(int cur, int prev) {
  score[cur] += score[prev];
  // 各頂点にスコアを加算する
  for (auto next : graph.at(cur)) {
    if (next == prev)
      continue;
    dfs(next, cur);
  }
}

int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    // 辺の情報から無向グラフを作成
    graph.at(a).push_back(b);
    graph.at(b).push_back(a);
  }

  for (int i = 0; i < Q; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    score.at(p) += x;
  }

  dfs(1, 0);

  for (int i = 1; i <= N; i++) {
    printf("%d ", score.at(i));
  }
  return 0;
}


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

using Graph = vector<vector<int>>;

Graph graph(200100);
vector<int> score(200100, 0);

void dfs(int cur, int prev) {
  score[cur] += score[prev];
  // 各頂点にスコアを加算する
  for (auto next : graph.at(cur)) {
    if (next == prev)
      continue;
    dfs(next, cur);
  }
}

int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    // 辺の情報から無向グラフを作成
    graph.at(a).push_back(b);
    graph.at(b).push_back(a);
  }

  for (int i = 0; i < Q; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    score.at(p) += x;
  }

  dfs(1, 0);

  for (int i = 1; i <= N; i++) {
    printf("%d ", score.at(i));
  }
  return 0;
}
Problem p02936 (C++)

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

int N, Q;
ll tr[101010];
vector<int> g[101010];

void solve(int v, int p, ll sum) {
  for (int u : g[v]) {
    if (u == p)
      continue;
    solve(u, v, sum + tr[v]);
  }
  tr[v] += sum;
}

int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    tr[p] += x;
  }

  solve(0, -1, 0);
  rep(i, N) {
    if (i)
      cout << " ";
    cout << tr[i];
  }
  cout << endl;
}


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

int N, Q;
ll tr[301010];
vector<int> g[301010];

void solve(int v, int p, ll sum) {
  for (int u : g[v]) {
    if (u == p)
      continue;
    solve(u, v, sum + tr[v]);
  }
  tr[v] += sum;
}

int main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    tr[p] += x;
  }

  solve(0, -1, 0);
  rep(i, N) {
    if (i)
      cout << " ";
    cout << tr[i];
  }
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
vector<vector<int>> v(100000);
vector<ll> ans(100001, 0LL);
vector<bool> used(100001);

void search(int p, int parent, ll sum) {
  used[p] = true;
  sum += ans[p];
  ans[p] = sum;
  for (int i = 0; i < v[p].size(); i++) {
    if (v[p][i] != parent && used[v[p][i]] == false) {
      search(v[p][i], p, sum);
    }
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    v[a - 1].pb(b - 1);
    v[b - 1].pb(a - 1);
  }
  for (int i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    ans[a - 1LL] += b;
  }
  search(0, 0, 0LL);

  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
vector<vector<int>> v(200001);
vector<ll> ans(200001, 0LL);
vector<bool> used(200001);

void search(int p, int parent, ll sum) {
  used[p] = true;
  sum += ans[p];
  ans[p] = sum;
  for (int i = 0; i < v[p].size(); i++) {
    if (v[p][i] != parent && used[v[p][i]] == false) {
      search(v[p][i], p, sum);
    }
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    v[a - 1].pb(b - 1);
    v[b - 1].pb(a - 1);
  }
  for (int i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    ans[a - 1LL] += b;
  }
  search(0, 0, 0LL);

  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;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
/* 4方向への隣接頂点への移動を表すベクトル */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const long long INF = 1LL << 60;
const int maxi = 1e6 + 10;

vector<int> value;
Graph G;
// d: 頂点 v の深さ
void dfs(int now, int parent, int score) {
  value[now] += score;
  // for(auto nv : G[now]) {
  //     if (nv == parent) continue;
  //     dfs(G,nv,now, value[now]);
  // }
  for (int i = 0; i < G[now].size(); i++) {
    if (G[now][i] == parent)
      continue;
    dfs(G[now][i], now, value[now]);
  }
}

int main() {
  // 頂点数と辺数
  int N, Q;
  cin >> N >> Q;

  G.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int u, x;
    cin >> u >> x;
    u--;
    value[u] += x;
  }

  dfs(0, -1, 0);

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

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
/* 4方向への隣接頂点への移動を表すベクトル */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

const long long INF = 1LL << 60;
const int maxi = 1e6 + 10;

vector<int> value;
Graph G;
// d: 頂点 v の深さ
void dfs(int now, int parent, int score) {
  value[now] += score;
  // for(auto nv : G[now]) {
  //     if (nv == parent) continue;
  //     dfs(G,nv,now, value[now]);
  // }
  for (int i = 0; i < G[now].size(); i++) {
    if (G[now][i] == parent)
      continue;
    dfs(G[now][i], now, value[now]);
  }
}

int main() {
  // 頂点数と辺数
  int N, Q;
  cin >> N >> Q;

  G.resize(N);
  value.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int u, x;
    cin >> u >> x;
    u--;
    value[u] += x;
  }

  dfs(0, -1, 0);

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

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

vector<int> G[200005];
ll val[20005];

void dfs(int p, int u) {
  for (auto v : G[u]) {
    if (v != p) {
      val[v] += val[u];
      dfs(u, v);
    }
  }
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    val[p] += x;
  }

  dfs(-1, 0);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << val[i];
  }
  cout << endl;

  return 0;
}


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

vector<int> G[200005];
ll val[200005];

void dfs(int p, int u) {
  for (auto v : G[u]) {
    if (v != p) {
      val[v] += val[u];
      dfs(u, v);
    }
  }
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    val[p] += x;
  }

  dfs(-1, 0);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << val[i];
  }
  cout << endl;

  return 0;
}