Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
vi adj[123456];
bool vis[123456];
int var[123456];
int ans[123456];

void dfs(int u) {
  vis[u] = 1;
  int c = 1;
  for (int x : adj[u]) {
    if (!vis[x]) {
      ans[x] += ans[u];
      dfs(x);
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________

  int n, q;
  cin >> n >> q;
  zip(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  zip(i, 0, q) {
    int a, b;
    cin >> a >> b;
    var[a] += b;
    ans[a] += b;
  }

  dfs(1);
  rep(i, 1, n) { cout << ans[i] << " "; }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
vi adj[1234567];
bool vis[1234567];
int var[1234567];
int ans[1234567];

void dfs(int u) {
  vis[u] = 1;
  int c = 1;
  for (int x : adj[u]) {
    if (!vis[x]) {
      ans[x] += ans[u];
      dfs(x);
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________

  int n, q;
  cin >> n >> q;
  zip(i, 0, n - 1) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  zip(i, 0, q) {
    int a, b;
    cin >> a >> b;
    var[a] += b;
    ans[a] += b;
  }

  dfs(1);
  rep(i, 1, n) { cout << ans[i] << " "; }

  return 0;
}