Problem p02710 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

vector<vector<long long>> g;
vector<long long> tin, tout, t, b;
long long tmr = 0;

void dfs(long long v, long long p) {
  tin[v] = ++tmr;
  for (int i = 0; i < g[v].size(); ++i) {
    if (g[v][i] != p)
      dfs(g[v][i], v);
  }
  tout[v] = tmr;
}

void push(int v, int tl, int tr) {
  if (b[v] != -1)
    t[v] = (tr - tl + 1) * b[v];
  if (tl != tr && b[v] != -1) {
    b[v << 1] = b[v];
    b[v << 1 | 1] = b[v];
  }
  b[v] = -1;
}

void upd(long long l, long long r, long long val, long long v, long long tl,
         long long tr) {
  push(v, tl, tr);
  if (l > tr || tl > r)
    return;
  if (l <= tl && tr <= r) {
    b[v] = val;
    push(v, tl, tr);
    return;
  }
  long long tm = (tl + tr) >> 1;
  upd(l, r, val, v << 1, tl, tm);
  upd(l, r, val, v << 1 | 1, tm + 1, tr);
  t[v] = t[v << 1] + t[v << 1 | 1];
}

long long get(long long l, long long r, long long v, long long tl,
              long long tr) {
  push(v, tl, tr);
  if (l > tr || tl > r)
    return 0;
  if (l <= tl && tr <= r)
    return t[v];
  long long tm = (tl + tr) >> 1;
  return get(l, r, v << 1, tl, tm) + get(l, r, v << 1 | 1, tm + 1, tr);
}

long long cnt(long long k) { return k * (k + 1) / 2; }

bool comp(const long long &frst, const long long &scnd) {
  return tin[frst] > tin[scnd];
}

int main() {
  long long n, el;
  cin >> n;
  g.resize(n + 1);
  tin.resize(n + 1);
  tout.resize(n + 1);
  t.resize(4 * n);
  b.resize(4 * n);
  vector<vector<long long>> a(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> el;
    a[el].push_back(i);
  }
  long long fr, to;
  for (int i = 0; i < n - 1; ++i) {
    cin >> fr >> to;
    g[fr].push_back(to);
    g[to].push_back(fr);
  }
  dfs(1, -1);

  upd(1, n, 1, 1, 1, n);

  for (int i = 1; i <= n; ++i) {
    sort(a[i].begin(), a[i].end(), comp);
    long long ans = cnt(n);
    for (int x : a[i]) {
      for (int to : g[x]) {
        if (tin[to] < tin[x])
          continue;
        ans -= cnt(get(tin[to], tout[to], 1, 1, n));
      }
      upd(tin[x], tout[x], 0, 1, 1, n);
    }
    ans -= cnt(get(1, n, 1, 1, n));
    cout << ans << "\\n";
    upd(1, n, 1, 1, 1, n);
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

vector<string> g;
vector<char> used;

void dfs(long long v) {
  used[v] = true;
  for (int i = 0; i < g[v].size(); ++i) {
    if (!used[g[v][i]])
      dfs(g[v][i]);
  }
}

int main() {
  long long n;
  cin >> n;
  g.resize(n);
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long fr, to;
  for (int i = 0; i < n - 1; ++i) {
    cin >> fr >> to;
    fr--, to--;
    g[fr].push_back(to);
    g[to].push_back(fr);
  }
  used.assign(n, false);
  dfs(0);
  cout << "YES";
}