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

Buggy submission (status: Time Limit Exceeded):
from collections import defaultdict
import sys

sys.setrecursionlimit(10**6)

N, Q = map(int, input().split())
e = [[] for _ in range(N + 1)]
for i in range(N - 1):
    a, b = map(int, input().split())
    e[a].append(b)

q = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    q[p] += x

seen = [False] * (N + 1)
ans = [0] * (N + 1)


def dfs(e, v, x):
    seen[v] = True
    ans[v] += x
    for next_v in e[v]:
        if seen[next_v]:
            continue
        else:
            dfs(e, next_v, x + q[next_v])


dfs(e, 1, q[1])
print(*ans[1:])


Fixed submission:
from collections import defaultdict
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)

N, Q = map(int, input().split())
e = [[] for _ in range(N + 1)]
for i in range(N - 1):
    a, b = map(int, input().split())
    e[a].append(b)

q = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    q[p] += x

seen = [False] * (N + 1)
ans = [0] * (N + 1)


def dfs(e, v, x):
    seen[v] = True
    ans[v] += x
    for next_v in e[v]:
        if seen[next_v]:
            continue
        else:
            dfs(e, next_v, x + q[next_v])


dfs(e, 1, q[1])
print(*ans[1:])
Problem p02936 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import defaultdict
import sys

sys.setrecursionlimit(10**9)

N, Q = map(int, input().split())
G = defaultdict(list)
for i in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1  # 0-indexed
    G[a].append(b)
    G[b].append(a)

# print('G', G)

V = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1  # 0-indexed
    V[p] += x


def dfs(v, p):
    if p != -1:
        V[v] += V[p]
    for idx in G[v]:
        if idx == p:
            continue
        dfs(idx, v)


dfs(0, -1)

# print('V', V)

print(" ".join(map(str, V)))


Fixed submission:
from collections import defaultdict
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
G = defaultdict(list)
for i in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1  # 0-indexed
    G[a].append(b)
    G[b].append(a)

# print('G', G)

V = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1  # 0-indexed
    V[p] += x


def dfs(v, p):
    if p != -1:
        V[v] += V[p]
    for idx in G[v]:
        if idx == p:
            continue
        dfs(idx, v)


dfs(0, -1)

# print('V', V)

print(" ".join(map(str, V)))
Problem p02936 (C++)

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

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// status unsolved
int n, q;
vector<vector<int>> to(100010);
long long weight[100010] = {};

void dfs(int now, int before) {
  if (before != -1)
    weight[now] += weight[before];

  for (auto next : to[now]) {
    if (next == before)
      continue;
    dfs(next, now);
  }
  return;
}
int main() {

  cin >> n >> q;

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  rep(i, q) {
    int p;
    long long x;
    cin >> p >> x;
    p--;
    weight[p] += x;
  }
  dfs(0, n);

  rep(i, n) cout << weight[i] << " ";
  cout << endl;

  return 0;
}
/*


*/


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

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// status unsolved
int n, q;
vector<vector<int>> to(200010);
long long weight[200010] = {};

void dfs(int now, int before) {
  if (before != -1)
    weight[now] += weight[before];

  for (auto next : to[now]) {
    if (next == before)
      continue;
    dfs(next, now);
  }
  return;
}
int main() {

  cin >> n >> q;

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }

  rep(i, q) {
    int p;
    long long x;
    cin >> p >> x;
    p--;
    weight[p] += x;
  }
  dfs(0, n);

  rep(i, n) cout << weight[i] << " ";
  cout << endl;

  return 0;
}
/*


*/
Problem p02936 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys

sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
px = [list(map(int, input().split())) for _ in range(q)]

graph = [[] for _ in range(n + 1)]
for a, b in ab:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (n + 1)
for p, x in px:
    value[p] += x


def dfs(v, p, add):
    value[v] += add
    for x in graph[v]:
        if x == p:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

ans = value[1:]
print(*ans)


Fixed submission:
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
px = [list(map(int, input().split())) for _ in range(q)]

graph = [[] for _ in range(n + 1)]
for a, b in ab:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (n + 1)
for p, x in px:
    value[p] += x


def dfs(v, p, add):
    value[v] += add
    for x in graph[v]:
        if x == p:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

ans = value[1:]
print(*ans)
Problem p02936 (C++)

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

queue<pair<int, long>> q;

