Problem p02936 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  ABC
//
//  Created by Amarsanaa Davaasuren on 2019/08/31.
//  Copyright © 2019 Amarsanaa Davaasuren. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

class UnionFind {
public:
  vector<int> Parent;
  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int node) {
    if (Parent[node] < 0) {
      return node;
    } else {
      return Parent[node] = root(Parent[node]);
    }
  }

  int size(int node) { return -root(node); }

  bool connect(int A, int B) {
    int Aroot = root(A);
    int Broot = root(B);
    if (Aroot != Broot) {
      if (size(Aroot) < size(Broot))
        swap(Aroot, Broot);
      Parent[Aroot] += Parent[Broot];
      Parent[Broot] = Aroot;
      return true;
    } else {
      return false;
    }
  }
};

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using ll = long long;

int main() {

  int N, Q;
  cin >> N >> Q;

  vector<vector<int>> G;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<ll> value(N);
  rep(i, Q) {
    int p, x;
    cin >> p >> x;
    p--;
    value[p] += x;
  }

  auto dfs = [&](auto dfs, int child, int parent) -> void {
    for (int relative : G[child])
      if (relative != parent) {
        value[relative] += value[child];
        dfs(dfs, relative, child);
      }
  };

  dfs(dfs, 0, -1);
  rep(i, N) cout << value[i] << (i == N - 1 ? '\n' : ' ');
}


Fixed submission:
//
//  main.cpp
//  ABC
//
//  Created by Amarsanaa Davaasuren on 2019/08/31.
//  Copyright © 2019 Amarsanaa Davaasuren. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

class UnionFind {
public:
  vector<int> Parent;
  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int node) {
    if (Parent[node] < 0) {
      return node;
    } else {
      return Parent[node] = root(Parent[node]);
    }
  }

  int size(int node) { return -root(node); }

  bool connect(int A, int B) {
    int Aroot = root(A);
    int Broot = root(B);
    if (Aroot != Broot) {
      if (size(Aroot) < size(Broot))
        swap(Aroot, Broot);
      Parent[Aroot] += Parent[Broot];
      Parent[Broot] = Aroot;
      return true;
    } else {
      return false;
    }
  }
};

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
using ll = long long;

int main() {

  int N, Q;
  cin >> N >> Q;

  vector<vector<int>> G(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<ll> value(N);
  rep(i, Q) {
    int p, x;
    cin >> p >> x;
    p--;
    value[p] += x;
  }

  auto dfs = [&](auto dfs, int child, int parent) -> void {
    for (int relative : G[child])
      if (relative != parent) {
        value[relative] += value[child];
        dfs(dfs, relative, child);
      }
  };

  dfs(dfs, 0, -1);
  rep(i, N) cout << value[i] << (i == N - 1 ? '\n' : ' ');
}