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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

const int MAX_V = 1000;

int par_uft[MAX_V];
int rank_uft[MAX_V];

void init_uft(int n) {
  int i;
  for (i = 0; i < n; i++) {
    par_uft[i] = i;
    rank_uft[i] = 0;
  }
}

int find_uft(int x) {
  if (par_uft[x] == x) {
    return x;
  } else {
    return par_uft[x] = find_uft(par_uft[x]);
  }
}

void unite_uft(int x, int y) {
  x = find_uft(x);
  y = find_uft(y);
  if (x == y)
    return;

  if (rank_uft[x] < rank_uft[y]) {
    par_uft[x] = y;
  } else {
    par_uft[y] = x;
    if (rank_uft[x] == rank_uft[y]) {
      rank_uft[x]++;
    }
  }
}

bool same_uft(int x, int y) { return find_uft(x) == find_uft(y); }

int main() {
  int n, k;
  cin >> n >> k;
  init_uft(n + 1);

  int m, t, s;
  for (int i = 0; i < k; i++) {
    cin >> m >> s;
    for (int j = 0; j < m - 1; j++) {
      cin >> t;
      unite_uft(s, t);
    }
  }
  int cnt = 0;
  cin >> m;
  vi v(n + 1, 0);
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    if (same_uft(s, t)) {
      if (!v[s]) {
        v[s] = 1;
        cnt++;
      }
      if (!v[t]) {
        v[t] = 1;
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

const int MAX_V = 1000;

int par_uft[MAX_V];
int rank_uft[MAX_V];

void init_uft(int n) {
  int i;
  for (i = 0; i < n; i++) {
    par_uft[i] = i;
    rank_uft[i] = 0;
  }
}

int find_uft(int x) {
  if (par_uft[x] == x) {
    return x;
  } else {
    return par_uft[x] = find_uft(par_uft[x]);
  }
}

void unite_uft(int x, int y) {
  x = find_uft(x);
  y = find_uft(y);
  if (x == y)
    return;

  if (rank_uft[x] < rank_uft[y]) {
    par_uft[x] = y;
  } else {
    par_uft[y] = x;
    if (rank_uft[x] == rank_uft[y]) {
      rank_uft[x]++;
    }
  }
}

bool same_uft(int x, int y) { return find_uft(x) == find_uft(y); }

int main() {
  int n, k;
  cin >> n >> k;
  init_uft(n + 1);

  int m, t, s;
  for (int i = 0; i < k; i++) {
    cin >> m >> s;
    for (int j = 0; j < m - 1; j++) {
      cin >> t;
      unite_uft(s, t);
    }
  }
  int cnt = 0;
  cin >> m;
  vi v(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> s >> t;
    if (same_uft(s, t)) {
      if (!v[s]) {
        v[s] = 1;
        cnt++;
      }
      if (!v[t]) {
        v[t] = 1;
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}