prithivMLmods/Coder-Stat huggingface dataset:
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
/*input
7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7
*/

#include <iostream>
#include <map>
#include <numeric>
#include <vector>

using namespace std;

class UnionFind {
private:
  vector<int> group;
  vector<int> size;

public:
  UnionFind(int N);
  int get(int n);
  void merge(int a, int b);
};

UnionFind::UnionFind(int N) {
  group.resize(N);
  iota(group.begin(), group.end(), 0);
  size.resize(N);
  fill(size.begin(), size.end(), 1);
}

int UnionFind::get(int n) {
  if (n == group[n])
    return n;
  int g = group[n];
  return get(g);
}

void UnionFind::merge(int a, int b) {
  if (group[a] == group[b])
    return;
  a = get(a);
  b = get(b);
  if (size[a] < size[b])
    swap(a, b);
  group[b] = a;
  size[a] += size[b];
}

int main() {

  int N, K, L;
  cin >> N >> K >> L;

  UnionFind road_group(N);
  for (int k = 0; k < K; k++) {
    int p, q;
    cin >> p >> q;
    road_group.merge(p - 1, q - 1);
  }

  UnionFind rail_group(N);
  for (int l = 0; l < L; l++) {
    int r, s;
    cin >> r >> s;
    rail_group.merge(r - 1, s - 1);
  }

  // for(int n=0;n<N;n++){
  // 	cout << "road:" << road_group.get(n) <<
  // 			" rail:" << rail_group.get(n) << endl;
  // }

  map<pair<int, int>, int> pair_array;
  for (int n = 0; n < N; n++) {
    pair_array[make_pair(road_group.get(n), rail_group.get(n))]++;
  }

  // show result
  for (int n = 0; n < N; n++) {
    int c = pair_array[make_pair(road_group.get(n), rail_group.get(n))];
    cout << c;
    if (n != N - 1)
      cout << " ";
  }
  cout << endl;

  return 0;
}


Fixed submission:
/*input
7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7
*/

#include <iostream>
#include <map>
#include <numeric>
#include <vector>

using namespace std;

class UnionFind {
private:
  vector<int> group;
  vector<int> size;

public:
  UnionFind(int N);
  int get(int n);
  void merge(int a, int b);
};

UnionFind::UnionFind(int N) {
  group.resize(N);
  iota(group.begin(), group.end(), 0);
  size.resize(N);
  fill(size.begin(), size.end(), 1);
}

int UnionFind::get(int n) {
  if (n == group[n])
    return n;
  int g = group[n];
  group[n] = get(g);
  return group[n];
}

void UnionFind::merge(int a, int b) {
  if (group[a] == group[b])
    return;
  a = get(a);
  b = get(b);
  if (size[a] < size[b])
    swap(a, b);
  group[b] = a;
  size[a] += size[b];
}

int main() {

  int N, K, L;
  cin >> N >> K >> L;

  UnionFind road_group(N);
  for (int k = 0; k < K; k++) {
    int p, q;
    cin >> p >> q;
    road_group.merge(p - 1, q - 1);
  }

  UnionFind rail_group(N);
  for (int l = 0; l < L; l++) {
    int r, s;
    cin >> r >> s;
    rail_group.merge(r - 1, s - 1);
  }

  // for(int n=0;n<N;n++){
  // 	cout << "road:" << road_group.get(n) <<
  // 			" rail:" << rail_group.get(n) << endl;
  // }

  map<pair<int, int>, int> pair_array;
  for (int n = 0; n < N; n++) {
    pair_array[make_pair(road_group.get(n), rail_group.get(n))]++;
  }

  // show result
  for (int n = 0; n < N; n++) {
    int c = pair_array[make_pair(road_group.get(n), rail_group.get(n))];
    cout << c;
    if (n != N - 1)
      cout << " ";
  }
  cout << endl;

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int load[100000], train[100000];
int find(int x, int *box) {
  if (box[x] == x)
    return x;
  return box[x] = find(box[x], box);
}
void unite(int x, int y, int *box) {
  int rx = find(x, box);
  int ry = find(y, box);
  if (rx == ry)
    return;
  box[rx] = ry;
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i < n; i++) {
    load[i] = i;
    train[i] = i;
  }
  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(p - 1, q - 1, load);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unite(r - 1, s - 1, train);
  }

  vector<pair<int, int>> v(n);
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    v[i] = make_pair(find(i, load), find(i, train));
    m[v[i]]++;
  }
  for (pair<int, int> w : v)
    cout << m[w] << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int load[200000], train[200000];
