Problem p02936 (C++)

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

#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline int readInt() {
  int x;
  scanf("%d", &x);
  return x;
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

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

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

struct Edge {
  int to, from;
  ll cost;
  Edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {}
};

typedef vector<vector<Edge>> AdjList;
AdjList graph;

vector<vector<int>> G(200010);
ll ans[200010];
ll plusl[200010];

void dfs(int v, ll x, int prev) {
  ans[v] = x;

  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    if (u == prev)
      continue;
    dfs(u, x + plusl[u], u);
  }
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);

  int N, Q;
  cin >> N >> Q;

  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  set<ll> st;
  REP(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    st.insert(p);
    plusl[p] += x;
  }

  dfs(0, plusl[0], -1);

  REP(i, N) {
    if (i)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
  return 0;
}

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

#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline int readInt() {
  int x;
  scanf("%d", &x);
  return x;
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

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

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

struct Edge {
  int to, from;
  ll cost;
  Edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {}
};

typedef vector<vector<Edge>> AdjList;
AdjList graph;

vector<vector<int>> G(200010);
ll ans[200010];
ll plusl[200010];

void dfs(int v, ll x, int prev) {
  ans[v] = x;

  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    if (u == prev)
      continue;
    dfs(u, x + plusl[u], v);
  }
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);

  int N, Q;
  cin >> N >> Q;

  REP(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  set<ll> st;
  REP(i, Q) {
    ll p, x;
    cin >> p >> x;
    p--;
    st.insert(p);
    plusl[p] += x;
  }

  dfs(0, plusl[0], -1);

  REP(i, N) {
    if (i)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
  return 0;
}