Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

// typedef
//------------------------------------------
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define SORT(c) sort((c).begin(), (c).end())
#define COUT(x) cout << x << endl

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const ULL INF = 1e18;
const int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

// function
//--------------------------------------------
LL gcd(LL, LL);
bool isEven(LL);
void coutY();
void coutN();

int main() {
  LL n, q;
  cin >> n >> q;
  vector<PLL> ab(n - 1);
  REP(i, n - 1) cin >> ab[i].first >> ab[i].second;
  vector<PLL> ope(q);
  REP(i, q) cin >> ope[i].first >> ope[i].second;
  REP(i, n) {
    ab[i].first--;
    ab[i].second--;
  }
  REP(i, q) ope[i].first--;
  VLL counter(n);
  REP(i, q) counter[ope[i].first] += ope[i].second;
  SORT(ab);
  REP(i, n) { counter[ab[i].second] += counter[ab[i].first]; }
  // REP(i,n-1)cout<<ab[i].first<<' '<<ab[i].second<<endl;
  REP(i, n) cout << counter[i] << ' ';
}

LL gcd(LL a, LL b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}
bool isEven(LL a) { return !((bool)(a % 2)); }
void coutY() {
  cout << "Yes" << endl;
  return;
}
void coutN() {
  cout << "No" << endl;
  return;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

// typedef
//------------------------------------------
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define SORT(c) sort((c).begin(), (c).end())
#define COUT(x) cout << x << endl

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const ULL INF = 1e18;
const int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

// function
//--------------------------------------------
LL gcd(LL, LL);
bool isEven(LL);
void coutY();
void coutN();

int main() {
  LL n, q;
  cin >> n >> q;
  vector<PLL> ab(n - 1);
  REP(i, n - 1) cin >> ab[i].first >> ab[i].second;
  vector<PLL> ope(q);
  REP(i, q) cin >> ope[i].first >> ope[i].second;
  REP(i, n) {
    ab[i].first--;
    ab[i].second--;
  }
  REP(i, q) ope[i].first--;
  VLL counter(n);
  REP(i, q) counter[ope[i].first] += ope[i].second;
  SORT(ab);
  REP(i, n - 1) { counter[ab[i].second] += counter[ab[i].first]; }
  // REP(i,n-1)cout<<ab[i].first<<' '<<ab[i].second<<endl;
  REP(i, n) cout << counter[i] << ' ';
}

LL gcd(LL a, LL b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}
bool isEven(LL a) { return !((bool)(a % 2)); }
void coutY() {
  cout << "Yes" << endl;
  return;
}
void coutN() {
  cout << "No" << endl;
  return;
}