Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc049/tasks/arc065_b

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define REP1(i, n) for (ll i = 1, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define radrad(a) 180.0 * a / M_PI
#define pow(x) x *x
static const int MAX = 1000010;
static const ll INFTY = 1e12;
static const ll NUMMAX = 1000000007;
static const ll MOD = 1000000007;
#define LOOP 10000000
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

// 入力
int H, W;
vector<string> field;
int result = 100;
int dfs_res = 0;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
public:
  vector<ll> Parent;
  vector<ll> Size;

  // Constructor
  UnionFind(ll InitSize) : Parent(InitSize), Size(InitSize, 1LL) {
    for (ll i = 0; i < InitSize; i++)
      Parent[i] = i; // myself
  }

  void Init(ll InitSize) {
    Parent.resize(InitSize);
    Size.assign(InitSize, 1LL);
    for (ll i = 0; i < InitSize; i++)
      Parent[i] = i; // myself
  }

  // Member Function
  // FindRoot
  ll FindRoot(ll x) {
    while (Parent[x] != x) {
      x = Parent[x] = Parent[Parent[x]];
    }
    return x;
  }

  // Merge
  void Merge(ll x, ll y) {
    x = FindRoot(x);
    y = FindRoot(y);
    if (x == y)
      return;
    if (Size[x] < Size[y])
      swap(x, y);
    Size[x] += Size[y];
    Parent[y] = x;
    return;
  }

  bool IsSame(ll x, ll y) { return FindRoot(x) == FindRoot(y); }

  ll CalcSize(ll x) { return Size[FindRoot(x)]; }
};

int main() {

  ll N, K, L;
  cin >> N >> K >> L;
  vector<int> p(K), q(K);
  REP(i, K) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    p[i] = a;
    q[i] = b;
  }
  vector<int> r(L), s(L);
  REP(i, L) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    r[i] = a;
    s[i] = b;
  }

  UnionFind UF_douro(N);
  UnionFind UF_tetudo(N);
  vector<int> ans(N);
  int count = 0;

  REP(i, K) { UF_douro.Merge(p[i], q[i]); }
  REP(i, L) { UF_tetudo.Merge(r[i], s[i]); }
  REP(i, N) {
    REP(j, N) {
      if (UF_douro.IsSame(i, j) && UF_tetudo.IsSame(i, j)) {
        count++;
      }
    }
    ans[i] = count;
    count = 0;
  }
  map<pair<int, int>, int> mp;
  for (int i = 0; i < N; i++) {
    int r1 = UF_douro.FindRoot(i), r2 = UF_tetudo.FindRoot(i);
    mp[make_pair(r1, r2)]++;
  }

  for (int i = 0; i < N; i++) {
    int r1 = UF_douro.FindRoot(i), r2 = UF_tetudo.FindRoot(i);
    cout << mp[make_pair(r1, r2)] << " ";
  }
  cout << endl;

  return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/


Fixed submission:
// https://atcoder.jp/contests/abc049/tasks/arc065_b

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define REP1(i, n) for (ll i = 1, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define radrad(a) 180.0 * a / M_PI
#define pow(x) x *x
static const int MAX = 1000010;
static const ll INFTY = 1e12;
static const ll NUMMAX = 1000000007;
static const ll MOD = 1000000007;
#define LOOP 10000000
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

// 入力
int H, W;
vector<string> field;
int result = 100;
int dfs_res = 0;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
public:
  vector<ll> Parent;
  vector<ll> Size;

  // Constructor
  UnionFind(ll InitSize) : Parent(InitSize), Size(InitSize, 1LL) {
    for (ll i = 0; i < InitSize; i++)
      Parent[i] = i; // myself
  }

  void Init(ll InitSize) {
    Parent.resize(InitSize);
    Size.assign(InitSize, 1LL);
    for (ll i = 0; i < InitSize; i++)
      Parent[i] = i; // myself
  }

  // Member Function
  // FindRoot
  ll FindRoot(ll x) {
    while (Parent[x] != x) {
      x = Parent[x] = Parent[Parent[x]];
    }
    return x;
  }

  // Merge
  void Merge(ll x, ll y) {
    x = FindRoot(x);
    y = FindRoot(y);
    if (x == y)
      return;
    if (Size[x] < Size[y])
      swap(x, y);
    Size[x] += Size[y];
    Parent[y] = x;
    return;
  }

  bool IsSame(ll x, ll y) { return FindRoot(x) == FindRoot(y); }

  ll CalcSize(ll x) { return Size[FindRoot(x)]; }
};

int main() {

  ll N, K, L;
  cin >> N >> K >> L;
  vector<int> p(K), q(K);
  REP(i, K) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    p[i] = a;
    q[i] = b;
  }
  vector<int> r(L), s(L);
  REP(i, L) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    r[i] = a;
    s[i] = b;
  }

  UnionFind UF_douro(N);
  UnionFind UF_tetudo(N);
  vector<int> ans(N);
  int count = 0;

  REP(i, K) { UF_douro.Merge(p[i], q[i]); }
  REP(i, L) { UF_tetudo.Merge(r[i], s[i]); }

  map<pair<int, int>, int> mp;
  for (int i = 0; i < N; i++) {
    int r1 = UF_douro.FindRoot(i), r2 = UF_tetudo.FindRoot(i);
    mp[make_pair(r1, r2)]++;
  }

  for (int i = 0; i < N; i++) {
    int r1 = UF_douro.FindRoot(i), r2 = UF_tetudo.FindRoot(i);
    cout << mp[make_pair(r1, r2)] << " ";
  }
  cout << endl;

  return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/