Problem p01504 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

#define rank Asdfiasofsad
class UF {
public:
  int V;
  vector<int> par, rank, base;
  UF() {}
  UF(int V) : V(V), par(V), rank(V, 0), base(V, 0) {
    for (int i = 0; i < V; i++)
      par[i] = i;
  }

  int find(int x) {
    assert(x < V);
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y, base[y] |= base[x];
    else {
      par[y] = x;
      base[x] |= base[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int n, k, t;
typedef pair<int, int> P;
typedef pair<int, P> PP;
vector<int> T;
vector<PP> edge;

int solve() {
  sort(edge.begin(), edge.end());
  int sum = 0;
  for (int i = 0; i < n - 1; i++)
    sum += -edge[i].first;
  UF uf(n);
  for (int i = 0; i < t; i++)
    uf.base[T[i]] = 1;

  int numBase = t, numNode = n;
  for (int i = 0; i < n - 1; i++) {
    int a = edge[i].second.first;
    int b = edge[i].second.second;
    int c = -edge[i].first;
    a = uf.find(a);
    b = uf.find(b);
    if (uf.same(a, b))
      continue;
    if (numBase == k + 1 && uf.base[a] && uf.base[b])
      continue;
    if (numNode == k + 1)
      continue;
    if (uf.base[a] && uf.base[b])
      numBase--;
    numNode--;
    sum -= c;
    uf.unite(a, b);
  }
  return sum;
}

signed main() {
  int numCase = 0;
  while (1) {
    cin >> n >> t >> k;
    if (n == 0 && k == 0 && t == 0)
      break;
    cout << "Case " << ++numCase << ": ";

    edge.resize(n - 1);
    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      edge[i] = PP(-c, P(a, b));
    }

    T.resize(t);
    for (int i = 0; i < t; i++)
      cin >> T[i];

    cout << solve() << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

#define rank Asdfiasofsad
class UF {
public:
  int V;
  vector<int> par, rank, base;
  UF() {}
  UF(int V) : V(V), par(V), rank(V, 0), base(V, 0) {
    for (int i = 0; i < V; i++)
      par[i] = i;
  }

  int find(int x) {
    assert(x < V);
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y, base[y] |= base[x];
    else {
      par[y] = x;
      base[x] |= base[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int n, k, t;
typedef pair<int, int> P;
typedef pair<int, P> PP;
vector<int> T;
vector<PP> edge;

int solve() {
  sort(edge.begin(), edge.end());
  int sum = 0;
  for (int i = 0; i < n - 1; i++)
    sum += -edge[i].first;
  UF uf(n);
  for (int i = 0; i < t; i++)
    uf.base[T[i]] = 1;

  int numBase = t, numNode = n;
  for (int i = 0; i < n - 1; i++) {
    int a = edge[i].second.first;
    int b = edge[i].second.second;
    int c = -edge[i].first;
    a = uf.find(a);
    b = uf.find(b);
    if (uf.same(a, b))
      continue;
    if (numBase == k + 1 && uf.base[a] && uf.base[b])
      continue;
    if (numNode == k + 1)
      continue;
    if (uf.base[a] && uf.base[b])
      numBase--;
    numNode--;
    sum -= c;
    uf.unite(a, b);
  }
  return sum;
}

signed main() {
  int numCase = 0;
  while (1) {
    cin >> n >> t >> k;
    if (n == 0 && k == 0 && t == 0)
      break;
    cout << "Case " << ++numCase << ": ";

    edge.resize(n - 1);
    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      edge[i] = PP(-c, P(a, b));
    }

    T.resize(t);
    for (int i = 0; i < t; i++)
      cin >> T[i], T[i]--;

    cout << solve() << endl;
  }

  return 0;
}