int find(int x, int *box) {
  if (box[x] == x)
    return x;
  return box[x] = find(box[x], box);
}
void unite(int x, int y, int *box) {
  int rx = find(x, box);
  int ry = find(y, box);
  if (rx == ry)
    return;
  box[rx] = ry;
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i < n; i++) {
    load[i] = i;
    train[i] = i;
  }
  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(p - 1, q - 1, load);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unite(r - 1, s - 1, train);
  }

  vector<pair<int, int>> v(n);
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    v[i] = make_pair(find(i, load), find(i, train));
    m[v[i]]++;
  }
  for (pair<int, int> w : v)
    cout << m[w] << " ";
  cout << endl;
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll p[100010][2];
ll r[100010][2];
ll root(ll x, ll yojo) {
  return p[x][yojo] == x ? x : p[x][yojo] = root(p[x][yojo], yojo);
}
void unite(ll x, ll y, ll yojo) {
  x = root(x, yojo);
  y = root(y, yojo);
  if (x == y)
    return;
  if (r[x][yojo] < r[y][yojo])
    p[x][yojo] = y;
  else {
    p[y][yojo] = x;
    if (r[x][yojo] == r[y][yojo])
      r[x][yojo]++;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    p[i][0] = p[i][1] = i;
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    unite(x, y, 0);
  }
  for (int i = 0; i < l; i++) {
    cin >> x >> y;
    unite(x, y, 1);
  }
  map<P, ll> mp;
  ll a, b;
  for (int i = 1; i <= n; i++) {
    a = root(i, 0);
    b = root(i, 1);
    mp[make_pair(a, b)]++;
  }
  for (int i = 1; i <= n; i++) {
    a = root(i, 0);
    b = root(i, 1);
    cout << mp[make_pair(a, b)];
    if (i == n)
      cout << endl;
    else
      cout << ' ';
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll p[200020][2];
ll r[200020][2];
ll root(ll x, ll yojo) {
  return p[x][yojo] == x ? x : p[x][yojo] = root(p[x][yojo], yojo);
}
void unite(ll x, ll y, ll yojo) {
  x = root(x, yojo);
  y = root(y, yojo);
  if (x == y)
    return;
  if (r[x][yojo] < r[y][yojo])
    p[x][yojo] = y;
  else {
    p[y][yojo] = x;
    if (r[x][yojo] == r[y][yojo])
      r[x][yojo]++;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    p[i][0] = p[i][1] = i;
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    unite(x, y, 0);
  }
  for (int i = 0; i < l; i++) {
    cin >> x >> y;
    unite(x, y, 1);
  }
  map<P, ll> mp;
  ll a, b;
  for (int i = 1; i <= n; i++) {
    a = root(i, 0);
    b = root(i, 1);
    mp[make_pair(a, b)]++;
  }
  for (int i = 1; i <= n; i++) {
    a = root(i, 0);
    b = root(i, 1);
    cout << mp[make_pair(a, b)];
    if (i == n)
      cout << endl;
    else
      cout << ' ';
  }
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> p;
  UF(int n) : p(n) { iota(p.begin(), p.end(), 0); }
  int r(int x) {
    if (p[x] == 0)
      return x;
    return p[x] = r(p[x]);
  }
  void m(int x, int y) {
    x = r(x);
    y = r(y);
    p[x] = y;
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UF a(n), b(n);
  while (k--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    a.m(f, t);
  }
  while (l--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    b.m(f, t);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[{a.r(i), b.r(i)}]++;
  }
  for (int i = 0; i < n; i++) {
    cout << m[{a.r(i), b.r(i)}] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> p;
  UF(int n) : p(n) { iota(p.begin(), p.end(), 0); }
  int r(int x) {
    if (p[x] == x)
      return x;
    return p[x] = r(p[x]);
  }
  void m(int x, int y) {
    x = r(x);
    y = r(y);
    p[x] = y;
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UF a(n), b(n);
  while (k--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    a.m(f, t);
  }
  while (l--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    b.m(f, t);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[{a.r(i), b.r(i)}]++;
  }
  for (int i = 0; i < n; i++) {
    cout << m[{a.r(i), b.r(i)}] << endl;
  }
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef pair<int, int> P;
typedef long long ll;

class UnionFind {
public:
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n, 0) {
    for (int i = 1; i <= n; i++)
      par[i] = i;
  }

  int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

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

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

int main(void) {
  int n, k, l;
  int p, q;
  map<P, int> num;
  cin >> n >> k >> l;
  UnionFind uf(n), uf1(n);
  for (int i = 0; i < k; i++) {
    cin >> p >> q;
    uf.unite_tree(p, q);
  }
  for (int i = 0; i < l; i++) {
    cin >> p >> q;
    uf1.unite_tree(p, q);
  }
  for (int i = 1; i <= n; i++) {
    p = uf.find(i);
    q = uf1.find(i);
    num[P(p, q)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      p = uf.find(i);
      q = uf1.find(i);
      cout << num[P(p, q)] << endl;
    } else {
      p = uf.find(i);
      q = uf1.find(i);
      cout << num[P(p, q)] << " ";
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef pair<int, int> P;
typedef long long ll;

class UnionFind {
public:
  vector<int> par, rank;
  UnionFind(int n) : par(n + 1), rank(n, 0) {
    for (int i = 1; i <= n; i++)
      par[i] = i;
  }

  int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

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

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

int main(void) {
  int n, k, l;
  int p, q;
  map<P, int> num;
  cin >> n >> k >> l;
  UnionFind uf(n), uf1(n);
  for (int i = 0; i < k; i++) {
    cin >> p >> q;
    uf.unite_tree(p, q);
  }
  for (int i = 0; i < l; i++) {
    cin >> p >> q;
    uf1.unite_tree(p, q);
  }
  for (int i = 1; i <= n; i++) {
    p = uf.find(i);
    q = uf1.find(i);
    num[P(p, q)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      p = uf.find(i);
      q = uf1.find(i);
      cout << num[P(p, q)] << endl;
    } else {
      p = uf.find(i);
      q = uf1.find(i);
      cout << num[P(p, q)] << " ";
    }
  }
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 1e5 + 10;

vector<int> g[MAX], h[MAX];
vector<int> f1(MAX, -1), f2(MAX, -1);
vector<int> C[MAX];
vector<bool> vis(MAX, 0);

int id1 = -1;
int id2 = -1;

void DFS1(int v) {

  f1[v] = id1;
  vis[v] = 1;

  for (auto u : g[v]) {
    if (vis[u])
      continue;
    DFS1(u);
  }
}

void DFS2(int v) {

  C[f1[v]].pb(id2);
  f2[v] = id2;
  vis[v] = 1;

  for (auto u : h[v]) {
    if (vis[u])
      continue;
    DFS2(u);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N, K, L;

  cin >> N >> K >> L;

  while (K--) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }

  while (L--) {
    int x, y;
    cin >> x >> y;
    h[x].pb(y);
    h[y].pb(x);
  }

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      id1++;
      DFS1(i);
    }
  }

  vis = vector<bool>(MAX, 0);

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      id2++;
      DFS2(i);
    }
  }

  for (int i = 1; i <= N; i++) {
    if (C[i].size())
      sort(C[i].begin(), C[i].end());
  }

  for (int i = 1; i <= N; i++) {
    int at = f1[i];
    int val = f2[i];
    cout << upper_bound(C[at].begin(), C[at].end(), val) -
                lower_bound(C[at].begin(), C[at].end(), val)
         << " \n"[i == N];
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 2e5 + 10;

vector<int> g[MAX], h[MAX];
vector<int> f1(MAX, -1), f2(MAX, -1);
vector<int> C[MAX];
vector<bool> vis(MAX, 0);

int id1 = -1;
int id2 = -1;

void DFS1(int v) {

  f1[v] = id1;
  vis[v] = 1;

  for (auto u : g[v]) {
    if (vis[u])
      continue;
    DFS1(u);
  }
}

void DFS2(int v) {

  C[f1[v]].pb(id2);
  f2[v] = id2;
  vis[v] = 1;

  for (auto u : h[v]) {
    if (vis[u])
      continue;
    DFS2(u);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N, K, L;

  cin >> N >> K >> L;

  while (K--) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }

  while (L--) {
    int x, y;
    cin >> x >> y;
    h[x].pb(y);
    h[y].pb(x);
  }

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      id1++;
      DFS1(i);
    }
  }

  vis = vector<bool>(MAX, 0);

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      id2++;
      DFS2(i);
    }
  }

  for (int i = 1; i <= N; i++) {
    if (C[i].size())
      sort(C[i].begin(), C[i].end());
  }

  for (int i = 1; i <= N; i++) {
    int at = f1[i];
    int val = f2[i];
    cout << upper_bound(C[at].begin(), C[at].end(), val) -
                lower_bound(C[at].begin(), C[at].end(), val)
         << " \n"[i == N];
  }

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

vector<pll> par(1e5 + 10);
void init(ll n) {
  for (ll i = 0; i < n; i++) {
    par[i] = pll(i, i);
  }
}

ll root(ll x, ll flag) {
  if (flag) {
    if (par[x].first == x) {
      return x;
    } else {
      return par[x].first = root(par[x].first, flag);
    }
  } else {
    if (par[x].second == x) {
      return x;
    } else {
      return par[x].second = root(par[x].second, flag);
    }
  }
}

bool same(ll x, ll y, ll flag) { return root(x, flag) == root(y, flag); }

void unite(ll x, ll y, ll flag) {
  if (same(x, y, flag))
    return;
  if (flag) {
    par[root(x, flag)].first = root(y, flag);
  } else {
    par[root(x, flag)].second = root(y, flag);
  }
}

int main() {
  ll n, k, l;
  cin >> n >> k >> l;
  init(n);
  for (ll i = 0; i < k; i++) {
    ll p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q, 1);
  }
  for (ll i = 0; i < l; i++) {
    ll p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q, 0);
  }
  map<pll, ll> num;
  for (ll i = 0; i < n; i++) {
    num[pll(root(i, 1), root(i, 0))]++;
  }
  for (ll i = 0; i < n; i++) {
    cout << num[pll(root(i, 1), root(i, 0))];
    if (i != n - 1)
      cout << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

vector<pll> par(2e5 + 10);
void init(ll n) {
  for (ll i = 0; i < n; i++) {
    par[i] = pll(i, i);
  }
}

ll root(ll x, ll flag) {
  if (flag) {
    if (par[x].first == x) {
      return x;
    } else {
      return par[x].first = root(par[x].first, flag);
    }
  } else {
    if (par[x].second == x) {
      return x;
    } else {
      return par[x].second = root(par[x].second, flag);
    }
  }
}

bool same(ll x, ll y, ll flag) { return root(x, flag) == root(y, flag); }

void unite(ll x, ll y, ll flag) {
  if (same(x, y, flag))
    return;
  if (flag) {
    par[root(x, flag)].first = root(y, flag);
  } else {
    par[root(x, flag)].second = root(y, flag);
  }
}

int main() {
  ll n, k, l;
  cin >> n >> k >> l;
  init(n);
  for (ll i = 0; i < k; i++) {
    ll p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q, 1);
  }
  for (ll i = 0; i < l; i++) {
    ll p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q, 0);
  }
  map<pll, ll> num;
  for (ll i = 0; i < n; i++) {
    num[pll(root(i, 1), root(i, 0))]++;
  }
  for (ll i = 0; i < n; i++) {
    cout << num[pll(root(i, 1), root(i, 0))];
    if (i != n - 1)
      cout << " ";
  }
  cout << endl;
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 500010;

ll dsu[N], size[N];

ll root(ll i) {
  while (i != dsu[i]) {
    i = dsu[i];
  }
  return i;
}

vector<ll> adj[N];
vector<ll> vis;
vector<ll> vis1;
vector<ll> ans;
map<ll, ll> ch;
void dfs(ll v) {
  vis[v] = 1;
  ll myroot = root(v);
  ch[myroot] += 1;
  for (auto u : adj[v]) {
    if (vis[u] == 0) {
      dfs(u);
    }
  }
}
void dfs1(ll v) {
  vis1[v] = 1;
  ll myroot = root(v);
  ans[v] = ch[myroot];
  for (auto u : adj[v]) {
    if (vis1[u] == 0) {
      dfs1(u);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k, l;
  cin >> n >> k >> l;
  for (ll i = 0; i <= n; ++i) {
    dsu[i] = i;
    size[i] = 1;
  }
  for (ll i = 0; i < k; ++i) {
    ll x, y;
    cin >> x >> y;
    ll rx = root(x);
    ll ry = root(y);
    if (size[rx] > size[ry]) {
      dsu[ry] = rx;
      size[rx] += size[ry];
    } else {
      dsu[rx] = ry;
      size[ry] += size[rx];
    }
  }

  for (ll i = 0; i < l; ++i) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }

  vis.resize(n + 1, 0);
  vis1.resize(n + 1, 0);
  ans.resize(n + 1, 0);

  for (ll i = 1; i <= n; ++i) {
    if (vis[i] == 0) {
      ch.clear();
      dfs(i);
      dfs1(i);
    }
  }

  for (ll i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 500010;

ll dsu[N], size[N];

ll root(ll i) {
  while (i != dsu[i]) {
    dsu[i] = dsu[dsu[i]];
    i = dsu[i];
  }
  return i;
}

vector<ll> adj[N];
vector<ll> vis;
vector<ll> vis1;
vector<ll> ans;
map<ll, ll> ch;
void dfs(ll v) {
  vis[v] = 1;
  ll myroot = root(v);
  ch[myroot] += 1;
  for (auto u : adj[v]) {
    if (vis[u] == 0) {
      dfs(u);
    }
  }
}
void dfs1(ll v) {
  vis1[v] = 1;
  ll myroot = root(v);
  ans[v] = ch[myroot];
  for (auto u : adj[v]) {
    if (vis1[u] == 0) {
      dfs1(u);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k, l;
  cin >> n >> k >> l;
  for (ll i = 0; i <= n; ++i) {
    dsu[i] = i;
    size[i] = 1;
  }
  for (ll i = 0; i < k; ++i) {
    ll x, y;
    cin >> x >> y;
    ll rx = root(x);
    ll ry = root(y);
    if (size[rx] > size[ry]) {
      dsu[ry] = rx;
      size[rx] += size[ry];
    } else {
      dsu[rx] = ry;
      size[ry] += size[rx];
    }
  }

  for (ll i = 0; i < l; ++i) {
    ll a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }

  vis.resize(n + 1, 0);
  vis1.resize(n + 1, 0);
  ans.resize(n + 1, 0);

  for (ll i = 1; i <= n; ++i) {
    if (vis[i] == 0) {
      ch.clear();
      dfs(i);
      dfs1(i);
    }
  }

  for (ll i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }

  return (0);
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
using Int = long long;
using namespace std;
// http://www.prefield.com/algorithm/container/union_find.html
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind road(N), rail(N);
  REP(i, K) {
    int p, q;
    cin >> p >> q;
    p--, q--;
    road.unionSet(p, q);
  }
  REP(i, L) {
    int r, s;
    cin >> r >> s;
    r--, s--;
    rail.unionSet(r, s);
  }
  map<int, set<int>> groups;
  vector<int> ans(N, -1);
  REP(i, N) { groups[road.root(i)].insert(i); }
  for (auto &p : groups) {
    auto S = p.second;
    while (S.size()) {
      int i = *S.begin();
      S.erase(i);
      vector<int> buf;
      for (int j : S)
        if (rail.findSet(i, j))
          buf.push_back(j);
      for (int j : buf)
        S.erase(j);
      ans[i] = buf.size() + 1;
      for (int j : buf)
        ans[j] = buf.size() + 1;
    }
  }
  for (int i : ans)
    cout << i << ' ';
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
using Int = long long;
using namespace std;
// http://www.prefield.com/algorithm/container/union_find.html
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind road(N), rail(N);
  REP(i, K) {
    int p, q;
    cin >> p >> q;
    p--, q--;
    road.unionSet(p, q);
  }
  REP(i, L) {
    int r, s;
    cin >> r >> s;
    r--, s--;
    rail.unionSet(r, s);
  }
  map<Int, int> cnt;
  REP(i, N) { cnt[(Int)road.root(i) * N + rail.root(i)]++; }
  REP(i, N) { cout << cnt[(Int)road.root(i) * N + rail.root(i)] << ' '; }
  cout << endl;
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define LL long long

int F(int f[], int x) { return (f[x] == x ? x : F(f, f[x])); }

void unite(int f[], int x, int y) {
  if (F(f, x) == F(f, y))
    return;
  f[F(f, x)] = F(f, y);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int n, k, l, x, y;
  cin >> n >> k >> l;
  int rd[n + 1], rw[n + 1];

  for (int i = 1; i <= n; i++)
    rd[i] = rw[i] = i;

  while (k--) {
    cin >> x >> y;
    unite(rd, x, y);
  }

  while (l--) {
    cin >> x >> y;
    unite(rw, x, y);
  }

  pair<int, int> q[n + 1];
  map<pair<int, int>, int> ans;

  for (int i = 1; i <= n; i++) {
    q[i] = {F(rd, i), F(rw, i)};
    ans[q[i]]++;
  }

  for (int i = 1; i <= n; i++)
    cout << ans[q[i]] << " ";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define LL long long

int F(int f[], int x) { return (f[x] == x ? x : f[x] = F(f, f[x])); }

void unite(int f[], int x, int y) {
  if (F(f, x) == F(f, y))
    return;
  f[F(f, x)] = F(f, y);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int n, k, l, x, y;
  cin >> n >> k >> l;
  int rd[n + 1], rw[n + 1];

  for (int i = 1; i <= n; i++)
    rd[i] = rw[i] = i;

  while (k--) {
    cin >> x >> y;
    unite(rd, x, y);
  }

  while (l--) {
    cin >> x >> y;
    unite(rw, x, y);
  }

  pair<int, int> q[n + 1];
  map<pair<int, int>, int> ans;

  for (int i = 1; i <= n; i++) {
    q[i] = {F(rd, i), F(rw, i)};
    ans[q[i]]++;
  }

  for (int i = 1; i <= n; i++)
    cout << ans[q[i]] << " ";
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
#define vecmin(A) *min_element(A.begin(), A.end())
#define vecmax(A) *max_element(A.begin(), A.end())
typedef long long ll;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, k, L;
  cin >> n >> k >> L;
  UnionFind road(n + 1);
  UnionFind train(n + 1);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    road.unite(p, q);
  }
  rep(i, L) {
    int r, s;
    cin >> r >> s;
    train.unite(r, s);
  }
  rep1(i, n) {
    int ans = 0;
    rep1(j, n) {
      if (road.same(i, j) && train.same(i, j))
        ans++;
    } // j
    cout << ans << endl;
  } // i
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
#define vecmin(A) *min_element(A.begin(), A.end())
#define vecmax(A) *max_element(A.begin(), A.end())
typedef long long ll;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, k, L;
  cin >> n >> k >> L;
  UnionFind road(n + 1);
  UnionFind train(n + 1);
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    road.unite(p, q);
  }
  rep(i, L) {
    int r, s;
    cin >> r >> s;
    train.unite(r, s);
  }
  map<pair<int, int>, int> mp;
  rep1(i, n) mp[make_pair(road.root(i), train.root(i))]++;
  rep1(i, n) cout << mp[make_pair(road.root(i), train.root(i))] << endl;
}
Problem p03855 (C++)

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

const int maxn = 1e5 + 20;

int n, k, l, a, b;
int visited[2][maxn], ar[maxn];
vector<int> graph[2][maxn];
vector<vector<int>> ans[maxn];

void roadComponent(int i, int CPcnt) {
  visited[0][i] = CPcnt;
  for (int x : graph[0][i])
    if (!visited[0][x])
      roadComponent(x, CPcnt);
}
void railComponent(int i, int CPcnt) {
  visited[1][i] = CPcnt;
  if (ans[visited[0][i]].size() != 0 &&
      ans[visited[0][i]][ans[visited[0][i]].size() - 1][0] == -CPcnt)
    ans[visited[0][i]][ans[visited[0][i]].size() - 1].push_back(i);
  else {
    vector<int> need;
    need.push_back(-CPcnt);
    need.push_back(i);
    ans[visited[0][i]].push_back(need);
  }
  ar[i] = ans[visited[0][i]].size() - 1;
  for (int x : graph[1][i])
    if (!visited[1][x])
      railComponent(x, CPcnt);
}

int main() {
  cin >> n >> k >> l;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[0][a].push_back(b);
    graph[0][b].push_back(a);
  }
  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    graph[1][a].push_back(b);
    graph[1][b].push_back(a);
  }

  int CPcnt = 1;
  for (int i = 1; i <= n; i++)
    if (!visited[0][i])
      roadComponent(i, CPcnt++);
  CPcnt = 1;
  for (int i = 1; i <= n; i++)
    if (!visited[1][i])
      railComponent(i, CPcnt++);
  for (int i = 1; i <= n; i++)
    cout << ans[visited[0][i]][ar[i]].size() - 1 << " ";
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 20;

int n, k, l, a, b;
int visited[2][maxn], ar[maxn];
vector<int> graph[2][maxn];
vector<vector<int>> ans[maxn];

void roadComponent(int i, int CPcnt) {
  visited[0][i] = CPcnt;
  for (int x : graph[0][i])
    if (!visited[0][x])
      roadComponent(x, CPcnt);
}
void railComponent(int i, int CPcnt) {
  visited[1][i] = CPcnt;
  if (ans[visited[0][i]].size() != 0 &&
      ans[visited[0][i]][ans[visited[0][i]].size() - 1][0] == -CPcnt)
    ans[visited[0][i]][ans[visited[0][i]].size() - 1].push_back(i);
  else {
    vector<int> need;
    need.push_back(-CPcnt);
    need.push_back(i);
    ans[visited[0][i]].push_back(need);
  }
  ar[i] = ans[visited[0][i]].size() - 1;
  for (int x : graph[1][i])
    if (!visited[1][x])
      railComponent(x, CPcnt);
}

int main() {
  cin >> n >> k >> l;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    graph[0][a].push_back(b);
    graph[0][b].push_back(a);
  }
  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    graph[1][a].push_back(b);
    graph[1][b].push_back(a);
  }

  int CPcnt = 1;
  for (int i = 1; i <= n; i++)
    if (!visited[0][i])
      roadComponent(i, CPcnt++);
  CPcnt = 1;
  for (int i = 1; i <= n; i++)
    if (!visited[1][i])
      railComponent(i, CPcnt++);
  for (int i = 1; i <= n; i++)
    cout << ans[visited[0][i]][ar[i]].size() - 1 << " ";
  cout << endl;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[2 * N], b[2 * N];
vector<int> v[N];
map<pair<int, int>, int> mp;
queue<int> q;
int main() {
  int n, k, l, i, j;
  cin >> n >> k >> l;
  for (i = 0; i < k; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int cnt = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == 0) {
      a[i] = ++cnt;
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        for (j = 0; j < v[x].size(); j++)
          if (a[v[x][j]] == 0) {
            a[v[x][j]] = cnt;
            q.push(v[x][j]);
          }
        q.pop();
      }
    }
  for (i = 1; i <= n; i++)
    v[i].clear();
  for (i = 0; i < l; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  cnt = 0;
  for (i = 1; i <= n; i++)
    if (b[i] == 0) {
      b[i] = ++cnt;
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        for (j = 0; j < v[x].size(); j++)
          if (b[v[x][j]] == 0) {
            b[v[x][j]] = cnt;
            q.push(v[x][j]);
          }
        q.pop();
      }
    }
  for (i = 1; i <= n; i++)
    mp[make_pair(a[i], b[i])]++;
  for (i = 1; i <= n; i++)
    printf("%d ", mp[make_pair(a[i], b[i])]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int a[N], b[N];
vector<int> v[N];
map<pair<int, int>, int> mp;
queue<int> q;
int main() {
  int n, k, l, i, j;
  cin >> n >> k >> l;
  for (i = 0; i < k; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int cnt = 0;
  for (i = 1; i <= n; i++)
    if (a[i] == 0) {
      a[i] = ++cnt;
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        for (j = 0; j < v[x].size(); j++)
          if (a[v[x][j]] == 0) {
            a[v[x][j]] = cnt;
            q.push(v[x][j]);
          }
        q.pop();
      }
    }
  for (i = 1; i <= n; i++)
    v[i].clear();
  for (i = 0; i < l; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  cnt = 0;
  for (i = 1; i <= n; i++)
    if (b[i] == 0) {
      b[i] = ++cnt;
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        for (j = 0; j < v[x].size(); j++)
          if (b[v[x][j]] == 0) {
            b[v[x][j]] = cnt;
            q.push(v[x][j]);
          }
        q.pop();
      }
    }
  for (i = 1; i <= n; i++)
    mp[make_pair(a[i], b[i])]++;
  for (i = 1; i <= n; i++)
    printf("%d ", mp[make_pair(a[i], b[i])]);
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <map>
using namespace std;
int N;
vector<int> p_a;
vector<int> p_b;
int r_a(int v) {
  if (p_a[v] == v)
    return v;
  return p_a[v] = r_a(p_a[v]);
}
int r_b(int v) {
  if (p_b[v] == v)
    return v;
  return p_b[v] = r_b(p_b[v]);
}
void u_a(int a, int b) {
  a = r_a(a);
  b = r_a(b);
  if (a == b)
    return;
  p_a[b] = a;
}
void u_b(int a, int b) {
  a = r_b(a);
  b = r_b(b);
  if (a == b)
    return;
  p_b[b] = a;
}
int main() {
  cin >> N;
  int K, L;
  cin >> K >> L;
  p_a.resize(N);
  p_b.resize(N);
  for (int i = 0; i < N; i++)
    p_a[i] = i;
  for (int i = 0; i < N; i++)
    p_b[i] = i;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    u_a(p, q);
  }
  for (int i = 0; i < L; i++) {
    int r, s;
    cin >> r >> s;
    u_b(r, s);
  }
  map<long long, int> ans;
  vector<long long> key(N);
  for (int i = 0; i < N; i++) {
    key[i] = (long long)r_a(i) * N + r_b(i);
    ans[key[i]]++;
  }
  for (int i = 0; i < N; i++)
    cout << ans[key[i]] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <map>
using namespace std;
int N;
vector<int> p_a;
vector<int> p_b;
int r_a(int v) {
  if (p_a[v] == v)
    return v;
  return p_a[v] = r_a(p_a[v]);
}
int r_b(int v) {
  if (p_b[v] == v)
    return v;
  return p_b[v] = r_b(p_b[v]);
}
void u_a(int a, int b) {
  a = r_a(a);
  b = r_a(b);
  if (a == b)
    return;
  p_a[b] = a;
}
void u_b(int a, int b) {
  a = r_b(a);
  b = r_b(b);
  if (a == b)
    return;
  p_b[b] = a;
}
int main() {
  cin >> N;
  int K, L;
  cin >> K >> L;
  p_a.resize(N);
  p_b.resize(N);
  for (int i = 0; i < N; i++)
    p_a[i] = i;
  for (int i = 0; i < N; i++)
    p_b[i] = i;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    u_a(p, q);
  }
  for (int i = 0; i < L; i++) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    u_b(r, s);
  }
  map<long long, int> ans;
  vector<long long> key(N);
  for (int i = 0; i < N; i++) {
    key[i] = (long long)r_a(i) * N + r_b(i);
    ans[key[i]]++;
  }
  for (int i = 0; i < N; i++)
    cout << ans[key[i]] << endl;
}
Problem p03855 (C++)

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

#define sz(x) int(x.size())
#define Task "abc"
#define For(i, a, b) for (int i = (a), _b = (b); i <= b; ++i)
#define All(x) (x).begin(), (x).end()
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;

const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int n, m, q;
int root_a[maxn], root_b[maxn];

int getroot(int u, int root[]) {
  return u == root[u] ? u : (root[u] = getroot(root[u], root));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i) {
    root_a[i] = i;
    root_b[i] = i;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    int ru = getroot(u, root_a), rv = getroot(v, root_a);
    if (ru == rv)
      continue;
    root_a[rv] = ru;
  }

  for (int i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;
    int ru = getroot(u, root_b), rv = getroot(v, root_b);
    if (ru == rv)
      continue;
    root_b[rv] = ru;
  }
  map<pair<int, int>, int> myMap;
  for (int u = 1; u <= n; ++u) {
    int ru = getroot(u, root_a), rv = getroot(u, root_b);
    myMap[mp(ru, rv)]++;
  }
  for (int u = 1; u <= n; ++u) {
    cout << myMap[mp(root_a[u], root_b[u])] << ' ';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define sz(x) int(x.size())
#define Task "abc"
#define For(i, a, b) for (int i = (a), _b = (b); i <= b; ++i)
#define All(x) (x).begin(), (x).end()
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;

const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int n, m, q;
int root_a[maxn], root_b[maxn];

int getroot(int u, int root[]) {
  return u == root[u] ? u : (root[u] = getroot(root[u], root));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i) {
    root_a[i] = i;
    root_b[i] = i;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    int ru = getroot(u, root_a), rv = getroot(v, root_a);
    if (ru == rv)
      continue;
    root_a[rv] = ru;
  }

  for (int i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;
    int ru = getroot(u, root_b), rv = getroot(v, root_b);
    if (ru == rv)
      continue;
    root_b[rv] = ru;
  }
  map<pair<int, int>, int> myMap;
  for (int u = 1; u <= n; ++u) {
    int ru = getroot(u, root_a), rv = getroot(u, root_b);
    myMap[mp(ru, rv)]++;
  }
  for (int u = 1; u <= n; ++u) {
    cout << myMap[mp(root_a[u], root_b[u])] << ' ';
  }
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, t1, t2, fa1[1000001], fa2[1000001];
map<int, int> g[100001];
int find1(int x) {
  if (x == fa1[x])
    return x;
  return fa1[x] = find1(fa1[x]);
}
int find2(int x) {
  if (x == fa2[x])
    return x;
  return fa2[x] = find2(fa2[x]);
}
int main() {
  int i, t, k, x, y;
  scanf("%d%d%d", &n, &t1, &t2);
  for (i = 1; i <= n; i++)
    fa1[i] = i, fa2[i] = i;
  for (i = 1; i <= t1; i++) {
    scanf("%d%d", &t, &k);
    x = find1(t);
    y = find1(k);
    if (x != y)
      fa1[x] = y;
  }
  for (i = 1; i <= t2; i++) {
    scanf("%d%d", &t, &k);
    x = find2(t);
    y = find2(k);
    if (x != y)
      fa2[x] = y;
  }
  for (i = 1; i <= n; i++) {
    x = find1(fa1[i]);
    y = find2(fa2[i]);
    // printf("%d %d %d\n",i,fa1[i],fa2[i]);
    g[x][y]++;
  }
  for (i = 1; i <= n; i++) {
    x = find1(fa1[i]);
    y = find2(fa2[i]);
    printf("%d ", g[x][y]);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, t1, t2, fa1[1000001], fa2[1000001];
map<int, int> g[1000001];
int find1(int x) {
  if (x == fa1[x])
    return x;
  return fa1[x] = find1(fa1[x]);
}
int find2(int x) {
  if (x == fa2[x])
    return x;
  return fa2[x] = find2(fa2[x]);
}
int main() {
  int i, t, k, x, y;
  scanf("%d%d%d", &n, &t1, &t2);
  for (i = 1; i <= n; i++)
    fa1[i] = i, fa2[i] = i;
  for (i = 1; i <= t1; i++) {
    scanf("%d%d", &t, &k);
    x = find1(t);
    y = find1(k);
    if (x != y)
      fa1[x] = y;
  }
  for (i = 1; i <= t2; i++) {
    scanf("%d%d", &t, &k);
    x = find2(t);
    y = find2(k);
    if (x != y)
      fa2[x] = y;
  }
  for (i = 1; i <= n; i++) {
    x = find1(fa1[i]);
    y = find2(fa2[i]);
    // printf("%d %d %d\n",i,fa1[i],fa2[i]);
    g[x][y]++;
  }
  for (i = 1; i <= n; i++) {
    x = find1(fa1[i]);
    y = find2(fa2[i]);
    printf("%d ", g[x][y]);
  }
}
Problem p03855 (C++)

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

struct UnionFind {
  vector<int> par, rank;

  UnionFind(int n) {
    par.resize(n);
    fill(par.begin(), par.end(), -1);
    rank.resize(n);
  }

  int root(int x) {
    if (par[x] < 0) {
      return x;
    }

    par[x] = root(par[x]);

    return par[x];
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx != ry) {
      if (rank[rx] >= rank[ry]) {
        par[ry] = rx;

        if (rank[rx] == rank[ry]) {
          rank[rx] += 1;
        }
      } else {
        par[rx] = ry;
      }
    }
  }

  bool is_same(int x, int y) { return root(x) == root(y); }

  int count(int x) { return -par[root(x)]; }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;

  UnionFind roads = UnionFind(n);
  UnionFind rails = UnionFind(n);

  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    roads.unite(p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    rails.unite(r, s);
  }

  map<pair<int, int>, int> counter;

  for (int i = 1; i <= n; i++) {
    counter[make_pair(roads.root(i), rails.root(i))]++;
  }

  for (int i = 1; i <= n; i++) {
    cout << counter[make_pair(roads.root(i), rails.root(i))] << ' ';
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector<int> par, rank;

  UnionFind(int n) {
    par.resize(n);
    fill(par.begin(), par.end(), -1);
    rank.resize(n);
  }

  int root(int x) {
    if (par[x] < 0) {
      return x;
    }

    par[x] = root(par[x]);

    return par[x];
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx != ry) {
      if (rank[rx] >= rank[ry]) {
        par[ry] = rx;

        if (rank[rx] == rank[ry]) {
          rank[rx] += 1;
        }
      } else {
        par[rx] = ry;
      }
    }
  }

  bool is_same(int x, int y) { return root(x) == root(y); }

  int count(int x) { return -par[root(x)]; }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;

  UnionFind roads = UnionFind(n + 1);
  UnionFind rails = UnionFind(n + 1);

  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    roads.unite(p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    rails.unite(r, s);
  }

  map<pair<int, int>, int> counter;

  for (int i = 1; i <= n; i++) {
    counter[make_pair(roads.root(i), rails.root(i))]++;
  }

  for (int i = 1; i <= n; i++) {
    cout << counter[make_pair(roads.root(i), rails.root(i))] << ' ';
  }
  cout << endl;

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define N 100005
vi par(N);
vi unionrank(N);
void init(int n) {
  rep(i, n) {
    par[i] = i;
    unionrank[i] = 0;
  }
}
int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  }
  if (unionrank[x] < unionrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (unionrank[x] == unionrank[y]) {
      unionrank[x]++;
    }
  }
}
bool same(int x, int y) { return root(x) == root(y); }
int main() {
  int n, k, l;
  cin >> n >> k >> l;
  init(2 * n);
  map<P, int> m;
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q);
  }
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    unite(s + n, r + n);
  }
  int a[n];
  int b[n];
  rep(i, n) {
    a[i] = root(i);
    b[i] = root(i + n);
  }
  rep(i, n) { m[P(a[i], b[i])]++; }
  rep(i, n - 1) { cout << m[P(a[i], b[i])] << " "; }
  cout << m[P(a[n - 1], b[n - 1])] << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define N 200005
vi par(2 * N);
vi unionrank(2 * N);
void init(int n) {
  rep(i, n) {
    par[i] = i;
    unionrank[i] = 0;
  }
}
int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  }
  if (unionrank[x] < unionrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (unionrank[x] == unionrank[y]) {
      unionrank[x]++;
    }
  }
}
bool same(int x, int y) { return root(x) == root(y); }
int main() {
  int n, k, l;
  cin >> n >> k >> l;
  init(2 * n);
  map<P, int> m;
  rep(i, k) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    unite(p, q);
  }
  rep(i, l) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    unite(s + n, r + n);
  }
  int a[n];
  int b[n];
  rep(i, n) {
    a[i] = root(i);
    b[i] = root(i + n);
  }
  rep(i, n) { m[P(a[i], b[i])]++; }
  rep(i, n - 1) { cout << m[P(a[i], b[i])] << " "; }
  cout << m[P(a[n - 1], b[n - 1])] << endl;

  return 0;
}