Problem p03515 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
using vi = vector<int>;
using vii = vector<vi>;
const int mod = 1e9 + 7;

struct edge {
  int u, v, cost;

  bool operator<(const edge &e) const { return (cost < e.cost); }
};

const int INF = 1 << 30;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

int main() {

  int N;
  cin >> N;
  if (N > 2000)
    throw(0);
  vector<edge> edges;
  for (int i = 1; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    edges.emplace_back((edge){a, b, c});
  }
  sort(begin(edges), end(edges));
  reverse(begin(edges), end(edges));

  UnionFind uf(N);
  vector<int64> cost(N);
  vector<vector<int>> dsu(N);
  for (int i = 0; i < N; i++) {
    dsu[i].emplace_back(i);
  }
  vector<int64> lazy(N);
  for (auto &e : edges) {

    e.u = uf.find(e.u);
    e.v = uf.find(e.v);
    if (dsu[e.u].size() < dsu[e.v].size()) {
      swap(e.u, e.v);
    }

    int64 sa = lazy[e.v] - lazy[e.u]; // e.u のほうが sa だけ多い
    int64 latte = 1LL * e.cost * uf.size(e.v);
    int64 malta = 1LL * e.cost * uf.size(e.u);

    while (!dsu[e.v].empty()) {
      cost[dsu[e.v].back()] += malta + sa - latte;
      dsu[e.u].emplace_back(dsu[e.v].back());
      dsu[e.v].pop_back();
    }
    lazy[e.u] += latte;
    lazy[e.v] = 0;
    uf.unite(e.u, e.v);
  }
  for (int i = 0; i < N; i++) {
    cout << cost[i] + lazy[uf.find(i)] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
using vi = vector<int>;
using vii = vector<vi>;
const int mod = 1e9 + 7;

struct edge {
  int u, v, cost;

  bool operator<(const edge &e) const { return (cost < e.cost); }
};

const int INF = 1 << 30;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

int main() {

  int N;
  cin >> N;
  vector<edge> edges;
  for (int i = 1; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    edges.emplace_back((edge){a, b, c});
  }
  sort(begin(edges), end(edges));
  reverse(begin(edges), end(edges));

  UnionFind uf(N);
  vector<int64> cost(N);
  vector<vector<int>> dsu(N);
  for (int i = 0; i < N; i++) {
    dsu[i].emplace_back(i);
  }
  vector<int64> lazy(N);
  for (auto &e : edges) {

    e.u = uf.find(e.u);
    e.v = uf.find(e.v);
    if (dsu[e.u].size() < dsu[e.v].size()) {
      swap(e.u, e.v);
    }

    int64 sa = lazy[e.v] - lazy[e.u]; // e.u のほうが sa だけ多い
    int64 latte = 1LL * e.cost * uf.size(e.v);
    int64 malta = 1LL * e.cost * uf.size(e.u);

    while (!dsu[e.v].empty()) {
      cost[dsu[e.v].back()] += malta + sa - latte;
      dsu[e.u].emplace_back(dsu[e.v].back());
      dsu[e.v].pop_back();
    }
    lazy[e.u] += latte;
    lazy[e.v] = 0;
    uf.unite(e.u, e.v);
  }
  for (int i = 0; i < N; i++) {
    cout << cost[i] + lazy[uf.find(i)] << endl;
  }
}