Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Data = pair<int, int>;
#define mod 1000000007
long mmax(long a, long b, long c) {
  if (a < b) {
    return max(b, c);
  } else {
    return max(a, c);
  }
}

long mmin(long a, long b, long c) {
  if (a < b) {
    return min(a, c);
  } else {
    return min(b, c);
  }
}

long long modinv(long long a) {
  long long b = mod, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

long long moddevide(long long a, long long b) { return (a * modinv(b)) % mod; }

long long modncr(long long n, long long r) {
  long long i, plus = 1;
  for (i = 0; i < r; i++) {
    plus = (plus * (n - i)) % mod;
    plus = moddevide(plus, i + 1);
  }
  return plus;
}

long long euclidean_gcd(long long a, long long b) {
  if (a < b)
    return euclidean_gcd(b, a);
  long long r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

typedef struct unionfind {
  vector<int> par;
  unionfind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return root(par[x]);
  }
  void unite(int x, int y) {
    int px = root(x);
    int py = root(y);
    if (px != py) {
      par[px] = py;
    }
  }
  bool parcheck(int x, int y) {
    int px = root(x);
    int py = root(y);
    return px == py;
  }
};

int main() {
  int i, N, K, L, a, b;
  cin >> N >> K >> L;
  unionfind road(N), train(N);

  for (i = 0; i < K; i++) {
    cin >> a >> b;
    road.unite(a - 1, b - 1);
  }
  for (i = 0; i < L; i++) {
    cin >> a >> b;
    train.unite(a - 1, b - 1);
  }
  map<Data, int> m;
  vector<Data> ans(N);
  for (i = 0; i < N; i++) {
    ans[i].first = road.root(i);
    ans[i].second = train.root(i);
    m[ans[i]]++;
  }
  for (i = 0; i < N; i++) {
    cout << m[ans[i]];
    cout << " ";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Data = pair<int, int>;
#define mod 1000000007
long mmax(long a, long b, long c) {
  if (a < b) {
    return max(b, c);
  } else {
    return max(a, c);
  }
}

long mmin(long a, long b, long c) {
  if (a < b) {
    return min(a, c);
  } else {
    return min(b, c);
  }
}

long long modinv(long long a) {
  long long b = mod, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

long long moddevide(long long a, long long b) { return (a * modinv(b)) % mod; }

long long modncr(long long n, long long r) {
  long long i, plus = 1;
  for (i = 0; i < r; i++) {
    plus = (plus * (n - i)) % mod;
    plus = moddevide(plus, i + 1);
  }
  return plus;
}

long long euclidean_gcd(long long a, long long b) {
  if (a < b)
    return euclidean_gcd(b, a);
  long long r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

typedef struct unionfind {
  vector<int> par;
  unionfind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int px = root(x);
    int py = root(y);
    if (px != py) {
      par[px] = py;
    }
  }
  bool parcheck(int x, int y) {
    int px = root(x);
    int py = root(y);
    return px == py;
  }
};

int main() {
  int i, N, K, L, a, b;
  cin >> N >> K >> L;
  unionfind road(N), train(N);

  for (i = 0; i < K; i++) {
    cin >> a >> b;
    road.unite(a - 1, b - 1);
  }
  for (i = 0; i < L; i++) {
    cin >> a >> b;
    train.unite(a - 1, b - 1);
  }
  map<Data, int> m;
  vector<Data> ans(N);
  for (i = 0; i < N; i++) {
    ans[i].first = road.root(i);
    ans[i].second = train.root(i);
    m[ans[i]]++;
  }
  for (i = 0; i < N; i++) {
    cout << m[ans[i]];
    cout << " ";
  }
  return 0;
}