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

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10000000)

N, Q = [int(x) for x in input().split()]
# V = [set() for _ in range(N)]
V = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    # V[a].add(b)
    V[a].append(b)

C = [0] * N

for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p -= 1
    C[p] += x

ANS = [0] * N


def dfs(n, val):
    ANS[n] += val + C[n]
    for next in V[n]:
        dfs(next, val + C[n])


def main():
    dfs(0, 0)
    print(" ".join([str(x) for x in ANS]))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10000000)

N, Q = [int(x) for x in input().split()]
# V = [set() for _ in range(N)]
V = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    # V[a].add(b)
    V[a].append(b)

C = [0] * N

for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p -= 1
    C[p] += x

ANS = [0] * N


def dfs(n, val):
    ANS[n] += val + C[n]
    for next in V[n]:
        dfs(next, val + C[n])


def main():
    dfs(0, 0)
    print(" ".join([str(x) for x in ANS]))


if __name__ == "__main__":
    main()
Problem p02936 (C++)

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

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>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

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 (Python)

Buggy submission (status: Runtime Error):
def solve(N, Q, ABs, PXs):
    values = {i: 0 for i in range(Q + 1)}
    ans = [-1 for i in range(N + 1)]
    vertexes = {}
    for a, b in ABs:
        vertexes.setdefault(a, []).append(b)
        vertexes.setdefault(b, []).append(a)

    for p, x in PXs:
        values[p] += x

    queue = []
    queue.append((1, 0))
    while queue:
        index, parent_value = queue.pop()
        value = parent_value + values[index]
        ans[index] = value
        for child in vertexes[index]:
            if ans[child] == -1:
                queue.append((child, value))
    return " ".join([str(a) for a in ans[1:]])


if __name__ == "__main__":
    N, Q = tuple(map(int, input().split(" ")))
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N - 1)]
    PXs = [tuple(map(int, input().split(" "))) for _ in range(Q)]
    print(solve(N, Q, ABs, PXs))


Fixed submission:
def solve(N, Q, ABs, PXs):
    values = {i: 0 for i in range(N + 1)}
    ans = [-1 for i in range(N + 1)]
    vertexes = {}
    for a, b in ABs:
        vertexes.setdefault(a, []).append(b)
        vertexes.setdefault(b, []).append(a)

    for p, x in PXs:
        values[p] += x

    queue = []
    queue.append((1, 0))
    while queue:
        index, parent_value = queue.pop()
        value = parent_value + values[index]
        ans[index] = value
        for child in vertexes[index]:
            if ans[child] == -1:
                queue.append((child, value))
    return " ".join([str(a) for a in ans[1:]])


