Problem p02936 (C++)

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

using namespace std;

#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

int mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B> istream &operator>>(istream &, pair<A, B> &);
template <typename A> istream &operator>>(istream &, vector<A> &);
void add(int &, int);
int mul(int, int);
int powz(int, int);

template <typename A> ostream &operator<<(ostream &, vector<A> &);

const int N = 100002;

int dp[N], ans[N];
vi adj[N];

void dfs(int u, int p) {
  if (p != -1) {
    ans[u] += ans[p];
  }
  for (auto i : adj[u]) {
    if (i != p) {
      dfs(i, u);
    }
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  while (q--) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tc = 1;
  //~ cin>>tc;
  for (int _ = 0; _ < tc; _++) {
    //~ cout<<"Case #"<<_+1<<": ";
    solve();
    if (_ != tc - 1)
      cout << "\n";
  }
}

int mul(int a, int b) { return ((a)*1ll * (b)) % mod; }

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

int powz(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    b /= 2;
    a = mul(a, a);
  }
  return res;
}

template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
  input >> x.F >> x.S;
  return input;
}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}


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

using namespace std;

#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

int mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B> istream &operator>>(istream &, pair<A, B> &);
template <typename A> istream &operator>>(istream &, vector<A> &);
void add(int &, int);
int mul(int, int);
int powz(int, int);

template <typename A> ostream &operator<<(ostream &, vector<A> &);

const int N = 200002;

int dp[N], ans[N];
vi adj[N];

void dfs(int u, int p) {
  if (p != -1) {
    ans[u] += ans[p];
  }
  for (auto i : adj[u]) {
    if (i != p) {
      dfs(i, u);
    }
  }
}

void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  while (q--) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tc = 1;
  //~ cin>>tc;
  for (int _ = 0; _ < tc; _++) {
    //~ cout<<"Case #"<<_+1<<": ";
    solve();
    if (_ != tc - 1)
      cout << "\n";
  }
}

int mul(int a, int b) { return ((a)*1ll * (b)) % mod; }

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

int powz(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    b /= 2;
    a = mul(a, a);
  }
  return res;
}

template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
  input >> x.F >> x.S;
  return input;
}

template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}