void wfs(vector<vector<int>> Ed, vector<pair<long, int>> &Ve) {
  if (q.empty() == 0) {
    pair<int, long> v = q.front();
    q.pop();
    Ve[v.first].first += v.second;
    for (int i = 0; i < Ed[v.first].size(); i++) {
      if (Ed[v.first][i] != Ve[v.first].second) {
        Ve[Ed[v.first][i]].second = v.first;
        q.push(make_pair(Ed[v.first][i], Ve[v.first].first));
      }
    }
    wfs(Ed, Ve);
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> Ed(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    Ed[a - 1].push_back(b - 1);
    Ed[b - 1].push_back(a - 1);
  }
  vector<pair<long, int>> Ve(N, make_pair(0, 0));
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    Ve[p - 1].first += x;
  }

  q.push(make_pair(0, 0));
  wfs(Ed, Ve);
  for (int i = 0; i < N; i++) {
    cout << Ve[i].first;
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}

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

queue<pair<int, long>> q;

void wfs(vector<vector<int>> &Ed, vector<pair<long, int>> &Ve) {
  if (q.empty() == 0) {
    pair<int, long> v = q.front();
    q.pop();
    Ve[v.first].first += v.second;
    for (int i = 0; i < Ed[v.first].size(); i++) {
      if (Ed[v.first][i] != Ve[v.first].second) {
        Ve[Ed[v.first][i]].second = v.first;
        q.push(make_pair(Ed[v.first][i], Ve[v.first].first));
      }
    }
    wfs(Ed, Ve);
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> Ed(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    Ed[a - 1].push_back(b - 1);
    Ed[b - 1].push_back(a - 1);
  }
  vector<pair<long, int>> Ve(N, make_pair(0, 0));
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    Ve[p - 1].first += x;
  }

  q.push(make_pair(0, 0));
  wfs(Ed, Ve);
  for (int i = 0; i < N; i++) {
    cout << Ve[i].first;
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct node {
  int ff, num;
} v[20005];

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    v[i].ff = 0;
    v[i].num = 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[y].ff = x;
  }
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x;
    cin >> y;
    v[x].num += y;
  }
  cout << v[1].num;
  for (int i = 2; i <= n; i++) {
    v[i].num += v[v[i].ff].num;
    cout << " " << v[i].num;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int ff, num;
} v[200005];

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    v[i].ff = 0;
    v[i].num = 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[y].ff = x;
  }
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x;
    cin >> y;
    v[x].num += y;
  }
  cout << v[1].num;
  for (int i = 2; i <= n; i++) {
    v[i].num += v[v[i].ff].num;
    cout << " " << v[i].num;
  }
  cout << endl;
  return 0;
}
Problem p02936 (Python)

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

sys.setrecursionlimit(1000000)


def dfs(v, par):
    for nv in tree[v]:
        if nv == par:
            continue
        add[nv] += add[v]
        dfs(nv, v)


n, q = map(int, input().split())
tree = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    tree[b].append(a)
    tree[a].append(b)
add = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    add[p] += x
dfs(0, -1)
print(*add)


Fixed submission:
import sys

sys.setrecursionlimit(1000000)
input = sys.stdin.readline


def dfs(v, par):
    for nv in tree[v]:
        if nv == par:
            continue
        add[nv] += add[v]
        dfs(nv, v)


n, q = map(int, input().split())
tree = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    tree[b].append(a)
    tree[a].append(b)
add = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    add[p] += x
dfs(0, -1)
print(*add)
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;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
#define chmin(x, y) x = min(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

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

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
#define chmin(x, y) x = min(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

