Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define present(c, x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define inf 1e18
#define flush fflush(stdout);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[100001];
ll ar[100001];
void dfs(ll ver, ll p, ll ch) {
  ar[ver] += ch;
  // debug2(ver,ch);
  for (ll i = 0; i < v[ver].size(); i++) {
    if (v[ver][i] != p) {
      dfs(v[ver][i], ver, ar[ver]);
    }
  }
}
int main() {
  boost ll n, q;
  cin >> n >> q;
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }

  while (q--) {
    ll ver, x;
    cin >> ver >> x;
    ar[ver] += x;
  }
  dfs(1, -1, 0);
  for (ll i = 1; i <= n; i++) {
    cout << ar[i] << " ";
  }
  return 0;
}


Fixed submission:
/*
  Vivek Rathi
  CSE 2nd Year
  MNNIT
*/
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define present(c, x) ((c).find(x) != (c).end())
#define null NULL
#define mp make_pair
#define fi first
#define se second
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define inf 1e18
#define flush fflush(stdout);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}
//-------------------------------Template--Above------------------------------------------------
vector<ll> v[200001];
ll ar[200001];
void dfs(ll ver, ll p, ll ch) {
  ar[ver] += ch;
  // debug2(ver,ch);
  for (ll i = 0; i < v[ver].size(); i++) {
    if (v[ver][i] != p) {
      dfs(v[ver][i], ver, ar[ver]);
    }
  }
}
int main() {
  boost ll n, q;
  cin >> n >> q;
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].pb(b);
    v[b].pb(a);
  }

  while (q--) {
    ll ver, x;
    cin >> ver >> x;
    ar[ver] += x;
  }
  dfs(1, -1, 0);
  for (ll i = 1; i <= n; i++) {
    cout << ar[i] << " ";
  }
  return 0;
}