Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
using namespace __gnu_pbds;
#define st first
#define nd second
void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (int _ = 0; _ < n; ++_)                                                \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"
#define maxn 100001
#define M 1000000007
#define int long long
#define inf 1e18
#define ninf -1e17
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int>> ht;

vector<int> adj[maxn];
vector<int> ans(maxn);
void dfs(int u, int p) {
  if (p != -1) {
    ans[u] += ans[p];
  }
  for (auto v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    cin >> p >> q;
    adj[p].push_back(q);
    adj[q].push_back(p);
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    ans[x] += y;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
using namespace __gnu_pbds;
#define st first
#define nd second
void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (int _ = 0; _ < n; ++_)                                                \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"
#define maxn 300001
#define M 1000000007
#define int long long
#define inf 1e18
#define ninf -1e17
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef cc_hash_table<int, int, hash<int>> ht;

vector<int> adj[maxn];
vector<int> ans(maxn);
void dfs(int u, int p) {
  if (p != -1) {
    ans[u] += ans[p];
  }
  for (auto v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    cin >> p >> q;
    adj[p].push_back(q);
    adj[q].push_back(p);
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    ans[x] += y;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
}