void dfs(int v, int p = -1) {
  int d = ans[v];
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += d;
    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 <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお

vector<ll> tree[200010];
vector<ll> ans;

void dfs(int v, int pair) {
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i] == pair)
      continue;
    ans[tree[v][i]] += ans[v];
    dfs(tree[v][i], v);
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  dfs(0, -1);

  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define check(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
// オーバーフローに気をつけろよおおおおおお
// 確認忘れるなよおおおおおお

vector<ll> tree[200010];
vector<ll> ans;

void dfs(int v, int pair) {
  for (int i = 0; i < tree[v].size(); i++) {
    if (tree[v][i] == pair)
      continue;
    ans[tree[v][i]] += ans[v];
    dfs(tree[v][i], v);
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  dfs(0, -1);

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

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

typedef struct _TREE {
  vector<int> connect;
  long long startCnt;
  long long totalCnt;
} TREE;

TREE trees[100001];

void addCounters(int beforeTree, int thisTree, long long addCounter) {
  trees[thisTree].totalCnt = trees[thisTree].startCnt + addCounter;
  for (unsigned int i = 0; i < trees[thisTree].connect.size(); i++) {
    int connectedTree = trees[thisTree].connect[i];
    if (connectedTree == beforeTree)
      continue;

    addCounters(thisTree, connectedTree, trees[thisTree].totalCnt);
  }
}

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

  // initialization
  for (int i = 1; i <= N; i++) {
    trees[i].connect.clear();
    trees[i].startCnt = 0;
    trees[i].totalCnt = 0;
  }
  // edge
  for (int i = 1; i <= N - 1; i++) {
    int a, b;
    cin >> a >> b;
    trees[a].connect.push_back(b);
    trees[b].connect.push_back(a);
  }
  // counter
  for (int i = 1; i <= Q; i++) {
    int p, x;
    cin >> p >> x;
    trees[p].startCnt += x;
  }
  // calc
  addCounters(0, 1, 0);

  // answer
  for (int i = 1; i < N; i++) {
    cout << trees[i].totalCnt << " ";
  }
  cout << trees[N].totalCnt << endl;

  return 0;
}


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

typedef struct _TREE {
  vector<int> connect;
  long long startCnt;
  long long totalCnt;
} TREE;

TREE trees[200001];

void addCounters(int beforeTree, int thisTree, long long addCounter) {
  trees[thisTree].totalCnt = trees[thisTree].startCnt + addCounter;
  for (unsigned int i = 0; i < trees[thisTree].connect.size(); i++) {
    int connectedTree = trees[thisTree].connect[i];
    if (connectedTree == beforeTree)
      continue;

    addCounters(thisTree, connectedTree, trees[thisTree].totalCnt);
  }
}

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

  // initialization
  for (int i = 1; i <= N; i++) {
    trees[i].connect.clear();
    trees[i].startCnt = 0;
    trees[i].totalCnt = 0;
  }
  // edge
  for (int i = 1; i <= N - 1; i++) {
    int a, b;
    cin >> a >> b;
    trees[a].connect.push_back(b);
    trees[b].connect.push_back(a);
  }
  // counter
  for (int i = 1; i <= Q; i++) {
    int p, x;
    cin >> p >> x;
    trees[p].startCnt += x;
  }
  // calc
  addCounters(0, 1, 0);

  // answer
  for (int i = 1; i < N; i++) {
    cout << trees[i].totalCnt << " ";
  }
  cout << trees[N].totalCnt << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long int;
template <typename T> void swap(T *t1, T *t2) {
  T *tmp = t1;
  t1 = t2;
  t2 = tmp;
}

vector<Int> m[100002];
Int stts[100002];
Int arr[100002];

void dfs(Int u, Int val) {
  stts[u] = 1;
  arr[u] += val;
  for (Int i = 0; i < m[u].size(); i++) {
    if (stts[m[u][i]] == 0) {
      dfs(m[u][i], arr[u]);
    }
  }
}

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

  Int n, q, a, b;
  cin >> n >> q;
  Int p, x;

  // init
  for (Int i = 0; i < n; i++) {
    arr[i] = 0;
    stts[i] = 0;
  }

  for (Int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    m[a - 1].push_back(b - 1);
    m[b - 1].push_back(a - 1);
  }
  for (Int i = 0; i < q; i++) {
    cin >> p >> x;
    arr[p - 1] += x;
  }
  dfs(0, 0);
  for (Int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long int;
template <typename T> void swap(T *t1, T *t2) {
  T *tmp = t1;
  t1 = t2;
  t2 = tmp;
}

vector<Int> m[300002];
Int stts[300002];
Int arr[300002];

void dfs(Int u, Int val) {
  stts[u] = 1;
  arr[u] += val;
  for (Int i = 0; i < m[u].size(); i++) {
    if (stts[m[u][i]] == 0) {
      dfs(m[u][i], arr[u]);
    }
  }
}

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

  Int n, q, a, b;
  cin >> n >> q;
  Int p, x;

  // init
  for (Int i = 0; i < n; i++) {
    arr[i] = 0;
    stts[i] = 0;
  }

  for (Int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    m[a - 1].push_back(b - 1);
    m[b - 1].push_back(a - 1);
  }
  for (Int i = 0; i < q; i++) {
    cin >> p >> x;
    arr[p - 1] += x;
  }
  dfs(0, 0);
  for (Int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;

  return 0;
}
Problem p02936 (C++)

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

vector<vector<int>> to(20005);
vector<int> ans;

void dfs(int c, int p = -1) {
  for (int u : to.at(c)) {
    if (u == p) {
      continue;
    }
    ans.at(u) += ans.at(c);
    dfs(u, c);
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  // v.resize(N);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to.at(a).push_back(b);
    to.at(b).push_back(a);
  }
  ans.resize(n);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans.at(p) += x;
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << ans.at(i) << endl;
  }
  return 0;
}


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

vector<vector<int>> to(200005);
vector<int> ans;

void dfs(int c, int p = -1) {
  for (int u : to.at(c)) {
    if (u == p) {
      continue;
    }
    ans.at(u) += ans.at(c);
    dfs(u, c);
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  // v.resize(N);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to.at(a).push_back(b);
    to.at(b).push_back(a);
  }
  ans.resize(n);
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans.at(p) += x;
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << ans.at(i) << 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 < (int)(n); i++)

#define MAXDATA 100
vector<vector<int>> to(MAXDATA);
vector<bool> seen(MAXDATA, false);
vector<int> ans(MAXDATA, 0);

void dfs(const vector<vector<int>> &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    ans[next_v] += ans[v];
    dfs(G, next_v);
  }
}

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

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

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

  dfs(to, 0);

  rep(i, n) cout << ans[i] << " ";
  cout << endl;
  return 0;
}

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