if __name__ == "__main__":
    N, Q = tuple(map(int, input().split(" ")))
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N - 1)]
    PXs = [tuple(map(int, input().split(" "))) for _ in range(Q)]
    print(solve(N, Q, ABs, PXs))
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int, int> P;
int main(void) {
  int n, q, i, a, b, v[100001], x, p1, x1;
  P pp[100001];
  scanf("%d %d", &n, &q);
  for (i = 0; i < n - 1; i++)
    scanf("%d %d", &pp[i].first, &pp[i].second);
  sort(pp, pp + n - 1);
  for (i = 1; i <= n; i++)
    v[i] = 0;
  for (i = 0; i < q; i++) {
    scanf("%d %d", &p1, &x1);
    v[p1] += x1;
  }
  for (i = 0; i < n - 1; i++) {
    a = pp[i].first;
    b = pp[i].second;
    x = v[a];
    v[b] += x;
  }
  printf("%d", v[1]);
  for (i = 2; i <= n; i++)
    printf(" %d", v[i]);
  printf("\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int, int> P;
int main(void) {
  int n, q, i, a, b, v[200001], x, p1, x1;
  P pp[200001];
  scanf("%d %d", &n, &q);
  for (i = 0; i < n - 1; i++)
    scanf("%d %d", &pp[i].first, &pp[i].second);
  sort(pp, pp + n - 1);
  for (i = 1; i <= n; i++)
    v[i] = 0;
  for (i = 0; i < q; i++) {
    scanf("%d %d", &p1, &x1);
    v[p1] += x1;
  }
  for (i = 0; i < n - 1; i++) {
    a = pp[i].first;
    b = pp[i].second;
    x = v[a];
    v[b] += x;
  }
  printf("%d", v[1]);
  for (i = 2; i <= n; i++)
    printf(" %d", v[i]);
  printf("\n");
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, q, tot;
int head[200005], fa[200005], ans[200005];
struct node {
  int to, net;
} e[200005];
void add(int x, int y) {
  e[++tot].to = y;
  e[tot].net = head[x];
  head[x] = tot;
}
int a[200005];
void dfs(int x, int last) {
  for (int i = head[x]; i; i = e[i].net) {
    int to = e[i].to;
    if (to == last)
      continue;
    fa[to] = x;
    ans[to] += ans[x];
    dfs(to, x);
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  for (int i = 1; i <= q; i++) {
    int x, v;
    scanf("%d%d", &x, &v);
    ans[x] += v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, q, tot;
int head[200005], fa[200005], ans[200005];
struct node {
  int to, net;
} e[400005];
void add(int x, int y) {
  e[++tot].to = y;
  e[tot].net = head[x];
  head[x] = tot;
}
int a[200005];
void dfs(int x, int last) {
  for (int i = head[x]; i; i = e[i].net) {
    int to = e[i].to;
    if (to == last)
      continue;
    fa[to] = x;
    ans[to] += ans[x];
    dfs(to, x);
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  for (int i = 1; i <= q; i++) {
    int x, v;
    scanf("%d%d", &x, &v);
    ans[x] += v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;

int n, q;
int p[200000];
vector<int> v[200000];
int ans[200000];

void dfs(int par, int from) {
  ans[from] += p[from] + ans[par];
  for (int x : v[from]) {
    if (x != par)
      dfs(from, x);
  }
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;

int n, q;
int p[200001];
vector<int> v[200001];
int ans[200001];

void dfs(int par, int from) {
  ans[from] += p[from] + ans[par];
  for (int x : v[from]) {
    if (x != par)
      dfs(from, x);
  }
}

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

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

int main() {
  int N;
  int Q;
  cin >> N;
  cin >> Q;
  vector<int> eda(N - 1);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    eda.at(b - 1) = a - 1;
  }
  vector<int> answer(N, 0);
  for (int i = 0; i < Q; i++) {
    int p;
    int x;
    cin >> p >> x;
    answer.at(p - 1) += x;
  }
  for (int i = 1; i < N; i++) {
    answer.at(i) += answer.at(eda.at(i));
  }
  for (int i = 0; i < N; i++) {
    cout << answer.at(i) << endl;
  }
}

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

int main() {
  int N;
  int Q;
  cin >> N;
  cin >> Q;
  vector<int> eda(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    eda.at(b - 1) = a - 1;
  }
  vector<int> answer(N, 0);
  for (int i = 0; i < Q; i++) {
    int p;
    int x;
    cin >> p >> x;
    answer.at(p - 1) += x;
  }
  for (int i = 1; i < N; i++) {
    answer.at(i) += answer.at(eda.at(i));
  }
  for (int i = 0; i < N; i++) {
    cout << answer.at(i) << endl;
  }
}
Problem p02936 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;

int x[100000] = {};
int p[100000];
int dp[100000];

int solve2(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 0)
    return dp[i] = x[0];
  return dp[i] = solve2(p[i]) + x[i];
}

void solve() {
  int n, q;
  cin >> n >> q;
  {
    vector<vector<int>> e(n);
    rep(i, n - 1) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      e[a].push_back(b);
      e[b].push_back(a);
    }
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()) {
      int qp = Q.front();
      Q.pop();
      rep(i, e[qp].size()) {
        if (p[qp] != -1)
          if (e[qp][i] == p[qp])
            continue;
        Q.push(e[qp][i]);
        p[e[qp][i]] = qp;
      }
    }
  }
  rep(i, q) {
    int pp, xx;
    cin >> pp >> xx;
    pp--;
    x[pp] += xx;
  }
  rep(i, n) { dp[i] = -1; }

  rep(i, n) { cout << solve2(i) << endl; }
}

int main() {

  solve();

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;

int x[200000] = {};
int p[200000];
int dp[200000];

int solve2(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 0)
    return dp[i] = x[0];
  return dp[i] = solve2(p[i]) + x[i];
}

void solve() {
  int n, q;
  cin >> n >> q;
  {
    vector<vector<int>> e(n);
    rep(i, n - 1) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      e[a].push_back(b);
      e[b].push_back(a);
    }
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()) {
      int qp = Q.front();
      Q.pop();
      rep(i, e[qp].size()) {
        if (p[qp] != -1)
          if (e[qp][i] == p[qp])
            continue;
        Q.push(e[qp][i]);
        p[e[qp][i]] = qp;
      }
    }
  }
  rep(i, q) {
    int pp, xx;
    cin >> pp >> xx;
    pp--;
    x[pp] += xx;
  }
  rep(i, n) { dp[i] = -1; }

  rep(i, n) { cout << solve2(i) << endl; }
}

int main() {

  solve();

  return 0;
}
Problem p02936 (C++)

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

std::vector<long> to[20005];
std::vector<long long> ans;

void dfs(int a, int p = -1) {
  // printf("a=%d\n",a);
  // printf("to[%d].size()=%ld\n",a,to[a].size());
  for (int i = 0; i < to[a].size(); i++) {
    if (to[a][i] == p) {
      continue;
    }
    ans[to[a][i]] += ans[a];
    // printf("ans[%ld] += %ld\n",to[a][i], ans[a]);
    dfs(to[a][i], a);
  }
  return;
}

int main() {

  long N, Q;
  scanf("%ld %ld", &N, &Q);
  // printf("N=%ld, Q=%ld\n",N,Q);
  ans.resize(200005);
  int temp1, temp2;

  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    temp1--;
    temp2--;
    // printf("tp[%d].push_back(%d)\n",temp1,temp2);
    to[temp1].push_back(temp2);
    to[temp2].push_back(temp1);
  }
  // printf("to[0][0]=%ld\n",to[0][0]);
  for (int i = 0; i < Q; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    temp1--;
    ans[temp1] += temp2;
  }
  dfs(0);

  // printf("============\n");
  printf("%llu", ans[0]);
  for (int i = 1; i < N; i++) {
    printf(" %llu", ans[i]);
  }
  printf("\n");

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <vector>

std::vector<long> to[200005];
std::vector<long long> ans;

void dfs(int a, int p = -1) {
  // printf("a=%d\n",a);
  // printf("to[%d].size()=%ld\n",a,to[a].size());
  for (int i = 0; i < to[a].size(); i++) {
    if (to[a][i] == p) {
      continue;
    }
    ans[to[a][i]] += ans[a];
    // printf("ans[%ld] += %ld\n",to[a][i], ans[a]);
    dfs(to[a][i], a);
  }
  return;
}

int main() {

  long N, Q;
  scanf("%ld %ld", &N, &Q);
  // printf("N=%ld, Q=%ld\n",N,Q);
  ans.resize(200005);
  int temp1, temp2;

  for (int i = 0; i < N - 1; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    temp1--;
    temp2--;
    // printf("tp[%d].push_back(%d)\n",temp1,temp2);
    to[temp1].push_back(temp2);
    to[temp2].push_back(temp1);
  }
  // printf("to[0][0]=%ld\n",to[0][0]);
  for (int i = 0; i < Q; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    temp1--;
    ans[temp1] += temp2;
  }
  dfs(0);

  // printf("============\n");
  printf("%llu", ans[0]);
  for (int i = 1; i < N; i++) {
    printf(" %llu", ans[i]);
  }
  printf("\n");

  return 0;
}
Problem p02936 (C++)

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

const int MAXN = 100 * 1000 + 20;
int n, q, a, b, num[MAXN], ans[MAXN];
vector<int> adj[MAXN];
bool vis[MAXN];

void dfs(int v, int sm) {
  vis[v] = true;
  sm += num[v];
  ans[v] = sm;
  for (int u : adj[v])
    if (!vis[u])
      dfs(u, sm);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> q;
  for (int i = 1; i < n; i++)
    cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    num[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}


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

const int MAXN = 200 * 1000 + 20;
int n, q, a, b, num[MAXN], ans[MAXN];
vector<int> adj[MAXN];
bool vis[MAXN];

void dfs(int v, int sm) {
  vis[v] = true;
  sm += num[v];
  ans[v] = sm;
  for (int u : adj[v])
    if (!vis[u])
      dfs(u, sm);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> q;
  for (int i = 1; i < n; i++)
    cin >> a >> b, adj[a].push_back(b), adj[b].push_back(a);
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    num[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << ' ';
  cout << '\n';
  return 0;
}
Problem p02936 (C++)

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

vector<bool> seen(200005, false);
vector<int> counter;
void dfs(vector<vector<int>> vec, int v) {
  seen.at(v) = true;
  for (int i = 0; i < vec.at(v).size(); i++) {
    if (seen.at(vec.at(v).at(i))) {
      continue;
    }
    counter[vec[v][i]] += counter[v];
    dfs(vec, vec.at(v).at(i));
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> to(N, vector<int>(0));

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

  counter.assign(N, 0);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    counter[p - 1] += x;
  }
  dfs(to, 0);

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


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

vector<bool> seen(200005, false);
vector<int> counter;
void dfs(vector<vector<int>> &vec, int v) {
  seen.at(v) = true;
  for (int i = 0; i < vec.at(v).size(); i++) {
    if (seen.at(vec.at(v).at(i))) {
      continue;
    }
    counter[vec[v][i]] += counter[v];
    dfs(vec, vec.at(v).at(i));
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> to(N, vector<int>(0));

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

  counter.assign(N, 0);
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    counter[p - 1] += x;
  }
  dfs(to, 0);

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

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

typedef long long ll;

vector<int> res(100001);

void dfs(int cp, vector<vector<int>> &v, vector<int> &myPoint, int sump) {
  int nsump = myPoint[cp] + sump;

  res[cp] = nsump;

  for (int i = 0; i < v[cp].size(); ++i) {
    int next = v[cp][i];
    dfs(next, v, myPoint, nsump);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> v(n + 1);

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

  vector<int> myPoint(n + 1);

  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    myPoint[a] += b;
  }

  dfs(1, v, myPoint, 0);

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;

vector<int> res(1000001);

void dfs(int cp, vector<vector<int>> &v, vector<int> &myPoint, int sump) {
  int nsump = myPoint[cp] + sump;

  res[cp] = nsump;

  for (int i = 0; i < v[cp].size(); ++i) {
    int next = v[cp][i];
    dfs(next, v, myPoint, nsump);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> v(n + 1);

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

  vector<int> myPoint(n + 1);

  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    myPoint[a] += b;
  }

  dfs(1, v, myPoint, 0);

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

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep_down(i, n) for (int i = n - 1; i >= 0; --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
const int INF = 1000000007;
int N, Q;
vector<int> T[100001];
ll X[100001];
ll Ans[100001];

void dfs(int num, ll value) {
  rep(i, T[num].size()) {
    int next = T[num][i];
    dfs(next, value + X[num]);
  }
  Ans[num] = value + X[num];
}

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

  cin >> N >> Q;
  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    T[a].push_back(b);
  }
  int p;
  ll x;
  rep(i, Q) {
    cin >> p >> x;
    p--;
    X[p] += x;
  }
  dfs(0, 0);
  rep(i, N) cout << Ans[i] << ' ';
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep_down(i, n) for (int i = n - 1; i >= 0; --i)
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
const int INF = 1000000007;
int N, Q;
vector<int> T[200002];
ll X[200002];
ll Ans[200002];

void dfs(int num, ll value) {
  rep(i, T[num].size()) {
    int next = T[num][i];
    dfs(next, value + X[num]);
  }
  Ans[num] = value + X[num];
}

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

  cin >> N >> Q;
  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    T[a].push_back(b);
  }
  int p;
  ll x;
  rep(i, Q) {
    cin >> p >> x;
    p--;
    X[p] += x;
  }
  dfs(0, 0);
  rep(i, N) cout << Ans[i] << ' ';
  return 0;
}
Problem p02936 (C++)

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

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define ll long long
#define el '\n'

using namespace std;

const int N = 2e5 + 10;

int ans[N];
vector<int> adl[N];
int n, q;

int lazy[N];

void dfs(int node, int p) {
  ans[node] = lazy[node];
  for (auto child : adl[node]) {
    if (child == p)
      continue;
    lazy[child] += lazy[node];
    dfs(child, node);
  }
}

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

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    adl[a].pb(b);
    adl[b].pb(a);
  }

  while (q--) {
    int node, val;
    cin >> node >> val;
    lazy[node] += val;
  }

  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << el;

  return 0;
}

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

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define ll long long
#define el '\n'

using namespace std;

const int N = 2e5 + 10;

int ans[N];
vector<int> adl[N];
int n, q;

int lazy[N];

void dfs(int node, int p) {
  ans[node] = lazy[node];
  for (auto child : adl[node]) {
    if (child == p)
      continue;
    lazy[child] += lazy[node];
    dfs(child, node);
  }
}

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

  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adl[a].pb(b);
    adl[b].pb(a);
  }

  while (q--) {
    int node, val;
    cin >> node >> val;
    lazy[node] += val;
  }

  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << el;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;

bool done[d5];
int ans[d5], sum[d5];
vector<int> adj[d5];

void dfs(int root, int extra) {
  done[root] = true;
  ans[root] = sum[root] + extra;
  for (auto u : adj[root]) {
    if (!done[u]) {
      dfs(u, extra + sum[root]);
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, u, v, q, x, p;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> p;
    sum[x - 1] += p;
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 200100;

bool done[d5];
int ans[d5], sum[d5];
vector<int> adj[d5];

void dfs(int root, int extra) {
  done[root] = true;
  ans[root] = sum[root] + extra;
  for (auto u : adj[root]) {
    if (!done[u]) {
      dfs(u, extra + sum[root]);
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, u, v, q, x, p;
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 0; i < q; i++) {
    cin >> x >> p;
    sum[x - 1] += p;
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI = acos(-1.0);
const int N = 200005;
const int MOD = 1e9 + 7;
const int inf = 1 << 28;
typedef long long ll;

vector<int> g[N];
int a[N], ans[N];
bool vis[N];

void dfs(int x, int c) {
  if (vis[x])
    return;
  vis[x] = 1;
  ans[x] += a[x] + c;

  for (int i = 0; i < g[x].size(); i++) {
    int v = g[x][i];
    dfs(v, ans[x]);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q, z, x;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> z >> x;
    g[z].push_back(x);
    g[x].push_back(z);
  }
  while (q--) {
    cin >> z >> x;
    a[z] += x;
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI = acos(-1.0);
const int N = 200005;
const int MOD = 1e9 + 7;
const int inf = 1 << 28;
typedef long long ll;

vector<int> g[N];
int a[N], ans[N];
bool vis[N];

void dfs(int x, int c) {
  if (vis[x])
    return;
  vis[x] = 1;
  ans[x] += a[x] + c;

  for (int i = 0; i < g[x].size(); i++) {
    int v = g[x][i];
    dfs(v, ans[x]);
  }
}
int main() {
  /* #ifndef ONLINE_JUDGE
       freopen("in.txt", "r", stdin);
       freopen("out.txt", "w", stdout);
   #endif*/
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q, z, x;
  cin >> n >> q;

  for (int i = 1; i < n; i++) {
    cin >> z >> x;
    g[z].push_back(x);
    g[x].push_back(z);
  }
  while (q--) {
    cin >> z >> x;
    a[z] += x;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

vector<int> v[100000];
int visited[100000] = {0};
int ans[100000] = {0};

void dfs(int x) {
  visited[x] += 1;
  for (int i = 0; i < v[x].size(); i++) {
    int nx = v[x][i];
    if (visited[nx] == 0) {
      ans[nx] += ans[x];
      dfs(nx);
    }
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  rep(i, Q) {
    int s, t;
    cin >> s >> t;
    s--;
    ans[s] += t;
  }
  dfs(0);
  rep(i, N) {
    cout << ans[i];
    if (i != N - 1)
      cout << " ";
    else
      cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

vector<int> v[200000];
int visited[200000] = {0};
int ans[200000] = {0};

void dfs(int x) {
  visited[x] += 1;
  for (int i = 0; i < v[x].size(); i++) {
    int nx = v[x][i];
    if (visited[nx] == 0) {
      ans[nx] += ans[x];
      dfs(nx);
    }
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  rep(i, Q) {
    int s, t;
    cin >> s >> t;
    s--;
    ans[s] += t;
  }
  dfs(0);
  rep(i, N) {
    cout << ans[i];
    if (i != N - 1)
      cout << " ";
    else
      cout << endl;
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void dfs(ll int u, ll int p, ll int c, vector<vector<ll int>> adj,
         vector<ll int> pcost, vector<ll int> &cost, ll int n) {
  cost[u] = pcost[u] + c;

  for (auto v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u, cost[u], adj, pcost, cost, n);
  }
  return;
}
int main() {
  ll int n, m, x, y;
  cin >> n >> m;
  vector<vector<ll int>> adj(n + 1);
  for (ll int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  vector<ll int> pcost(n + 1, 0);
  for (ll int i = 0; i < m; i++) {
    cin >> x >> y;
    pcost[x] += y;
  }
  vector<ll int> cost(n + 1, 0);
  dfs(1LL, -1LL, 0LL, adj, pcost, cost, n);

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

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void dfs(ll int u, ll int p, ll int c, vector<vector<ll int>> &adj,
         vector<ll int> &pcost, vector<ll int> &cost, ll int n) {
  cost[u] = pcost[u] + c;

  for (auto v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u, cost[u], adj, pcost, cost, n);
  }
  return;
}
int main() {
  ll int n, m, x, y;
  cin >> n >> m;
  vector<vector<ll int>> adj(n + 1);
  for (ll int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  vector<ll int> pcost(n + 1, 0);
  for (ll int i = 0; i < m; i++) {
    cin >> x >> y;
    pcost[x] += y;
  }
  vector<ll int> cost(n + 1, 0);
  dfs(1LL, -1LL, 0LL, adj, pcost, cost, n);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (ll)1e18
#define all(x) (x).begin(), (x).end()
#define print(a)                                                               \
  ;                                                                            \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << "\n";
#define mset(a)                                                                \
  ;                                                                            \
  memset(a, 0, sizeof(a));

const ll N = 1e5 + 5;
ll n, q;
vector<vi> v(N);
ll val[N];

void dfs(ll a, ll b) {
  val[a] += val[b];
  for (auto x : v[a]) {
    if (x != b)
      dfs(x, a);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  mset(val);
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  for (int i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    val[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << val[i] << " ";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (ll)1e18
#define all(x) (x).begin(), (x).end()
#define print(a)                                                               \
  ;                                                                            \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << "\n";
#define mset(a)                                                                \
  ;                                                                            \
  memset(a, 0, sizeof(a));

const ll N = 2e5 + 5;
ll n, q;
vector<vi> v(N);
ll val[N];

void dfs(ll a, ll b) {
  val[a] += val[b];
  for (auto x : v[a]) {
    if (x != b)
      dfs(x, a);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  mset(val);
  for (int i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }
  for (int i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    val[a] += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << val[i] << " ";
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int res[100005];
int c[100005];
bool vist[100005];
vector<int> adj[100005];

void bfs(int root, int sum) {
  queue<pii> q;
  q.push(make_pair(root, sum));
  while (!q.empty()) {
    pii e = q.front();
    q.pop();
    int u = e.first;
    int s = e.second;
    int m = adj[u].size();
    s += c[u];
    res[u] = s;
    vist[u] = true;
    for (int k = 0; k < m; ++k) {
      int v = adj[u][k];
      if (!vist[v]) {
        q.push(make_pair(v, s));
      }
    }
  }
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < q; ++i) {
    int p, x;
    scanf("%d %d", &p, &x);
    c[p - 1] += x;
  }

  bfs(0, 0);

  for (int i = 0; i < n; ++i) {
    printf("%d", res[i]);
    if (i == n - 1) {
      printf("\n");
    } else {
      printf(" ");
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int res[200005];
int c[200005];
bool vist[200005];
vector<int> adj[200005];

void bfs(int root, int sum) {
  queue<pii> q;
  q.push(make_pair(root, sum));
  while (!q.empty()) {
    pii e = q.front();
    q.pop();
    int u = e.first;
    int s = e.second;
    int m = adj[u].size();
    s += c[u];
    res[u] = s;
    vist[u] = true;
    for (int k = 0; k < m; ++k) {
      int v = adj[u][k];
      if (!vist[v]) {
        q.push(make_pair(v, s));
      }
    }
  }
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < q; ++i) {
    int p, x;
    scanf("%d %d", &p, &x);
    c[p - 1] += x;
  }

  bfs(0, 0);

  for (int i = 0; i < n; ++i) {
    printf("%d", res[i]);
    if (i == n - 1) {
      printf("\n");
    } else {
      printf(" ");
    }
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/**
 *    created: 11.05.2020 23:51:19
 **/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

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);
  }
}

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  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:
/**
 *    created: 11.05.2020 23:51:19
 **/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

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);
  }
}

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  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>
using namespace std;

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; 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;
  }
}

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

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;
  }
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, q;
int par[200000];
int ran[200000];
vector<int> ch[200000];
long long dp[200000];
int y[200000];
bool used[200000];
void dfs(int now, int mow) {
  for (int i = 0; i < ch[now].size(); i++) {
    if (par[ch[now][i]] == -1) {
      par[ch[now][i]] = now;
      ran[ch[now][i]] = mow + 1;
      dfs(now, mow + 1);
    }
  }
  return;
}
void solve(int now, int mow) {
  used[now] = 1;
  dp[now] += mow;
  dp[now] += y[now];
  for (int i = 0; i < ch[now].size(); i++) {
    if (used[ch[now][i]]) {
      continue;
    }
    solve(ch[now][i], dp[now]);
  }
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    par[i] = -1;
    cin >> a >> b;
    a--;
    b--;
    ch[a].push_back(b);
    ch[b].push_back(a);
  }
  par[0] = 1e9 + 7;
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    y[p] += x;
  }
  //
  dfs(0, 0);
  solve(0, 0);
  for (int i = 0; i < n; i++) {
    cout << dp[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, q;
int par[200000];
int ran[200000];
vector<int> ch[200000];
long long dp[200000];
int y[200000];
bool used[200000];
void dfs(int now, int mow) {
  for (int i = 0; i < ch[now].size(); i++) {
    if (par[ch[now][i]] == -1) {
      par[ch[now][i]] = now;
      ran[ch[now][i]] = mow + 1;
      dfs(ch[now][i], mow + 1);
    }
  }
  return;
}
void solve(int now, int mow) {
  used[now] = 1;
  dp[now] += mow;
  dp[now] += y[now];
  for (int i = 0; i < ch[now].size(); i++) {
    if (used[ch[now][i]]) {
      continue;
    }
    solve(ch[now][i], dp[now]);
  }
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    par[i] = -1;
    cin >> a >> b;
    a--;
    b--;
    ch[a].push_back(b);
    ch[b].push_back(a);
  }
  par[0] = 1e9 + 7;
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    y[p] += x;
  }
  //
  dfs(0, 0);
  solve(0, 0);
  for (int i = 0; i < n; i++) {
    cout << dp[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
  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 < (n); ++i)
typedef long long ll;
const int INF = 1001001001;

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

void dfs(int v, int p = -1) {
  for (int u : tos[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--;
    tos[a].push_back(b);
    tos[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] << ' '; }
}


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

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const int INF = 1001001001;

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

void dfs(int v, int p = -1) {
  for (int u : tos[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--;
    tos[a].push_back(b);
    tos[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] << ' '; }
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>

#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

struct Node {
  int num;
  vector<int> children;
  int add;
  int counter;
  Node() {
    counter = 0;
    add = 0;
  }
};

void add_all(vector<Node> &nodes, int p, int add = 0) {
  nodes[p].add += add;
  nodes[p].counter += nodes[p].add;
  for (auto child : nodes[p].children) {
    add_all(nodes, child, nodes[p].add);
  }
  nodes[p].add = 0;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<double> v(N, 0);
  vector<Node> nodes(N);
  REP(i, N) nodes[i].num = i;
  int a, b;
  FOR(i, 1, N) {
    cin >> a >> b;
    nodes[a - 1].children.push_back(b - 1);
  }

  int p, x;
  REP(i, Q) {
    cin >> p >> x;
    nodes[p - 1].add += x;
  }

  REP(i, N) add_all(nodes, i);

  cout << nodes[0].counter;
  FOR(i, 1, N) cout << " " << nodes[i].counter;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string.h>
#include <string>
#include <vector>

#define LOOP(N) for (int i = 0; i < (N); ++i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define FOR(i, start, end) for (int i = (start); i < (end); ++i)

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector<vector<int>>;

struct Node {
  int num;
  vector<int> children;
  int add;
  int counter;
  Node() {
    counter = 0;
    add = 0;
  }
};

void add_all(vector<Node> &nodes, int p, int add = 0) {
  nodes[p].add += add;
  if (nodes[p].add == 0)
    return;
  nodes[p].counter += nodes[p].add;
  for (auto child : nodes[p].children) {
    add_all(nodes, child, nodes[p].add);
  }
  nodes[p].add = 0;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<double> v(N, 0);
  vector<Node> nodes(N);
  REP(i, N) nodes[i].num = i;
  int a, b;
  FOR(i, 1, N) {
    cin >> a >> b;
    nodes[a - 1].children.push_back(b - 1);
  }

  int p, x;
  REP(i, Q) {
    cin >> p >> x;
    nodes[p - 1].add += x;
  }

  REP(i, N) add_all(nodes, i);

  cout << nodes[0].counter;
  FOR(i, 1, N) cout << " " << nodes[i].counter;
}
Problem p02936 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(3 * 10**5)


def main() -> None:
    def dfs(v, par, cnt):
        ans[v] = cnt + vs[v]
        for c in edges[v]:
            if c == par:
                continue
            dfs(c, v, ans[v])

    N, Q = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        edges[a].append(b)
        edges[b].append(a)
    vs = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        vs[p - 1] += x
    ans = [0] * N
    dfs(0, -1, 0)
    print(*ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(3 * 10**5)
input = sys.stdin.buffer.readline


def main() -> None:
    def dfs(v, par, cnt):
        ans[v] = cnt + vs[v]
        for c in edges[v]:
            if c == par:
                continue
            dfs(c, v, ans[v])

    N, Q = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        edges[a].append(b)
        edges[b].append(a)
    vs = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        vs[p - 1] += x
    ans = [0] * N
    dfs(0, -1, 0)
    print(*ans)


if __name__ == "__main__":
    main()