Problem p01768 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

const int MAX = 1010;

class Union_Find {
public:
  int par[MAX], rank[MAX], size[MAX], gnum;
  int info[MAX];

  Union_Find(int N) {
    gnum = N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int find(int x) {
    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;
      info[y] = min(info[y], info[x]);
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
      info[x] = min(info[y], info[x]);
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }
    gnum--;
  }

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

  int getSize(int x) { return size[find(x)]; }

  int groups() { return gnum; }
};

int main() {

  int N;
  cin >> N;

  Union_Find uf(N);
  map<string, int> mp;
  int A[N];
  int v = 0;
  rep(i, N) {
    string s;
    cin >> s;
    if (mp.find(s) == mp.end()) {
      mp[s] = v++;
    }
    cin >> A[i];
    uf.info[mp[s]] = A[i];
  }

  assert(v == N);

  int M;
  cin >> M;
  rep(i, M) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }

  ll sum = 0;

  rep(i, N) { sum += uf.info[uf.find(i)]; }

  cout << sum << endl;

  return 0;
}

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

const int MAX = 5010;

class Union_Find {
public:
  int par[MAX], rank[MAX], size[MAX], gnum;
  int info[MAX];

  Union_Find(int N) {
    gnum = N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int find(int x) {
    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;
      info[y] = min(info[y], info[x]);
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
      info[x] = min(info[y], info[x]);
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }
    gnum--;
  }

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

  int getSize(int x) { return size[find(x)]; }

  int groups() { return gnum; }
};

int main() {

  int N;
  cin >> N;

  Union_Find uf(N);
  map<string, int> mp;
  int A[N];
  int v = 0;
  rep(i, N) {
    string s;
    cin >> s;
    if (mp.find(s) == mp.end()) {
      mp[s] = v++;
    }
    cin >> A[i];
    uf.info[mp[s]] = A[i];
  }

  assert(v == N);

  int M;
  cin >> M;
  rep(i, M) {
    string s, t;
    cin >> s >> t;
    uf.unite(mp[s], mp[t]);
  }

  ll sum = 0;

  rep(i, N) { sum += uf.info[uf.find(i)]; }

  cout << sum << endl;

  return 0;
}