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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

vector<bool> visited(100005, false);

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

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

  vector<ll> ans(n, 0);
  for (int i = 0; i < q2; i++) {
    ll p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  queue<ll> q;
  q.push(0);
  visited[0] = true;
  while (!q.empty()) {
    ll now = q.front();
    q.pop();
    for (int i = 0; i < tree[now].size(); i++) {
      if (visited[tree[now][i]] == false) {
        q.push(tree[now][i]);
        ans[tree[now][i]] += ans[now];
        visited[tree[now][i]] = true;
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

vector<bool> visited(200005, false);

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

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

  vector<ll> ans(n, 0);
  for (int i = 0; i < q2; i++) {
    ll p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  queue<ll> q;
  q.push(0);
  visited[0] = true;
  while (!q.empty()) {
    ll now = q.front();
    q.pop();
    for (int i = 0; i < tree[now].size(); i++) {
      if (visited[tree[now][i]] == false) {
        q.push(tree[now][i]);
        ans[tree[now][i]] += ans[now];
        visited[tree[now][i]] = true;
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vi edges[20020];
ll sum[20020];

void dfs(int i, int p) {
  for (int j : edges[i]) {
    if (j == p)
      continue;
    sum[j] += sum[i];
    dfs(j, i);
  }
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vi edges[200200];
ll sum[200200];

void dfs(int i, int p) {
  for (int j : edges[i]) {
    if (j == p)
      continue;
    sum[j] += sum[i];
    dfs(j, i);
  }
}

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

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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

ll N, Q;
vector<ll> edges[100100];
ll c[100100];

void dfs(ll ix, ll carry, ll par) {
  c[ix] += carry;
  for (auto nx : edges[ix]) {
    if (nx == par)
      continue;
    dfs(nx, c[ix], ix);
  }
}

signed main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    c[p] += x;
  }
  dfs(0, 0, -1);
  rep(i, N) {
    cout << c[i];
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

ll N, Q;
vector<ll> edges[300100];
ll c[300100];

void dfs(ll ix, ll carry, ll par) {
  c[ix] += carry;
  for (auto nx : edges[ix]) {
    if (nx == par)
      continue;
    dfs(nx, c[ix], ix);
  }
}

signed main() {
  cin >> N >> Q;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  rep(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    c[p] += x;
  }
  dfs(0, 0, -1);
  rep(i, N) {
    cout << c[i];
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
//    https://youtu.be/mRHBP3xwgn4

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

typedef long long ll;
typedef long double ld;
typedef long long unsigned llu;
typedef pair<long long, long long> pll;
const long long inf = 2000000000000000000LL; // 2e18
#define pi acos(-1.0)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define shesh "\n"
#define ff first
#define ss second
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define debug(x) cout << "debug " << x << "\n"
#define mest(a, b) memset(a, b, sizeof(a))
ll ans[100010], a[100010];
vector<ll> v[100010];
bool vis[100010];
void dfs(ll node, ll carry) {
  carry += a[node];
  ans[node] += carry;
  for (ll i = 0; i < v[node].size(); i++) {
    if (!vis[v[node][i]]) {
      vis[v[node][i]] = true;
      dfs(v[node][i], carry);
    }
  }
}
int main() {
  fast;
  ll t, n, m, i, j, k, temp, flag, q, A, b, p, x;
  cin >> n >> q;
  for (i = 0; i < n - 1; i++) {
    cin >> A >> b;
    v[A].pb(b);
    v[b].pb(A);
  }
  while (q--) {
    cin >> p >> x;
    a[p] += x;
  }
  vis[1] = true;
  dfs(1, 0);
  for (i = 1; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}


Fixed submission:
//    https://youtu.be/mRHBP3xwgn4

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

typedef long long ll;
typedef long double ld;
typedef long long unsigned llu;
typedef pair<long long, long long> pll;
const long long inf = 2000000000000000000LL; // 2e18
#define pi acos(-1.0)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define shesh "\n"
#define ff first
#define ss second
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define debug(x) cout << "debug " << x << "\n"
#define mest(a, b) memset(a, b, sizeof(a))
ll ans[200010], a[200010];
vector<ll> v[200010];
bool vis[200010];
void dfs(ll node, ll carry) {
  carry += a[node];
  ans[node] += carry;
  for (ll i = 0; i < v[node].size(); i++) {
    if (!vis[v[node][i]]) {
      vis[v[node][i]] = true;
      dfs(v[node][i], carry);
    }
  }
}
int main() {
  fast;
  ll t, n, m, i, j, k, temp, flag, q, A, b, p, x;
  cin >> n >> q;
  for (i = 0; i < n - 1; i++) {
    cin >> A >> b;
    v[A].pb(b);
    v[b].pb(A);
  }
  while (q--) {
    cin >> p >> x;
    a[p] += x;
  }
  vis[1] = true;
  dfs(1, 0);
  for (i = 1; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}
Problem p02936 (C++)

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

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

vector<int> Graph[20010];
int Score[20010];
bool flag[20010];

void dfs(int s) {
  if (flag[s])
    return;
  flag[s] = true;
  for (int v : Graph[s]) {
    if (flag[v])
      continue;
    Score[v] += Score[s];
    dfs(v);
  }
}

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

vector<int> Graph[200010];
int Score[200010];
bool flag[200010];

void dfs(int s) {
  if (flag[s])
    return;
  flag[s] = true;
  for (int v : Graph[s]) {
    if (flag[v])
      continue;
    Score[v] += Score[s];
    dfs(v);
  }
}

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

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int s[100010];
vector<vector<int>> G;

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

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

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

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

  dfs(0, -1, 0);

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

int s[200010];
vector<vector<int>> G;

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

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

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

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

  dfs(0, -1, 0);

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPR(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
template <typename Iter>
std::string join(Iter begin, Iter end, std::string const &separator) {
  std::ostringstream result;
  if (begin != end) {
    result << *begin++;
  };
  while (begin != end) {
    result << separator << *begin++;
  };
  return result.str();
}
using namespace std;
typedef long long ll;
void dfs(vector<vector<ll>> edges, vector<ll> actions, vector<ll> &values,
         ll current_node, ll parent_value, ll parent_node) {
  values[current_node] = parent_value + actions[current_node];
  for (auto e : edges[current_node]) {
    if (e == parent_node) {
      continue;
    }
    dfs(edges, actions, values, e, values[current_node], current_node);
  }
}
void solve() {
  ll N, Q;
  cin >> N >> Q;
  vector<vector<ll>> edges(N, vector<ll>());
  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    edges[a - 1].push_back(b - 1);
    edges[b - 1].push_back(a - 1);
  }
  vector<ll> actions(N);
  REP(i, Q) {
    ll p, x;
    cin >> p >> x;
    actions[p - 1] += x;
  }
  vector<ll> values(N);
  dfs(edges, actions, values, 0, 0, -1);
  cout << join(ALL(values), " ") << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPR(i, n) for (int i = (int)(n); i > 0; i--)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
template <typename Iter>
std::string join(Iter begin, Iter end, std::string const &separator) {
  std::ostringstream result;
  if (begin != end) {
    result << *begin++;
  };
  while (begin != end) {
    result << separator << *begin++;
  };
  return result.str();
}
using namespace std;
typedef long long ll;
void dfs(vector<vector<ll>> &edges, vector<ll> &actions, vector<ll> &values,
         ll current_node, ll parent_value, ll parent_node) {
  values[current_node] = parent_value + actions[current_node];
  for (auto e : edges[current_node]) {
    if (e == parent_node) {
      continue;
    }
    dfs(edges, actions, values, e, values[current_node], current_node);
  }
}
void solve() {
  ll N, Q;
  cin >> N >> Q;
  vector<vector<ll>> edges(N, vector<ll>());
  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    edges[a - 1].push_back(b - 1);
    edges[b - 1].push_back(a - 1);
  }
  vector<ll> actions(N);
  REP(i, Q) {
    ll p, x;
    cin >> p >> x;
    actions[p - 1] += x;
  }
  vector<ll> values(N);
  dfs(edges, actions, values, 0, 0, -1);
  cout << join(ALL(values), " ") << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02936 (C++)

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

#define fi first
#define se second
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// int vvi[200010][200010];
// int n;
bool visited[200010];

void dfs(vector<vector<int>> &vvi, vector<ll> &vi, ll p, int i) {
  // cout << "p: " << p <<  " i: " << i << endl;
  vi[i] += p;
  visited[i] = true;
  /*for(auto k : vvi[i]) {
          cout << k << " ";
  }
  cout << endl;*/
  for (auto j : vvi[i]) {
    if (!visited[j])
      dfs(vvi, vi, vi[i], j);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> vvi(n, vector<int>(n));
  rep(i, n) rep(j, n) vvi[i][j] = 0;
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    a--;
    b--;
    vvi[a].pb(b);
    vvi[b].pb(a);
  }
  rep(i, n) visited[i] = false;
  vector<ll> vi(n, 0);
  int p, x;
  rep(i, q) {
    cin >> p >> x;
    vi[p - 1] += x;
  }
  // cout << "debug" << endl;
  rep(i, n) {
    // cout << vi[i] << endl;
  }
  // cout << "debug" << endl;
  dfs(vvi, vi, 0, 0);
  rep(i, n) {
    cout << vi[i];
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }

  return 0;
}


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

#define fi first
#define se second
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// int vvi[200010][200010];
// int n;
bool visited[200010];

void dfs(vector<vector<int>> &vvi, vector<ll> &vi, ll p, int i) {
  // cout << "p: " << p <<  " i: " << i << endl;
  vi[i] += p;
  visited[i] = true;
  /*for(auto k : vvi[i]) {
          cout << k << " ";
  }
  cout << endl;*/
  for (auto j : vvi[i]) {
    if (!visited[j])
      dfs(vvi, vi, vi[i], j);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<vector<int>> vvi(n, vector<int>());
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    a--;
    b--;
    vvi[a].pb(b);
    vvi[b].pb(a);
  }
  rep(i, n) visited[i] = false;
  vector<ll> vi(n, 0);
  int p, x;
  rep(i, q) {
    cin >> p >> x;
    vi[p - 1] += x;
  }
  // cout << "debug" << endl;
  rep(i, n) {
    // cout << vi[i] << endl;
  }
  // cout << "debug" << endl;
  dfs(vvi, vi, 0, 0);
  rep(i, n) {
    cout << vi[i];
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }

  return 0;
}
Problem p02936 (C++)

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

#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pb push_back

using ll = long long;
using pii = pair<int, int>;

template <typename T> using V = vector<T>;

const int N = 1e5 + 10;
ll add[N], ans[N];
vector<int> adj[N];

void dfs(int cur, int par) {
  ans[cur] += add[cur];
  for (auto nxt : adj[cur]) {
    if (nxt == par)
      continue;
    add[nxt] += add[cur];
    dfs(nxt, cur);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  while (q--) {
    int x, p;
    cin >> p >> x;
    add[p] += (ll)x;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " \n"[i == n];
  }
}

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

#define all(a) a.begin(), a.end()
#define ff first
#define ss second
#define pb push_back

using ll = long long;
using pii = pair<int, int>;

template <typename T> using V = vector<T>;

const int N = 2e5 + 10;
ll add[N], ans[N];
vector<int> adj[N];

void dfs(int cur, int par) {
  ans[cur] += add[cur];
  for (auto nxt : adj[cur]) {
    if (nxt == par)
      continue;
    add[nxt] += add[cur];
    dfs(nxt, cur);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  while (q--) {
    int x, p;
    cin >> p >> x;
    add[p] += (ll)x;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " \n"[i == n];
  }
}
Problem p02936 (C++)

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

using namespace std;
typedef pair<int, int> ii;

struct node {
  int counter = 0;
  vector<int> neighbors;
  bool visited = false;
};

void dfs(vector<node> &nodes, int subroot) {
  auto parent = nodes[subroot];
  for (int neighbor : nodes[subroot].neighbors) {
    auto n = nodes[neighbor];
    if (!n.visited) {
      n.visited = true;
      n.counter += parent.counter;
      dfs(nodes, neighbor);
    }
  }
}

int main() {
  // input
  int n, q;
  cin >> n >> q;
  vector<node> nodes(n + 1);

  // n-1 edges >> adj_list
  for (int i = 0; i < n - 1; ++i) {
    int node1, node2;
    cin >> node1 >> node2;
    nodes[node1].neighbors.push_back(node2);
    nodes[node2].neighbors.push_back(node1);
  }

  // q operations
  for (int i = 0; i < q; ++i) {
    int node, counter;
    cin >> node >> counter;
    nodes[node].counter += counter;
  }

  // dfs and add every subroot's counter to its children
  nodes[1].visited = true;
  dfs(nodes, 1);

  // output
  for (int i = 1; i < n + 1; ++i) {
    cout << nodes[i].counter << " ";
  }
}

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

using namespace std;
typedef pair<int, int> ii;

struct node {
  int counter = 0;
  vector<int> neighbors;
  bool visited = false;
};

void dfs(vector<node> &nodes, int subroot) {
  auto parent = nodes[subroot];
  for (int neighbor : parent.neighbors) {
    auto &n = nodes[neighbor];
    if (!n.visited) {
      n.visited = true;
      n.counter += parent.counter;
      dfs(nodes, neighbor);
    }
  }
}

int main() {
  // input
  int n, q;
  cin >> n >> q;
  vector<node> nodes(n + 1);

  // n-1 edges >> adj_list
  for (int i = 0; i < n - 1; ++i) {
    int node1, node2;
    cin >> node1 >> node2;
    nodes[node1].neighbors.push_back(node2);
    nodes[node2].neighbors.push_back(node1);
  }

  // q operations
  for (int i = 0; i < q; ++i) {
    int node, counter;
    cin >> node >> counter;
    nodes[node].counter += counter;
  }

  // dfs and add every subroot's counter to its children
  nodes[1].visited = true;
  dfs(nodes, 1);

  // output
  for (int i = 1; i < n + 1; ++i) {
    cout << nodes[i].counter << " ";
  }
}
Problem p02936 (C++)

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

int n, q, cnt, h[200005], nex[200005], to[200005 << 1], ans[200005];

void addedge(int x, int y) {
  nex[++cnt] = h[x];
  to[cnt] = y;
  h[x] = cnt;
}

void dfs(int x, int f) {
  ans[x] += ans[f];
  for (int i = h[x]; i; i = nex[i])
    if (to[i] != f)
      dfs(to[i], x);
}

int main() {
  cin >> n >> q;
  for (int i = 1, x, y; i < n && cin >> x >> y; i++) {
    addedge(x, y);
    addedge(y, x);
  }
  while (q--) {
    int x, v;
    cin >> x >> v;
    ans[x] += v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << ' ';
  cout << endl;

  return 0;
}

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

int n, q, cnt, h[200005], nex[200005 << 1], to[200005 << 1], ans[200005];

void addedge(int x, int y) {
  nex[++cnt] = h[x];
  to[cnt] = y;
  h[x] = cnt;
}

void dfs(int x, int f) {
  ans[x] += ans[f];
  for (int i = h[x]; i; i = nex[i])
    if (to[i] != f)
      dfs(to[i], x);
}

int main() {
  cin >> n >> q;
  for (int i = 1, x, y; i < n && cin >> x >> y; i++) {
    addedge(x, y);
    addedge(y, x);
  }
  while (q--) {
    int x, v;
    cin >> x >> v;
    ans[x] += v;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << ' ';
  cout << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1e9 + 7;
int INF = 2e18;

struct leaves {
  int up;
  vector<int> down;
  int point = 0;
};
int N, Q;

int ans[100100];
leaves tree[100100];

void dfs(int direct, int point) {
  tree[direct].point += point;
  for (int i = 0; i < tree[direct].down.size(); i++) {
    dfs(tree[direct].down[i], tree[direct].point);
  }
}

signed main() {
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    tree[a].down.push_back(b);
    tree[b].up = a;
  }
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    tree[a].point += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= N; i++) {
    if (i != N)
      cout << tree[i].point << " ";
    else
      cout << tree[i].point << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1e9 + 7;
int INF = 2e18;

struct leaves {
  int up;
  vector<int> down;
  int point = 0;
};
int N, Q;

int ans[250000];
leaves tree[250000];

void dfs(int direct, int point) {
  tree[direct].point += point;
  for (int i = 0; i < tree[direct].down.size(); i++) {
    dfs(tree[direct].down[i], tree[direct].point);
  }
}

signed main() {
  cin >> N >> Q;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    tree[a].down.push_back(b);
    tree[b].up = a;
  }
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    tree[a].point += b;
  }
  dfs(1, 0);
  for (int i = 1; i <= N; i++) {
    if (i != N)
      cout << tree[i].point << " ";
    else
      cout << tree[i].point << endl;
  }

  return 0;
}
Problem p02936 (C++)

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

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

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll n, q;
ll a[100010], b[100010];
vector<ll> g[100010];

void dfs(ll v, ll pre, ll sum) {
  sum += a[v];
  b[v] += sum;
  for (ll nv : g[v]) {
    if (nv == pre)
      continue;
    dfs(nv, v, sum);
  }
}

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

  cin >> n >> q;
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  while (q--) {
    ll v, x;
    cin >> v >> x;
    v--;
    a[v] += x;
  }

  dfs(0, -1, 0);
  rep(i, n) cout << b[i] << endl;

  return 0;
}


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

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

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll n, q;
ll a[200010], b[200010];
vector<ll> g[200010];

void dfs(ll v, ll pre, ll sum) {
  sum += a[v];
  b[v] += sum;
  for (ll nv : g[v]) {
    if (nv == pre)
      continue;
    dfs(nv, v, sum);
  }
}

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

  cin >> n >> q;
  rep(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  while (q--) {
    ll v, x;
    cin >> v >> x;
    v--;
    a[v] += x;
  }

  dfs(0, -1, 0);
  rep(i, n) cout << b[i] << endl;

  return 0;
}
Problem p02936 (C++)

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

const int N = 1e5;

vector<int> arr[N];
vector<int> val(N, 0);
vector<int> ans(N);
bool seen[N];

void sol(int v, int value) {
  ans[v] = val[v] + value;
  seen[v] = true;

  for (int i = 0; i < arr[v].size(); i++) {
    if (!seen[arr[v][i]])
      sol(arr[v][i], value + val[v]);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

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

  memset(seen, false, sizeof(seen));

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;

    arr[u - 1].push_back(v - 1);
    arr[v - 1].push_back(u - 1);
  }

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

    val[p - 1] += x;
  }

  sol(0, 0);

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

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

const int N = 2e5 + 7;

vector<int> arr[N];
vector<int> val(N, 0);
vector<int> ans(N);
bool seen[N];

void sol(int v, int value) {
  ans[v] = val[v] + value;
  seen[v] = true;

  for (int i = 0; i < arr[v].size(); i++) {
    if (!seen[arr[v][i]])
      sol(arr[v][i], value + val[v]);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

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

  memset(seen, false, sizeof(seen));

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;

    arr[u - 1].push_back(v - 1);
    arr[v - 1].push_back(u - 1);
  }

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

    val[p - 1] += x;
  }

  sol(0, 0);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define SIZE 100005
vll tree[SIZE];
ll arr[SIZE];
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll N, Q;
  cin >> N >> Q;
  for (ll i = 1; i < N; ++i) {
    ll u, v;
    cin >> u >> v;
    tree[u].pb(v);
  }
  while (Q--) {
    ll node, value;
    cin >> node >> value;
    arr[node] += value;
  }
  for (ll i = 1; i <= N; ++i) {
    vll ::iterator itr;
    for (itr = tree[i].begin(); itr != tree[i].end(); ++itr) {
      arr[*itr] += arr[i];
    }
  }
  for (int i = 1; i <= N; ++i) {
    cout << arr[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define SIZE 200005
vll tree[SIZE];
ll arr[SIZE];
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll N, Q;
  cin >> N >> Q;
  for (ll i = 1; i < N; ++i) {
    ll u, v;
    cin >> u >> v;
    tree[u].pb(v);
  }
  while (Q--) {
    ll node, value;
    cin >> node >> value;
    arr[node] += value;
  }
  for (ll i = 1; i <= N; ++i) {
    vll ::iterator itr;
    for (itr = tree[i].begin(); itr != tree[i].end(); ++itr) {
      arr[*itr] += arr[i];
    }
  }
  for (int i = 1; i <= N; ++i) {
    cout << arr[i] << endl;
  }
}
Problem p02936 (C++)

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

vector<long long> adj[20005];
vector<long long> res;

void dfs(long long v, long long p = -1) {
  for (auto itr : adj[v]) {
    if (itr == p)
      continue;
    res[itr] += res[v]; //
    dfs(itr, v);
  }
}

int main() {
  long long n, q;
  cin >> n >> q;
  long long a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long p, x;
  res.resize(n);
  for (int i = 0; i < q; i++) {
    cin >> p >> x;
    p--;
    res[p] += x;
  }

  dfs(0); // Yo !

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

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

vector<long long> adj[200005];
vector<long long> res;

void dfs(long long v, long long p = -1) {
  for (auto itr : adj[v]) {
    if (itr == p)
      continue;
    res[itr] += res[v]; //
    dfs(itr, v);
  }
}

int main() {
  long long n, q;
  cin >> n >> q;
  long long a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long p, x;
  res.resize(n);
  for (int i = 0; i < q; i++) {
    cin >> p >> x;
    p--;
    res[p] += x;
  }

  dfs(0); // Yo !

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llong long long
#define INF (__INT32_MAX__ / 2)
#define EPS 10e-8
#define MAX_N 100000

using namespace std;
typedef pair<int, int> ipair;
typedef pair<llong, llong> llpair;
llong modulo(llong left, llong right) { return (left % right + right) % right; }

int N, Q;
llpair nodes[MAX_N]; //(diff, cnter)
vector<int> adj_list[MAX_N];

bool isvisited[MAX_N];
void dfs(int node) {
  isvisited[node] = true;
  int cnter = nodes[node].second;

  for (int i = 0; i < adj_list[node].size(); ++i) {
    int next = adj_list[node][i];
    if (isvisited[next])
      continue;

    nodes[next].second = cnter + nodes[next].first;
    dfs(next);
  }
}

int main(int argc, char **argv) {
  cin >> N >> Q;
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    adj_list[a - 1].push_back(b - 1);
    adj_list[b - 1].push_back(a - 1);
  }

  // init
  for (int i = 0; i < N; ++i)
    nodes[i].first = nodes[i].second = 0;

  for (int i = 0; i < Q; ++i) {
    int p, x;
    cin >> p >> x;
    nodes[p - 1].first += x;
  }

  // DFS
  nodes[0].second = nodes[0].first;
  dfs(0);

  for (int i = 0; i < N; ++i) {
    if (i != 0)
      cout << " ";
    cout << nodes[i].second;
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llong long long
#define INF (__INT32_MAX__ / 2)
#define EPS 10e-8
#define MAX_N 200000

using namespace std;
typedef pair<int, int> ipair;
typedef pair<llong, llong> llpair;
llong modulo(llong left, llong right) { return (left % right + right) % right; }

int N, Q;
llpair nodes[MAX_N]; //(diff, cnter)
vector<int> adj_list[MAX_N];

bool isvisited[MAX_N];
void dfs(int node) {
  isvisited[node] = true;
  int cnter = nodes[node].second;

  for (int i = 0; i < adj_list[node].size(); ++i) {
    int next = adj_list[node][i];
    if (isvisited[next])
      continue;

    nodes[next].second = cnter + nodes[next].first;
    dfs(next);
  }
}

int main(int argc, char **argv) {
  cin >> N >> Q;
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    adj_list[a - 1].push_back(b - 1);
    adj_list[b - 1].push_back(a - 1);
  }

  // init
  for (int i = 0; i < N; ++i)
    nodes[i].first = nodes[i].second = 0;

  for (int i = 0; i < Q; ++i) {
    int p, x;
    cin >> p >> x;
    nodes[p - 1].first += x;
  }

  // DFS
  nodes[0].second = nodes[0].first;
  dfs(0);

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

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc138/tasks/abc138
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; ++i) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  vector<int> parent(N, -1); // 頂点 i の親
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (const auto &nv : G[v]) {
      if (parent[v] == nv)
        continue;
      parent[nv] = v;
      que.push(nv);
    }
  }
  vector<int> res(N, 0);
  for (int j = 0; j < Q; ++j) {
    queue<int> vque;
    // cout << "p[j] = " << p[j] << ", " << "x[j] = " << x[j] << endl;
    vque.push(p[j]);
    while (!vque.empty()) {
      int v = vque.front();
      vque.pop();
      res[v] += x[j];
      for (const auto &nv : G[v]) {
        // cout << "nv = " << nv << endl;
        if (parent[v] == nv)
          continue;
        vque.push(nv);
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    cout << res[i];
    if (i != N - 1)
      cout << " ";
    else
      cout << endl;
  }
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/abc138/tasks/abc138
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; ++i) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  vector<int> parent(N, -1); // 頂点 i の親
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (const auto &nv : G[v]) {
      if (parent[v] == nv)
        continue;
      parent[nv] = v;
      que.push(nv);
    }
  }
  vector<int> res(N, 0);
  for (int j = 0; j < Q; ++j) {
    res[p[j]] += x[j];
  }

  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (const auto &nv : G[v]) {
      if (parent[v] == nv)
        continue;
      res[nv] += res[v];
      que.push(nv);
    }
  }
  for (int i = 0; i < N; ++i) {
    cout << res[i];
    if (i != N - 1)
      cout << " ";
    else
      cout << endl;
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// abc138d
int n, q;
vector<int> adj[200002];
ll cnt[200002];
void dfs(int s, int p) {
  for (int i = 0; i < adj[s].size(); i++) {
    if (adj[s][i] == p)
      continue;
    cnt[adj[s][i]] += cnt[s];
    dfs(adj[s][i], cnt[adj[s][i]]);
  }
}

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1 << 21;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// abc138d
int n, q;
vector<int> adj[200002];
ll cnt[200002];
void dfs(int s, int p) {
  for (int i = 0; i < adj[s].size(); i++) {
    if (adj[s][i] == p)
      continue;
    cnt[adj[s][i]] += cnt[s];
    dfs(adj[s][i], s);
  }
}

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

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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = std::int64_t;
using P = std::tuple<int, int>;

int N, Q;
std::vector<int> G[200100];
ll cnt[200100];
int l[200100], r[200100];
int p, x;

void dfs(int v, int p, int &c) {
  l[v] = c;
  c += 1;

  for (int w : G[v]) {
    if (w == p) {
      continue;
    }
    dfs(w, v, c);
  }

  r[v] = c;
  c += 1;
}

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

  std::cin >> N >> Q;

  for (int i = 0; i < N - 1; ++i) {
    int A, B;
    std::cin >> A >> B;

    G[A].emplace_back(B);
    G[B].emplace_back(A);
  }

  int c = 0;
  dfs(1, -1, c);

  for (int i = 0; i < Q; ++i) {
    std::cin >> p >> x;

    cnt[l[p]] += x;
    cnt[r[p]] -= x;
  }

  for (int i = 1; i < c; ++i) {
    cnt[i] += cnt[i - 1];
  }

  for (int i = 1; i <= N; ++i) {
    std::cout << cnt[l[i]] << " \n"[i == N];
  }
}


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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = std::int64_t;
using P = std::tuple<int, int>;

int N, Q;
std::vector<int> G[200100];
ll cnt[400100];
int l[200100], r[200100];
int p, x;

void dfs(int v, int p, int &c) {
  l[v] = c;
  c += 1;

  for (int w : G[v]) {
    if (w == p) {
      continue;
    }
    dfs(w, v, c);
  }

  r[v] = c;
  c += 1;
}

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

  std::cin >> N >> Q;

  for (int i = 0; i < N - 1; ++i) {
    int A, B;
    std::cin >> A >> B;

    G[A].emplace_back(B);
    G[B].emplace_back(A);
  }

  int c = 0;
  dfs(1, -1, c);

  for (int i = 0; i < Q; ++i) {
    std::cin >> p >> x;

    cnt[l[p]] += x;
    cnt[r[p]] -= x;
  }

  for (int i = 1; i < c; ++i) {
    cnt[i] += cnt[i - 1];
  }

  for (int i = 1; i <= N; ++i) {
    std::cout << cnt[l[i]] << " \n"[i == N];
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF 1e9
using namespace std;
typedef long long ll;

vector<ll> ans(100000);
vector<queue<int>> ds(100000);
vector<ll> vs(100000, 0);

void dfs(int idx, int prv_val) {
  int cur_val;
  cur_val = prv_val + vs.at(idx);
  ans.at(idx) = cur_val;
  while (!ds.at(idx).empty()) {
    int nxt = ds.at(idx).front();
    dfs(nxt, cur_val);
    ds.at(idx).pop();
  }
}

signed main() {
  // std::ifstream in ("input.txt"); std::cin.rdbuf(in.rdbuf());

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

  int frm, to;
  REP(i, n - 1) {
    cin >> frm >> to;
    ds.at(frm - 1).push(to - 1);
  }

  int p, x;
  REP(i, q) {
    cin >> p >> x;
    vs.at(p - 1) += x;
  }

  dfs(0, 0);

  cout << ans.at(0);
  REP(i, n - 1) { cout << " " << ans.at(i + 1); }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define INF 1e9
using namespace std;
typedef long long ll;

vector<ll> ans(1000000);
vector<queue<int>> ds(1000000);
vector<ll> vs(1000000, 0);

void dfs(int idx, int prv_val) {
  int cur_val;
  cur_val = prv_val + vs.at(idx);
  ans.at(idx) = cur_val;
  while (!ds.at(idx).empty()) {
    int nxt = ds.at(idx).front();
    dfs(nxt, cur_val);
    ds.at(idx).pop();
  }
}

signed main() {
  // std::ifstream in ("input.txt"); std::cin.rdbuf(in.rdbuf());

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

  int frm, to;
  REP(i, n - 1) {
    cin >> frm >> to;
    ds.at(frm - 1).push(to - 1);
  }

  int p, x;
  REP(i, q) {
    cin >> p >> x;
    vs.at(p - 1) += x;
  }

  dfs(0, 0);

  cout << ans.at(0);
  REP(i, n - 1) { cout << " " << ans.at(i + 1); }
  cout << endl;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define N 200000
vector<int> v[N];
int n, q, tot, d[N], l[N], r[N], s[N];

void dfs(int x, int fa) {
  d[x] = ++tot, l[x] = r[x] = tot;
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] == fa)
      continue;
    dfs(v[x][i], x);
    l[x] = min(l[x], l[v[x][i]]);
    r[x] = max(r[x], r[v[x][i]]);
  }
}

int main() {
  // freopen("sample.in", "r", stdin);
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].pb(y), v[y].pb(x);
  }
  dfs(1, 0);
  for (int i = 1; i <= q; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    s[l[p]] += x, s[r[p] + 1] -= x;
  }
  for (int i = 1; i <= n; i++)
    s[i] = s[i] + s[i - 1];
  for (int i = 1; i <= n; i++)
    printf("%d ", s[d[i]]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define N 300000
vector<int> v[N];
int n, q, tot, d[N], l[N], r[N], s[N];

void dfs(int x, int fa) {
  d[x] = ++tot, l[x] = r[x] = tot;
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] == fa)
      continue;
    dfs(v[x][i], x);
    l[x] = min(l[x], l[v[x][i]]);
    r[x] = max(r[x], r[v[x][i]]);
  }
}

int main() {
  // freopen("sample.in", "r", stdin);
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].pb(y), v[y].pb(x);
  }
  dfs(1, 0);
  for (int i = 1; i <= q; i++) {
    int p, x;
    scanf("%d%d", &p, &x);
    s[l[p]] += x, s[r[p] + 1] -= x;
  }
  for (int i = 1; i <= n; i++)
    s[i] = s[i] + s[i - 1];
  for (int i = 1; i <= n; i++)
    printf("%d ", s[d[i]]);
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll sum[200010] = {0};
bool visited[200010];
vector<ll> g[100010];

int main(void) {
  ll n, q;
  cin >> n >> q;
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (ll i = 0; i < q; i++) {
    ll p, x;
    cin >> p >> x;
    sum[p] += x;
  }
  queue<ll> que;
  que.push(1);
  visited[1] = true;
  while (!que.empty()) {
    ll p = que.front();
    que.pop();
    for (ll i = 0; i < g[p].size(); i++) {
      ll next = g[p][i];
      if (visited[next])
        continue;
      sum[next] += sum[p];
      que.push(next);
      visited[next] = true;
    }
  }
  for (ll i = 1; i <= n; i++) {
    if (i == n)
      cout << sum[i] << endl;
    else
      cout << sum[i] << " ";
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll sum[200010] = {0};
bool visited[200010];
vector<ll> g[200010];

int main(void) {
  ll n, q;
  cin >> n >> q;
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (ll i = 0; i < q; i++) {
    ll p, x;
    cin >> p >> x;
    sum[p] += x;
  }
  queue<ll> que;
  que.push(1);
  visited[1] = true;
  while (!que.empty()) {
    ll p = que.front();
    que.pop();
    for (ll i = 0; i < g[p].size(); i++) {
      ll next = g[p][i];
      if (visited[next])
        continue;
      sum[next] += sum[p];
      que.push(next);
      visited[next] = true;
    }
  }
  for (ll i = 1; i <= n; i++) {
    if (i == n)
      cout << sum[i] << endl;
    else
      cout << sum[i] << " ";
  }
  return 0;
}