#define MAXDATA 200100
vector<vector<int>> to(MAXDATA);
vector<bool> seen(MAXDATA, false);
vector<int> ans(MAXDATA, 0);

void dfs(const vector<vector<int>> &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    ans[next_v] += ans[v];
    dfs(G, next_v);
  }
}

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

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

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

  dfs(to, 0);

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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define sz(x) int(x.size())
double pi = 3.141592653589793;
ll mod = 1000000007;
ll INF = 1000000001;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

vi to[20005];
vi 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].pb(b);
    to[b].pb(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define sz(x) int(x.size())
double pi = 3.141592653589793;
ll mod = 1000000007;
ll INF = 1000000001;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

vi to[200005];
vi 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].pb(b);
    to[b].pb(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 <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define per(i, j, k) for (int i = (int)j; i >= (int)k; i--)
#define F(i, k) for (int i = (int)0; i < (int)k; i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
const int maxn = 1e5 + 5;
using namespace std;
ll m, n, k;
vector<int> E[maxn];
ll mk[maxn], sc[maxn];
int vis[maxn];
void dfs(int x, ll now) {
  vis[x] = 1;
  sc[x] = mk[x] + now;
  for (auto t : E[x]) {
    if (vis[t])
      continue;
    dfs(t, now + mk[x]);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> m;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    mk[x] += y;
  }
  dfs(1, 0);
  rep(i, 1, n) cout << sc[i] << ' ';
  double ans = 0;
  cin >> n;
}

/*

*/

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define per(i, j, k) for (int i = (int)j; i >= (int)k; i--)
#define F(i, k) for (int i = (int)0; i < (int)k; i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
const int maxn = 2e5 + 5;
using namespace std;
ll m, n, k;
vector<int> E[maxn];
ll mk[maxn], sc[maxn];
int vis[maxn];
void dfs(int x, ll now) {
  vis[x] = 1;
  sc[x] = mk[x] + now;
  for (auto t : E[x]) {
    if (vis[t])
      continue;
    dfs(t, now + mk[x]);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> m;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    mk[x] += y;
  }
  dfs(1, 0);
  rep(i, 1, n) cout << sc[i] << ' ';
  double ans = 0;
  cin >> n;
}

/*

*/
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/* ****devACE**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 100005
vector<int> adj[N];
int dp[N];
void dfs(int node, int par) {
  dp[node] += dp[par];
  for (auto i : adj[node])
    if (i != par) {
      dfs(i, node);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    int n, m, i, j, k;
    cin >> n >> m;
    for (i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      adj[x].push_back(y);
      adj[y].push_back(x);
    }

    while (m--) {
      int x, w;
      cin >> x >> w;
      dp[x] += w;
    }
    dfs(1, 0);
    for (i = 1; i <= n; i++)
      cout << dp[i] << " ";
  }
  return 0;
}

Fixed submission:
/* ****devACE**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 200005
vector<int> adj[N];
int dp[N];
void dfs(int node, int par) {
  dp[node] += dp[par];
  for (auto i : adj[node])
    if (i != par) {
      dfs(i, node);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    int n, m, i, j, k;
    cin >> n >> m;
    for (i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      adj[x].push_back(y);
      adj[y].push_back(x);
    }

    while (m--) {
      int x, w;
      cin >> x >> w;
      dp[x] += w;
    }
    dfs(1, 0);
    for (i = 1; i <= n; i++)
      cout << dp[i] << " ";
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PI acos(-1)
#define eq(x, y) (fabs((x) - (y)) < eps)
#define debug(x) cerr << x << endl;
#define endl "\n"
#define pb push_back
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 7;
const int nmax = 1e5 + 10;
const int eps = 1e-8;

vector<int> adj[nmax];
int sum[nmax];
bool vis[nmax];

void dfs(int now, int from) {
  if (vis[now])
    return;
  vis[now] = true;
  sum[now] += sum[from];

  for (auto to : adj[now])
    dfs(to, now);
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  // cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  while (q--) {
    int v, x;
    cin >> v >> x;

    sum[v] += x;
  }

  dfs(1, 0);

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

  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define PI acos(-1)
#define eq(x, y) (fabs((x) - (y)) < eps)
#define debug(x) cerr << x << endl;
#define endl "\n"
#define pb push_back
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 7;
const int nmax = 1e5 + 10;
const int eps = 1e-8;

vector<int> adj[2 * nmax];
int sum[2 * nmax];
bool vis[2 * nmax];

void dfs(int now, int from) {
  if (vis[now])
    return;
  vis[now] = true;
  sum[now] += sum[from];

  for (auto to : adj[now])
    dfs(to, now);
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  // cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  while (q--) {
    int v, x;
    cin >> v >> x;

    sum[v] += x;
  }

  dfs(1, 0);

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

  cout << endl;

  return 0;
}
Problem p02936 (C++)

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

vector<vector<int>> T(100000);
vector<int> Count(100000, 0);

void solve(int a) {
  for (int i : T[a]) {
    Count[i] += Count[a];
    solve(i);
  }
  return;
}

int main() {
  int N, Q, a, b, p, x;
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    T[a].push_back(b);
  }
  for (int i = 0; i < Q; i++) {
    cin >> p >> x;
    p--;
    Count[p] += x;
  }
  solve(0);
  for (int i = 0; i < N; i++) {
    cout << Count[i] << " ";
  }
}


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

vector<vector<int>> T(200000);
vector<int> Count(200000, 0);

void solve(int a) {
  for (int i : T[a]) {
    Count[i] += Count[a];
    solve(i);
  }
  return;
}

int main() {
  int N, Q, a, b, p, x;
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    T[a].push_back(b);
  }
  for (int i = 0; i < Q; i++) {
    cin >> p >> x;
    p--;
    Count[p] += x;
  }
  solve(0);
  for (int i = 0; i < N; i++) {
    cout << Count[i] << " ";
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

const int MAX_N = 1e5 + 10;
vector<int> cost(MAX_N, 0);
vector<int> ans(MAX_N, -1);
vector<vector<int>> edge(MAX_N, vector<int>{});

int dfs(int frm, int n) {
  if (ans[n] != -1) {
    return 0;
  }
  if (frm == -1) {
    ans[n] = cost[n];
  } else {
    ans[n] = ans[frm] + cost[n];
  }
  for (auto &v : edge[n]) {
    dfs(n, v);
  }
  return 0;
}

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;

const int MAX_N = 3 * 1e5;
vector<int> cost(MAX_N, 0);
vector<int> ans(MAX_N, -1);
vector<vector<int>> edge(MAX_N, vector<int>{});

int dfs(int frm, int n) {
  if (ans[n] != -1) {
    return 0;
  }
  if (frm == -1) {
    ans[n] = cost[n];
  } else {
    ans[n] = ans[frm] + cost[n];
  }
  for (auto &v : edge[n]) {
    dfs(n, v);
  }
  return 0;
}

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

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

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

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


Fixed submission:
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

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

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

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

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

#define ll long long int
#define hell 1000000007
#define vi vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define vii vector<pii>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sep(i, a, b) for (ll i = a - 1; i >= b; i--)
#define inf (ll)(1e16)

using namespace std;

vi adj[100005];
vector<bool> visited(100005, false);
vi a(100005, 0);

void dfs(ll s, ll pv) {
  visited[s] = true;
  a[s] += pv;
  for (auto ch : adj[s]) {
    if (!visited[ch])
      dfs(ch, a[s]);
  }
}

void solve() {
  ll n, u, v, q;
  cin >> n >> q;
  rep(i, 1, n) {
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  rep(i, 0, q) {
    cin >> u >> v;
    a[u] += v;
  }
  dfs(1, 0);
  rep(i, 1, n + 1) cout << a[i] << " ";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


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

#define ll long long int
#define hell 1000000007
#define vi vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define vii vector<pii>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sep(i, a, b) for (ll i = a - 1; i >= b; i--)
#define inf (ll)(1e16)

using namespace std;

vi adj[300005];
vector<bool> visited(300005, false);
vi a(300005, 0);

void dfs(ll s, ll pv) {
  visited[s] = true;
  a[s] += pv;
  for (auto ch : adj[s]) {
    if (!visited[ch])
      dfs(ch, a[s]);
  }
}

void solve() {
  ll n, u, v, q;
  cin >> n >> q;
  rep(i, 1, n) {
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  rep(i, 0, q) {
    cin >> u >> v;
    a[u] += v;
  }
  dfs(1, 0);
  rep(i, 1, n + 1) cout << a[i] << " ";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02936 (C++)

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

using namespace std;

vector<vector<int>> adjl(200000);
vector<int> res(20000, 0);

void dfs(int t, int prev, int dsum) {
  dsum += res[t];
  res[t] = dsum;
  for (int i = 0; i < adjl[t].size(); i++)
    if (adjl[t][i] != prev)
      dfs(adjl[t][i], t, dsum);
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adjl[a - 1].push_back(b - 1);
    adjl[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    res[a - 1] += b;
  }
  dfs(0, -1, 0);
  for (int i = 0; i < n; i++)
    cout << res[i] << " ";
}

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

using namespace std;

vector<vector<int>> adjl(200000);
vector<int> res(200000, 0);

void dfs(int t, int prev, int dsum) {
  dsum += res[t];
  res[t] = dsum;
  for (int i = 0; i < adjl[t].size(); i++)
    if (adjl[t][i] != prev)
      dfs(adjl[t][i], t, dsum);
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adjl[a - 1].push_back(b - 1);
    adjl[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    res[a - 1] += b;
  }
  dfs(0, -1, 0);
  for (int i = 0; i < n; i++)
    cout << res[i] << " ";
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  Test
//
//  Created by momoc on 2019/8/18.
//  Copyright © 2019年 momoc. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;
const int N = 10 * 5 + 1;
int a[N] = {0};
vector<int> dege[N];
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int add = a[v];
    // printf("%d\n",add);
    for (int i = 0; i < dege[v].size(); i++) {
      a[dege[v][i]] += add;
      q.push(dege[v][i]);
    }
  }
}
int main(int argc, const char *argv[]) {
  // insert code here...
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    scanf("%d %d", &p, &q);
    dege[p].push_back(q);
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++) {
    int p, q;
    scanf("%d %d", &p, &q);
    a[p] += q;
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    printf("%d ", a[i]);
  }
  return 0;
}


Fixed submission:
//
//  main.cpp
//  Test
//
//  Created by momoc on 2019/8/18.
//  Copyright © 2019年 momoc. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;
const int N = 200000 + 1;
int a[N] = {0};
vector<int> dege[N];
void bfs() {
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int add = a[v];
    // printf("%d\n",add);
    for (int i = 0; i < dege[v].size(); i++) {
      a[dege[v][i]] += add;
      q.push(dege[v][i]);
    }
  }
}
int main(int argc, const char *argv[]) {
  // insert code here...
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    scanf("%d %d", &p, &q);
    dege[p].push_back(q);
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++) {
    int p, q;
    scanf("%d %d", &p, &q);
    a[p] += q;
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    printf("%d ", a[i]);
  }
  return 0;
}
Problem p02936 (C++)

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

// counterの値を親から子に伝搬させる
void dfs(int vertex, int parent, vector<vector<int>> &Tree,
         vector<int> &counter) {

  // 親のカウンターの値を足し合わせる
  counter[vertex] += counter[parent];

  // 子に伝播させる
  for (const int &nxtv : Tree[vertex]) {
    if (nxtv == parent)
      continue;
    dfs(nxtv, vertex, Tree, counter);
  }
}

// 方針：親から子にカウンターの値を伝播させていくことで実現
int main(void) {
  // 入力
  int N, Q;
  cin >> N >> Q;
  // 木
  vector<vector<int>> Tree(N);
  // 頂点のカウンター
  vector<int> counter(N + 1, 0);
  // 木の構築
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    Tree[a].push_back(b);
    Tree[b].push_back(a);
  }

  // カウンターの加算
  for (int j = 0; j < Q; ++j) {
    int p, x;
    cin >> p >> x;
    counter[p] += x;
  }

  // カウンターの伝搬
  dfs(1, 0, Tree, counter);

  // 出力
  for (int i = 1; i <= N; ++i) {
    cout << counter[i] << " ";
  }
  cout << endl;
  return 0;
}


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

// counterの値を親から子に伝搬させる
void dfs(int vertex, int parent, vector<vector<int>> &Tree,
         vector<int> &counter) {

  // 親のカウンターの値を足し合わせる
  counter[vertex] += counter[parent];

  // 子に伝播させる
  for (const int &nxtv : Tree[vertex]) {
    if (nxtv == parent)
      continue;
    dfs(nxtv, vertex, Tree, counter);
  }
}

// 方針：親から子にカウンターの値を伝播させていくことで実現
int main(void) {
  // 入力
  int N, Q;
  cin >> N >> Q;
  // 木
  vector<vector<int>> Tree(N + 1);
  // 頂点のカウンター
  vector<int> counter(N + 1, 0);
  // 木の構築
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    Tree[a].push_back(b);
    Tree[b].push_back(a);
  }

  // カウンターの加算
  for (int j = 0; j < Q; ++j) {
    int p, x;
    cin >> p >> x;
    counter[p] += x;
  }

  // カウンターの伝搬
  dfs(1, 0, Tree, counter);

  // 出力
  for (int i = 1; i <= N; ++i) {
    cout << counter[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02936 (C++)

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

void seq(vector<vector<ll>> next, vector<ll> &cnt, ll from, ll ff) {
  for (ll i = 0; i < next.at(from).size(); i++) {
    if (from != 0 && next.at(from).at(i) - 1 == ff)
      continue;
    cnt.at(next.at(from).at(i) - 1) += cnt.at(from);
    seq(next, cnt, next.at(from).at(i) - 1, from);
  }
  return;
}

int main() {
  ll n, q, a, b;
  cin >> n >> q;
  vector<vector<ll>> next(n, vector<ll>(0));
  for (ll i = 0; i < n - 1; i++) {
    cin >> a >> b;
    next.at(a - 1).push_back(b);
    next.at(b - 1).push_back(a);
  }
  vector<ll> p(q), x(q), cnt(n, 0);
  for (int i = 0; i < q; i++) {
    cin >> p.at(i) >> x.at(i);
    cnt.at(p.at(i) - 1) += x.at(i);
  }
  seq(next, cnt, 0, 0);
  for (ll i = 0; i < n - 1; i++)
    cout << cnt.at(i) << " ";
  cout << cnt.at(n - 1) << endl;
}

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

void seq(vector<vector<ll>> &next, vector<ll> &cnt, ll from, ll ff) {
  for (ll i = 0; i < next.at(from).size(); i++) {
    if (from != 0 && next.at(from).at(i) - 1 == ff)
      continue;
    cnt.at(next.at(from).at(i) - 1) += cnt.at(from);
    seq(next, cnt, next.at(from).at(i) - 1, from);
  }
  return;
}

int main() {
  ll n, q, a, b;
  cin >> n >> q;
  vector<vector<ll>> next(n, vector<ll>(0));
  for (ll i = 0; i < n - 1; i++) {
    cin >> a >> b;
    next.at(a - 1).push_back(b);
    next.at(b - 1).push_back(a);
  }
  vector<ll> p(q), x(q), cnt(n, 0);
  for (int i = 0; i < q; i++) {
    cin >> p.at(i) >> x.at(i);
    cnt.at(p.at(i) - 1) += x.at(i);
  }
  seq(next, cnt, 0, 0);
  for (ll i = 0; i < n - 1; i++)
    cout << cnt.at(i) << " ";
  cout << cnt.at(n - 1) << endl;
}
Problem p02936 (C++)

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

#define forn(i, n) for (int i = 0; i < n; i++)
vector<int> edges[100010];
long long ans[100010];
long long bucket[100010];

void dfs(int node, int prev, long long val = 0) {
  val += bucket[node];
  ans[node] = val;
  for (auto &x : edges[node]) {
    if (prev - x) {
      dfs(x, node, val);
    }
  }
}

int main() {
  int n, q;
  int a, b;
  cin >> n >> q;
  forn(i, n - 1) {
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  forn(i, q) {
    cin >> a >> b;
    bucket[a] += b;
  }
  dfs(1, -1);

  forn(i, n) { cout << ans[i + 1] << " "; }

  cout << endl;

  return 0;
}


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

#define forn(i, n) for (int i = 0; i < n; i++)
vector<int> edges[200010];
long long ans[200010];
long long bucket[200010];

void dfs(int node, int prev, long long val = 0) {
  val += bucket[node];
  ans[node] = val;
  for (auto &x : edges[node]) {
    if (prev - x) {
      dfs(x, node, val);
    }
  }
}

int main() {
  int n, q;
  int a, b;
  cin >> n >> q;
  forn(i, n - 1) {
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  forn(i, q) {
    cin >> a >> b;
    bucket[a] += b;
  }
  dfs(1, -1);

  forn(i, n) { cout << ans[i + 1] << " "; }

  cout << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
vector<long long> g[100005];
bool vis[100005];
long long lazy[100005];
long long cnt[100005];
void dfs(int i) {
  if (vis[i])
    return;
  vis[i] = 1;
  cnt[i] += lazy[i];
  for (int j = 0; j < g[i].size(); j++) {
    if (!vis[g[i][j]]) {
      cnt[g[i][j]] += cnt[i];
      dfs(g[i][j]);
    }
  }
}

int main() {
  long long n, q;
  cin >> n >> q;
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  while (q--) {
    long long x, p;
    cin >> x >> p;
    lazy[x] += p;
  }
  dfs(1);
  for (long long i = 1; i <= n; i++)
    cout << cnt[i] << ' ';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
vector<long long> g[200005];
bool vis[200005];
long long lazy[200005];
long long cnt[200005];
void dfs(int i) {
  if (vis[i])
    return;
  vis[i] = 1;
  cnt[i] += lazy[i];
  for (int j = 0; j < g[i].size(); j++) {
    if (!vis[g[i][j]]) {
      cnt[g[i][j]] += cnt[i];
      dfs(g[i][j]);
    }
  }
}

int main() {
  long long n, q;
  cin >> n >> q;
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  while (q--) {
    long long x, p;
    cin >> x >> p;
    lazy[x] += p;
  }
  dfs(1);
  for (long long i = 1; i <= n; i++)
    cout << cnt[i] << ' ';
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 4000
#define offset 8010
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ii> iii;
typedef pair<map<ll, ll>, ll> mi;
typedef pair<ii, ii> iiii;
typedef vector<ll> vi;
const ll MOD = 1e9 + 7;
vector<int> grafo[tam];
int sum[tam], ans[tam];
void dfs(int num, int pa, int tot) {
  tot += sum[num];
  ans[num] = tot;
  for (int v : grafo[num]) {
    if (v == pa)
      continue;
    dfs(v, num, tot);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int iz, der;
  forr(i, 0, n - 1) {
    cin >> iz >> der;
    grafo[iz].pb(der);
    grafo[der].pb(iz);
  }
  int valor;
  forr(i, 0, m) {
    cin >> iz >> valor;
    sum[iz] += valor;
  }
  dfs(1, 1, 0);
  forr(i, 1, n + 1) cout << ans[i] << ' ';
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 410000
#define offset 8010
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ii> iii;
typedef pair<map<ll, ll>, ll> mi;
typedef pair<ii, ii> iiii;
typedef vector<ll> vi;
const ll MOD = 1e9 + 7;
vector<int> grafo[tam];
int sum[tam], ans[tam];
void dfs(int num, int pa, int tot) {
  tot += sum[num];
  ans[num] = tot;
  for (int v : grafo[num]) {
    if (v == pa)
      continue;
    dfs(v, num, tot);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int iz, der;
  forr(i, 0, n - 1) {
    cin >> iz >> der;
    grafo[iz].pb(der);
    grafo[der].pb(iz);
  }
  int valor;
  forr(i, 0, m) {
    cin >> iz >> valor;
    sum[iz] += valor;
  }
  dfs(1, 1, 0);
  forr(i, 1, n + 1) cout << ans[i] << ' ';
}