Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/*input
6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10
*/

// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define is_empty(v) v.empty()
#define vs vector<string>
#define vvi vector<vector<ll, ll>>
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define inf 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repr(i, a, b) for (ll int i = a; i > b; i--)
#define reprr(i, a, b) for (ll int i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define sum(v) accumulate(v.begin(), v.end(), (ll)0)
// map <long long int,long long int> ma;
// set <long long int, greater <long long int> > s;
const ll N = 1e5 + 5;
vector<vi> adj(N, vi());
ll a[N];
ll ans[N];
void dfs(ll u, ll p) {
  ans[u] += a[u];
  rep(i, 0, sz(adj[u])) {
    if (adj[u][i] == p)
      continue;
    a[adj[u][i]] += a[u];
    dfs(adj[u][i], u);
  }
  return;
}

int solve() {
  ll n, q;
  cin >> n >> q;
  rep(i, 0, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  while (q--) {
    ll v, x;
    cin >> v >> x;
    v--;
    a[v] += x;
  }
  dfs(0, -1);
  rep(i, 0, n) cout << ans[i] << " ";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Fixed submission:
/*input
6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10
*/

// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define is_empty(v) v.empty()
#define vs vector<string>
#define vvi vector<vector<ll, ll>>
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define inf 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repr(i, a, b) for (ll int i = a; i > b; i--)
#define reprr(i, a, b) for (ll int i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define sum(v) accumulate(v.begin(), v.end(), (ll)0)
// map <long long int,long long int> ma;
// set <long long int, greater <long long int> > s;
const ll N = 2e5 + 5;
vector<vi> adj(N, vi());
ll a[N];
ll ans[N];
void dfs(ll u, ll p) {
  ans[u] += a[u];
  rep(i, 0, sz(adj[u])) {
    if (adj[u][i] == p)
      continue;
    a[adj[u][i]] += a[u];
    dfs(adj[u][i], u);
  }
  return;
}

int solve() {
  ll n, q;
  cin >> n >> q;
  rep(i, 0, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  while (q--) {
    ll v, x;
    cin >> v >> x;
    v--;
    a[v] += x;
  }
  dfs(0, -1);
  rep(i, 0, n) cout << ans[i] << " ";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
}