Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/**
 *   author: malviyanshiv
 *   created: 18 August 2019  17:46:30
 **/

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a - 1; i >= b; i--)
#define FORE(a) for (auto ele : a)
#define ALL(a) begin(a), end(a)
#define RPB(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define PB push_back
#define MP make_pair
#define MOD 1000000007
#define INF 0x7fffffff
#define LINF 2000000000000000007
#define F first
#define S second
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define SET(a, b) memset(a, b, sizeof(a))

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

typedef long long ll;
typedef pair<int, int> PII;

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

#define MAX 100005

vector<int> adj[MAX];
vector<int> value(MAX, 0);
vector<int> final(MAX, 0);
vector<bool> vis(MAX, false);

void dfs(int n, int val) {
  final[n] = val;
  vis[n] = true;
  for (int nei : adj[n]) {
    if (!vis[nei]) {
      dfs(nei, val + value[nei]);
    }
  }
}

int main() {
  optimizeIO();

  int n, q, a, b;
  cin >> n >> q;
  REP(i, 0, n - 1) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  while (q--) {
    cin >> a >> b;
    value[a] += b;
  }
  dfs(1, value[1]);
  final[1] = value[1];
  for (int i = 1; i <= n; i++)
    cout << final[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
/**
 *   author: malviyanshiv
 *   created: 18 August 2019  17:46:30
 **/

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = a - 1; i >= b; i--)
#define FORE(a) for (auto ele : a)
#define ALL(a) begin(a), end(a)
#define RPB(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define PB push_back
#define MP make_pair
#define MOD 1000000007
#define INF 0x7fffffff
#define LINF 2000000000000000007
#define F first
#define S second
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define SET(a, b) memset(a, b, sizeof(a))

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

typedef long long ll;
typedef pair<int, int> PII;

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

#define MAX 200005

vector<int> adj[MAX];
vector<int> value(MAX, 0);
vector<int> final(MAX, 0);
vector<bool> vis(MAX, false);

void dfs(int n, int val) {
  final[n] = val;
  vis[n] = true;
  for (int nei : adj[n]) {
    if (!vis[nei]) {
      dfs(nei, val + value[nei]);
    }
  }
}

int main() {
  optimizeIO();

  int n, q, a, b;
  cin >> n >> q;
  REP(i, 0, n - 1) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  while (q--) {
    cin >> a >> b;
    value[a] += b;
  }
  dfs(1, value[1]);
  final[1] = value[1];
  for (int i = 1; i <= n; i++)
    cout << final[i] << " ";
  cout << endl;
  return 0;
}