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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 100005;
map<pair<int, int>, int> mp;
int fa1[MAXN], fa2[MAXN], *fa, n, *head, m1, m2, cnt;
int head1[MAXN], head2[MAXN];
struct Edge {
  int to, next;
} e[MAXN << 2];
inline void insert(int u, int v) {
  e[++cnt].to = v, e[cnt].next = head[u], head[u] = cnt;
  e[++cnt].to = u, e[cnt].next = head[v], head[v] = cnt;
}
int *vis, vis1[MAXN], vis2[MAXN], q[MAXN];
inline void bfs(int s) {
  int front = 1, back = 0, u, v;
  q[++back] = s, vis[s] = 1;
  while (front <= back) {
    u = q[front++];
    for (int i = head[u]; i; i = e[i].next) {
      v = e[i].to;
      if (!vis[v]) {
        vis[v] = 1;
        q[++back] = v;
        fa[v] = s;
      }
    }
  }
}
int main() {
  head = head1;
  scanf("%d%d%d", &n, &m1, &m2);
  for (int i = 1; i <= m1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    insert(u, v);
  }
  head = head2;
  for (int i = 1; i <= m2; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    insert(u, v);
  }
  head = head1;
  fa = fa1, vis = vis1;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  head = head2;
  fa = fa2, vis = vis2;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  for (int i = 1; i <= n; ++i)
    ++mp[make_pair(fa1[i], fa2[i])];
  for (int i = 1; i <= n; ++i)
    printf("%d ", mp[make_pair(fa1[i], fa2[i])]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 300005;
map<pair<int, int>, int> mp;
int fa1[MAXN], fa2[MAXN], *fa, n, *head, m1, m2, cnt;
int head1[MAXN], head2[MAXN];
struct Edge {
  int to, next;
} e[MAXN << 2];
inline void insert(int u, int v) {
  e[++cnt].to = v, e[cnt].next = head[u], head[u] = cnt;
  e[++cnt].to = u, e[cnt].next = head[v], head[v] = cnt;
}
int *vis, vis1[MAXN], vis2[MAXN], q[MAXN];
inline void bfs(int s) {
  int front = 1, back = 0, u, v;
  q[++back] = s, vis[s] = 1;
  while (front <= back) {
    u = q[front++];
    for (int i = head[u]; i; i = e[i].next) {
      v = e[i].to;
      if (!vis[v]) {
        vis[v] = 1;
        q[++back] = v;
        fa[v] = s;
      }
    }
  }
}
int main() {
  head = head1;
  scanf("%d%d%d", &n, &m1, &m2);
  for (int i = 1; i <= m1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    insert(u, v);
  }
  head = head2;
  for (int i = 1; i <= m2; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    insert(u, v);
  }
  head = head1;
  fa = fa1, vis = vis1;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  head = head2;
  fa = fa2, vis = vis2;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  for (int i = 1; i <= n; ++i)
    ++mp[make_pair(fa1[i], fa2[i])];
  for (int i = 1; i <= n; ++i)
    printf("%d ", mp[make_pair(fa1[i], fa2[i])]);
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rep_r(i, s, n) for (int i = n - 1; i >= 0; i--)
#define rep_re(i, s, n) for (int i = n; i >= 0; i--)
using namespace std;
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = 1e25;
static const ll MAX_M = 100001;

ll a[100001] = {};

// initial : call init_uf();
// unite x and y : p[uf(x)] = uf(y)
// same x and y : uf(x) == uf(y)
static const ll MAX_N = 100001;
ll p[MAX_N] = {};
ll uf(ll i) { return (p[i] == i) ? i : p[i] = uf(p[i]); }
void init_uf() { rep(i, 1, MAX_N) p[i] = i; }

ll q[MAX_N] = {};
ll uf2(ll i) { return (q[i] == i) ? i : q[i] = uf2(q[i]); }
void init_uf2() { rep(i, 1, MAX_N) q[i] = i; }

int main() {
  init_uf();
  init_uf2();
  ll n, k, l;
  cin >> n >> k >> l;
  rep(i, 0, k) {
    ll x, y;
    cin >> x >> y;
    if (uf(x) != uf(y))
      p[uf(x)] = uf(y);
  }
  rep(i, 0, l) {
    ll x, y;
    cin >> x >> y;
    if (uf2(x) != uf2(y))
      q[uf2(x)] = uf2(y);
  }

  map<pair<ll, ll>, ll> m;
  repe(i, 1, n) m[{uf(i), uf2(i)}]++;
  repe(i, 1, n) cout << m[{uf(i), uf2(i)}] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rep_r(i, s, n) for (int i = n - 1; i >= 0; i--)
#define rep_re(i, s, n) for (int i = n; i >= 0; i--)
using namespace std;
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = 1e25;
static const ll MAX_M = 100001;

ll a[100001] = {};

// initial : call init_uf();
// unite x and y : p[uf(x)] = uf(y)
// same x and y : uf(x) == uf(y)
static const ll MAX_N = 200001;
ll p[MAX_N] = {};
ll uf(ll i) { return (p[i] == i) ? i : p[i] = uf(p[i]); }
void init_uf() { rep(i, 1, MAX_N) p[i] = i; }

ll q[MAX_N] = {};
ll uf2(ll i) { return (q[i] == i) ? i : q[i] = uf2(q[i]); }
void init_uf2() { rep(i, 1, MAX_N) q[i] = i; }

int main() {
  init_uf();
  init_uf2();
  ll n, k, l;
  cin >> n >> k >> l;
  rep(i, 0, k) {
    ll x, y;
    cin >> x >> y;
    if (uf(x) != uf(y))
      p[uf(x)] = uf(y);
  }
  rep(i, 0, l) {
    ll x, y;
    cin >> x >> y;
    if (uf2(x) != uf2(y))
      q[uf2(x)] = uf2(y);
  }

  map<pair<ll, ll>, ll> m;
  repe(i, 1, n) m[{uf(i), uf2(i)}]++;
  repe(i, 1, n) cout << m[{uf(i), uf2(i)}] << endl;
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int maxn = 1e5 + 10;
int F1[maxn], F2[maxn];
int findset1(int x) {
  if (F1[x] == x)
    return x;
  return F1[x] = findset1(F1[x]);
}
int findset2(int x) {
  if (F2[x] == x)
    return x;
  return F2[x] = findset2(F2[x]);
}
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= n; i++) {
    F1[i] = i;
    F2[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int fx = findset1(x);
    int fy = findset1(y);
    if (fx != fy)
      F1[fx] = fy;
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int fx = findset2(x);
    int fy = findset2(y);
    if (fx != fy)
      F2[fx] = fy;
  }
  for (int i = 1; i <= n; i++) {
    findset1(i);
    findset2(i);
  }
  map<pair<int, int>, int> mp;
  for (int i = 1; i <= n; i++) {
    mp[make_pair(F1[i], F2[i])]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << mp[make_pair(F1[i], F2[i])] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
const int maxn = 2e5 + 10;
int F1[maxn], F2[maxn];
int findset1(int x) {
  if (F1[x] == x)
    return x;
  return F1[x] = findset1(F1[x]);
}
int findset2(int x) {
  if (F2[x] == x)
    return x;
  return F2[x] = findset2(F2[x]);
}
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= n; i++) {
    F1[i] = i;
    F2[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int fx = findset1(x);
    int fy = findset1(y);
    if (fx != fy)
      F1[fx] = fy;
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int fx = findset2(x);
    int fy = findset2(y);
    if (fx != fy)
      F2[fx] = fy;
  }
  for (int i = 1; i <= n; i++) {
    findset1(i);
    findset2(i);
  }
  map<pair<int, int>, int> mp;
  for (int i = 1; i <= n; i++) {
    mp[make_pair(F1[i], F2[i])]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << mp[make_pair(F1[i], F2[i])] << " ";
  }
  cout << endl;
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int find(int x);
int same(int x, int y);
void unite(int x, int y);
int qfind(int x);
int qsame(int x, int y);
void qunite(int x, int y);
int p[200009];
int q[200009];
map<int, int> mp[100009];

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i <= n; i++) {
    p[i] = i;
    q[i] = i;
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    if (!same(a, b)) {
      unite(a, b);
    }
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    if (!qsame(a, b)) {
      qunite(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    mp[find(i)][qfind(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << mp[find(i)][qfind(i)];
    if (i == n)
      cout << endl;
    else
      cout << ' ';
  }
  return (0);
}

int find(int x) {
  if (p[x] == x)
    return x;
  return p[x] = find(p[x]);
}

int same(int x, int y) {
  if (find(x) == find(y))
    return 1;
  return 0;
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  p[x] = y;
}

int qfind(int x) {
  if (q[x] == x)
    return x;
  return q[x] = qfind(q[x]);
}

int qsame(int x, int y) {
  if (qfind(x) == qfind(y))
    return 1;
  return 0;
}

void qunite(int x, int y) {
  x = qfind(x);
  y = qfind(y);
  q[x] = y;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int find(int x);
int same(int x, int y);
void unite(int x, int y);
int qfind(int x);
int qsame(int x, int y);
void qunite(int x, int y);
int p[200009];
int q[200009];
map<int, int> mp[200009];

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i <= n; i++) {
    p[i] = i;
    q[i] = i;
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    if (!same(a, b)) {
      unite(a, b);
    }
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    if (!qsame(a, b)) {
      qunite(a, b);
    }
  }
  for (int i = 1; i <= n; i++) {
    mp[find(i)][qfind(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    cout << mp[find(i)][qfind(i)];
    if (i == n)
      cout << endl;
    else
      cout << ' ';
  }
  return (0);
}

int find(int x) {
  if (p[x] == x)
    return x;
  return p[x] = find(p[x]);
}

int same(int x, int y) {
  if (find(x) == find(y))
    return 1;
  return 0;
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  p[x] = y;
}

int qfind(int x) {
  if (q[x] == x)
    return x;
  return q[x] = qfind(q[x]);
}

int qsame(int x, int y) {
  if (qfind(x) == qfind(y))
    return 1;
  return 0;
}

void qunite(int x, int y) {
  x = qfind(x);
  y = qfind(y);
  q[x] = y;
}
Problem p03857 (C++)

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

using namespace std;

const int N = 1e5 + 10;

int f1[N], f2[N];
map<pair<int, int>, int> ans;

int getf(int *f, int u) {
  if (u == f[u])
    return u;
  return f[u] = getf(f, f[u]);
}

void merge(int *f, int x, int y) {
  int u = getf(f, x), v = getf(f, y);
  if (u != v)
    f[u] = v;
}

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    f1[i] = f2[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    merge(f1, x, y);
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    merge(f2, x, y);
  }
  for (int i = 1; i <= n; i++) {
    ans[make_pair(getf(f1, i), getf(f2, i))]++;
  }
  for (int i = 1; i <= n; i++) {
    printf("%d", ans[make_pair(getf(f1, i), getf(f2, i))]);
    if (i == n)
      puts("");
    else
      putchar(' ');
  }
}

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

using namespace std;

const int N = 2e5 + 10;

int f1[N], f2[N];
map<pair<int, int>, int> ans;

int getf(int *f, int u) {
  if (u == f[u])
    return u;
  return f[u] = getf(f, f[u]);
}

void merge(int *f, int x, int y) {
  int u = getf(f, x), v = getf(f, y);
  if (u != v)
    f[u] = v;
}

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    f1[i] = f2[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    merge(f1, x, y);
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    merge(f2, x, y);
  }
  for (int i = 1; i <= n; i++) {
    ans[make_pair(getf(f1, i), getf(f2, i))]++;
  }
  for (int i = 1; i <= n; i++) {
    printf("%d", ans[make_pair(getf(f1, i), getf(f2, i))]);
    if (i == n)
      puts("");
    else
      putchar(' ');
  }
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

class UnionFind {
  vector<int> par, high;

public:
  vector<int> size;
  UnionFind(int n) {
    par = high = vector<int>(n);
    size = vector<int>(n, 1);
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  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 (high[x] > high[y]) {
      par[y] = x;
      size[x] += size[y];
    } else {
      par[x] = y;
      size[y] += size[x];
      if (high[x] == high[y])
        high[y]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
map<int, int> E[100000];
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  UnionFind road(n), rail(n);
  rep(i, k) {
    int p, q;
    scanf("%d%d", &p, &q);
    p--;
    q--;
    road.unite(p, q);
  }
  rep(i, l) {
    int r, s;
    scanf("%d%d", &r, &s);
    r--;
    s--;
    rail.unite(r, s);
  }
  rep(i, n) { E[road.find(i)][rail.find(i)]++; }
  rep(i, n) {
    int r = road.find(i), s = rail.find(i);
    printf("%d", E[r][s]);
    if (i == n - 1)
      printf("\n");
    else
      printf(" ");
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

class UnionFind {
  vector<int> par, high;

public:
  vector<int> size;
  UnionFind(int n) {
    par = high = vector<int>(n);
    size = vector<int>(n, 1);
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  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 (high[x] > high[y]) {
      par[y] = x;
      size[x] += size[y];
    } else {
      par[x] = y;
      size[y] += size[x];
      if (high[x] == high[y])
        high[y]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
map<int, int> E[200000];
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  UnionFind road(n), rail(n);
  rep(i, k) {
    int p, q;
    scanf("%d%d", &p, &q);
    p--;
    q--;
    road.unite(p, q);
  }
  rep(i, l) {
    int r, s;
    scanf("%d%d", &r, &s);
    r--;
    s--;
    rail.unite(r, s);
  }
  rep(i, n) { E[road.find(i)][rail.find(i)]++; }
  rep(i, n) {
    int r = road.find(i), s = rail.find(i);
    printf("%d", E[r][s]);
    if (i == n - 1)
      printf("\n");
    else
      printf(" ");
  }
}
Problem p03857 (C++)

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

const long long int maxKL = 1e5 + 1;

vector<int> kv(maxKL, 0);
vector<int> lv(maxKL, 0);
vector<int> Gk[maxKL];
vector<int> Gl[maxKL];
int np;

int dfsk(int p) {
  for (int i = 0; i < Gk[p].size(); i++) {
    if (kv[Gk[p][i]] == 0) {
      kv[Gk[p][i]] = np;
      dfsk(Gk[p][i]);
    }
  }
  return 0;
}

int dfsl(int p) {
  for (int i = 0; i < Gl[p].size(); i++) {
    if (lv[Gl[p][i]] == 0) {
      lv[Gl[p][i]] = np;
      dfsl(Gl[p][i]);
    }
  }

  return 0;
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  int a, b;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    a--;
    b--;
    Gk[a].push_back(b);
    Gk[b].push_back(a);
  }
  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    a--;
    b--;
    Gl[a].push_back(b);
    Gl[b].push_back(a);
  }
  np = 1;
  for (int i = 0; i < n; i++) {
    if (kv[i] == 0) {
      kv[i] = np;
      dfsk(i);
      np++;
    }
  }
  np = 1;
  for (int i = 0; i < n; i++) {
    if (lv[i] == 0) {
      lv[i] = np;
      dfsl(i);
      np++;
    }
  }

  map<pair<int, int>, int> mp;
  for (int i = 0; i < n; i++)
    mp[make_pair(kv[i], lv[i])]++;
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << mp[make_pair(kv[i], lv[i])];
  }
  cout << endl;

  /*
  for(int i=0;i<n;i++){
          cout<<"k"<<i+1<<" "<<kv[i]<<endl;
  }
  for(int i=0;i<n;i++){
          cout<<"l"<<i+1<<" "<<lv[i]<<endl;
  }
  */

  return 0;
}

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

const long long int maxKL = (1e5) * 2 + 1;

vector<int> kv(maxKL, 0);
vector<int> lv(maxKL, 0);
vector<int> Gk[maxKL];
vector<int> Gl[maxKL];
int np;

int dfsk(int p) {
  for (int i = 0; i < Gk[p].size(); i++) {
    if (kv[Gk[p][i]] == 0) {
      kv[Gk[p][i]] = np;
      dfsk(Gk[p][i]);
    }
  }
  return 0;
}

int dfsl(int p) {
  for (int i = 0; i < Gl[p].size(); i++) {
    if (lv[Gl[p][i]] == 0) {
      lv[Gl[p][i]] = np;
      dfsl(Gl[p][i]);
    }
  }

  return 0;
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  int a, b;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    a--;
    b--;
    Gk[a].push_back(b);
    Gk[b].push_back(a);
  }
  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    a--;
    b--;
    Gl[a].push_back(b);
    Gl[b].push_back(a);
  }
  np = 1;
  for (int i = 0; i < n; i++) {
    if (kv[i] == 0) {
      kv[i] = np;
      dfsk(i);
      np++;
    }
  }
  np = 1;
  for (int i = 0; i < n; i++) {
    if (lv[i] == 0) {
      lv[i] = np;
      dfsl(i);
      np++;
    }
  }

  map<pair<int, int>, int> mp;
  for (int i = 0; i < n; i++)
    mp[make_pair(kv[i], lv[i])]++;
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << mp[make_pair(kv[i], lv[i])];
  }
  cout << endl;

  /*
  for(int i=0;i<n;i++){
          cout<<"k"<<i+1<<" "<<kv[i]<<endl;
  }
  for(int i=0;i<n;i++){
          cout<<"l"<<i+1<<" "<<lv[i]<<endl;
  }
  */

  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;

const int gmax_n = 200005;

int par[gmax_n];   // 親
int depth[gmax_n]; // 木の深さ

void init(int n) {
  rep(i, n) {
    par[i] = i;
    depth[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}

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

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

  init(n * 2);
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a, b);
  }

  rep(i, l) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a + n, b + n);
  }

  pair<int, int> p[gmax_n];
  map<pair<int, int>, int> m;
  rep(i, n) p[i] = make_pair(find(i), find(i + n));
  rep(i, n) m[p[i]]++;
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << m[p[i]];
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;

const int gmax_n = 200005;

int par[gmax_n * 2];   // 親
int depth[gmax_n * 2]; // 木の深さ

void init(int n) {
  rep(i, n) {
    par[i] = i;
    depth[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}

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

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

  init(n * 2);
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a, b);
  }

  rep(i, l) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a + n, b + n);
  }

  pair<int, int> p[gmax_n];
  map<pair<int, int>, int> m;
  rep(i, n) p[i] = make_pair(find(i), find(i + n));
  rep(i, n) m[p[i]]++;
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << m[p[i]];
  }
  cout << endl;
}
Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

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

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
int main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind A(N);
  UnionFind B(N);
  for (int I = 0; I < K; I++) {
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    A.merge(X, Y);
  }
  for (int I = 0; I < L; I++) {
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    B.merge(X, Y);
  }
  for (int X = 0; X < N; X++) {
    int M = 0;
    for (int Y = 0; Y < N; Y++) {
      if (X == Y)
        continue;
      if (A.issame(X, Y) && B.issame(X, Y))
        M++;
    }
    if (X == N - 1)
      cout << M + 1 << endl;
    else
      cout << M + 1 << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

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

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
int main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind A(N);
  UnionFind B(N);
  for (int I = 0; I < K; I++) {
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    A.merge(X, Y);
  }
  for (int I = 0; I < L; I++) {
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    B.merge(X, Y);
  }
  map<pair<int, int>, int> ma;
  for (int v = 0; v < N; ++v) {
    pair<int, int> p(A.root(v), B.root(v));
    ma[p]++;
  }

  for (int v = 0; v < N; ++v) {
    pair<int, int> p(A.root(v), B.root(v));
    cout << ma[p] << " ";
  }
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll(i) = (0); (i) < (ll)(n); ++(i))
using ll = long long;

struct UnionFind {
  static const int Max = 100001;
  int par[Max];
  int rank[Max];

  void init(int n) {
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
  }

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

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

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  UnionFind r, l;
  r.init(N + 1);
  l.init(N + 1);

  rep(i, K) {
    int p, q;
    cin >> p >> q;
    p--, q--;
    r.unite(p, q);
  }

  rep(i, L) {
    int r, s;
    cin >> r >> s;
    r--, s--;
    l.unite(r, s);
  }

  map<pair<int, int>, int> mp;
  rep(i, N) { mp[{r.find(i), l.find(i)}]++; }

  rep(i, N) { cout << mp[{r.find(i), l.find(i)}] << " "; }

  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll(i) = (0); (i) < (ll)(n); ++(i))
using ll = long long;

struct UnionFind {
  static const int Max = 300001;
  int par[Max];
  int rank[Max];

  void init(int n) {
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
  }

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

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

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  UnionFind r, l;
  r.init(N + 1);
  l.init(N + 1);

  rep(i, K) {
    int p, q;
    cin >> p >> q;
    p--, q--;
    r.unite(p, q);
  }

  rep(i, L) {
    int r, s;
    cin >> r >> s;
    r--, s--;
    l.unite(r, s);
  }

  map<pair<int, int>, int> mp;
  rep(i, N) { mp[{r.find(i), l.find(i)}]++; }

  rep(i, N) { cout << mp[{r.find(i), l.find(i)}] << " "; }

  cout << endl;
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

int rootK[100000];
int rootL[100000];

int getRootK(int v) {
  return rootK[v] == -1 ? v : rootK[v] = getRootK(rootK[v]);
}
int getRootL(int v) {
  return rootL[v] == -1 ? v : rootL[v] = getRootL(rootL[v]);
}

int main() {
  int N, K, L;
  while (cin >> N >> K >> L) {
    memset(rootK, -1, sizeof(rootK));
    memset(rootL, -1, sizeof(rootL));
    for (int i = 0; i < K; i++) {
      int p, q;
      cin >> p >> q;
      p = getRootK(p - 1);
      q = getRootK(q - 1);
      if (p != q)
        rootK[q] = p;
    }
    for (int i = 0; i < L; i++) {
      int p, q;
      cin >> p >> q;
      p = getRootL(p - 1);
      q = getRootL(q - 1);
      if (p != q)
        rootL[q] = p;
    }
    map<pair<int, int>, int> cnt;
    for (int i = 0; i < N; i++) {
      ++cnt[make_pair(getRootK(i), getRootL(i))];
    }
    for (int i = 0; i < N; i++) {
      cout << cnt[make_pair(getRootK(i), getRootL(i))];
      if (i == N - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

int rootK[200000];
int rootL[200000];

int getRootK(int v) {
  return rootK[v] == -1 ? v : rootK[v] = getRootK(rootK[v]);
}
int getRootL(int v) {
  return rootL[v] == -1 ? v : rootL[v] = getRootL(rootL[v]);
}

int main() {
  int N, K, L;
  while (cin >> N >> K >> L) {
    memset(rootK, -1, sizeof(rootK));
    memset(rootL, -1, sizeof(rootL));
    for (int i = 0; i < K; i++) {
      int p, q;
      cin >> p >> q;
      p = getRootK(p - 1);
      q = getRootK(q - 1);
      if (p != q)
        rootK[q] = p;
    }
    for (int i = 0; i < L; i++) {
      int p, q;
      cin >> p >> q;
      p = getRootL(p - 1);
      q = getRootL(q - 1);
      if (p != q)
        rootL[q] = p;
    }
    map<pair<int, int>, int> cnt;
    for (int i = 0; i < N; i++) {
      ++cnt[make_pair(getRootK(i), getRootL(i))];
    }
    for (int i = 0; i < N; i++) {
      cout << cnt[make_pair(getRootK(i), getRootL(i))];
      if (i == N - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int maxn = 1e6;
int fa1[N], fa2[N];
int find1(int x) {
  if (fa1[x] != x)
    fa1[x] = find1(fa1[x]);
  return fa1[x];
}
int find2(int x) {
  if (fa2[x] != x)
    fa2[x] = find2(fa2[x]);
  return fa2[x];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++)
    fa1[i] = i, fa2[i] = i;
  for (int i = 1; i <= k; i++) {
    cin >> x >> y;
    int r1 = find1(x);
    int r2 = find1(y);
    if (r1 != r2)
      fa1[r2] = r1;
  }
  for (int i = 1; i <= l; i++) {
    cin >> x >> y;
    int r1 = find2(x);
    int r2 = find2(y);
    if (r1 != r2)
      fa2[r2] = r1;
  }
  for (int i = 1; i <= n; i++) {
    find1(i);
    find2(i);
  }
  map<pair<int, int>, int> f;
  for (int i = 1; i <= n; i++) {
    f[make_pair(fa1[i], fa2[i])]++;
  }
  for (int i = 1; i <= n; i++)
    cout << f[make_pair(fa1[i], fa2[i])] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int maxn = 1e6;
int fa1[N], fa2[N];
int find1(int x) {
  if (fa1[x] != x)
    fa1[x] = find1(fa1[x]);
  return fa1[x];
}
int find2(int x) {
  if (fa2[x] != x)
    fa2[x] = find2(fa2[x]);
  return fa2[x];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++)
    fa1[i] = i, fa2[i] = i;
  for (int i = 1; i <= k; i++) {
    cin >> x >> y;
    int r1 = find1(x);
    int r2 = find1(y);
    if (r1 != r2)
      fa1[r2] = r1;
  }
  for (int i = 1; i <= l; i++) {
    cin >> x >> y;
    int r1 = find2(x);
    int r2 = find2(y);
    if (r1 != r2)
      fa2[r2] = r1;
  }
  for (int i = 1; i <= n; i++) {
    find1(i);
    find2(i);
  }
  map<pair<int, int>, int> f;
  for (int i = 1; i <= n; i++) {
    f[make_pair(fa1[i], fa2[i])]++;
  }
  for (int i = 1; i <= n; i++)
    cout << f[make_pair(fa1[i], fa2[i])] << " ";
  return 0;
}
Problem p03857 (C++)

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

int n, m[2];
vector<pair<int, int>> E;
int U[2][200010];
int find(int x, int t) { return x == U[t][x] ? x : U[t][x] = find(U[t][x], t); }
void unite(int x, int y, int t) {
  x = find(x, t), y = find(y, t);
  if (x != y)
    U[t][x] = y;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m[0] >> m[1];
  for (int i = 1; i <= n; i++)
    U[0][i] = U[1][i] = U[2][i] = i;
  for (int k = 0; k < 2; k++) {
    for (int i = 1; i <= m[k]; i++) {
      int u, v;
      cin >> u >> v;
      unite(u, v, k);
    }
  }
  map<pair<int, int>, int> cnt;
  for (int i = 1; i <= n; i++)
    cnt[{find(i, 0), find(i, 1)}]++;
  for (int i = 1; i <= n; i++)
    cout << cnt[{find(i, 0), find(i, 1)}] << ' ';
  cout << '\n';
  return 0;
}

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

int n, m[2];
vector<pair<int, int>> E;
int U[2][200010];
int find(int x, int t) { return x == U[t][x] ? x : U[t][x] = find(U[t][x], t); }
void unite(int x, int y, int t) {
  x = find(x, t), y = find(y, t);
  if (x != y)
    U[t][x] = y;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m[0] >> m[1];
  for (int i = 1; i <= n; i++)
    U[0][i] = U[1][i] = i;
  for (int k = 0; k < 2; k++) {
    for (int i = 1; i <= m[k]; i++) {
      int u, v;
      cin >> u >> v;
      unite(u, v, k);
    }
  }
  map<pair<int, int>, int> cnt;
  for (int i = 1; i <= n; i++)
    cnt[{find(i, 0), find(i, 1)}]++;
  for (int i = 1; i <= n; i++)
    cout << cnt[{find(i, 0), find(i, 1)}] << ' ';
  cout << '\n';
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<int>;
const int maxn = 62, mlg = 18, rt = 1 << 10, mod = 1e9 + 7;
struct dsu {
  vi p, r;
  vector<vi> col;
  dsu(int n) {
    p.resize(n + 1);
    iota(all(p), 0);
    r.resize(n + 1, 0);
    col.resize(n + 1);
  }
  int par(int v) { return v ^ p[v] ? p[v] = par(p[v]) : v; }
  void unite(int i, int j) {
    i = par(i), j = par(j);
    if (i == j)
      return;
    if (r[i] < r[j])
      swap(i, j);
    p[j] = i, r[i] += r[j];
    for (auto x : col[j])
      col[i].push_back(x);
  }
};
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, k, l;
  cin >> n >> k >> l;
  dsu a(n), b(n);
  int f, t;
  while (k--) {
    cin >> f >> t;
    a.unite(f, t);
  }
  for (int i = 1; i <= n; i++)
    b.col[i].pb(a.par(i));
  while (l--) {
    cin >> f >> t;
    b.unite(f, t);
  }
  vector<map<int, int>> ans(n + 1);
  for (int i = 1; i <= n; i++)
    if (i == b.par(i)) {
      for (auto j : b.col[i])
        ans[j][i]++;
    }
  for (int i = 1; i <= n; i++) {
    f = a.par(i), t = b.par(i);
    cout << ans[f][t] << ' ';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<int>;
const int maxn = 62, mlg = 18, rt = 1 << 10, mod = 1e9 + 7;
struct dsu {
  vi p, r;
  vector<vi> col;
  dsu(int n) {
    p.resize(n + 1);
    iota(all(p), 0);
    r.resize(n + 1, 1);
    col.resize(n + 1);
  }
  int par(int v) { return v ^ p[v] ? p[v] = par(p[v]) : v; }
  void unite(int i, int j) {
    i = par(i), j = par(j);
    if (i == j)
      return;
    if (r[i] < r[j])
      swap(i, j);
    p[j] = i, r[i] += r[j];
    for (auto x : col[j])
      col[i].push_back(x);
  }
};
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, k, l;
  cin >> n >> k >> l;
  dsu a(n), b(n);
  int f, t;
  while (k--) {
    cin >> f >> t;
    a.unite(f, t);
  }
  for (int i = 1; i <= n; i++)
    b.col[i].pb(a.par(i));
  while (l--) {
    cin >> f >> t;
    b.unite(f, t);
  }
  vector<map<int, int>> ans(n + 1);
  for (int i = 1; i <= n; i++)
    if (i == b.par(i)) {
      for (auto j : b.col[i])
        ans[j][i]++;
    }
  for (int i = 1; i <= n; i++) {
    f = a.par(i), t = b.par(i);
    cout << ans[f][t] << ' ';
  }
}
Problem p03857 (C++)

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

int n, m[2], p[2][100010];
map<int, int> mp[200010];

int fnd(int t, int x) { return p[t][x] = (p[t][x] == x ? x : fnd(t, p[t][x])); }
void uni(int t, int x, int y) { p[t][fnd(t, y)] = fnd(t, x); }

int main() {
  scanf("%d%d%d", &n, m, m + 1);
  for (int t = 0; t < 2; t++) {
    iota(p[t] + 1, p[t] + n + 1, 1);
    for (int i = 0, x, y; i < m[t]; i++) {
      scanf("%d%d", &x, &y);
      uni(t, x, y);
    }
  }
  for (int i = 1; i <= n; i++)
    mp[fnd(0, i)][fnd(1, i)]++;
  for (int i = 1; i <= n; i++)
    printf("%d\n", mp[fnd(0, i)][fnd(1, i)]);
}

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

int n, m[2], p[2][200010];
map<int, int> mp[200010];

int fnd(int t, int x) { return p[t][x] = (p[t][x] == x ? x : fnd(t, p[t][x])); }
void uni(int t, int x, int y) { p[t][fnd(t, y)] = fnd(t, x); }

int main() {
  scanf("%d%d%d", &n, m, m + 1);
  for (int t = 0; t < 2; t++) {
    iota(p[t] + 1, p[t] + n + 1, 1);
    for (int i = 0, x, y; i < m[t]; i++) {
      scanf("%d%d", &x, &y);
      uni(t, x, y);
    }
  }
  for (int i = 1; i <= n; i++)
    mp[fnd(0, i)][fnd(1, i)]++;
  for (int i = 1; i <= n; i++)
    printf("%d\n", mp[fnd(0, i)][fnd(1, i)]);
}
Problem p03857 (C++)

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

class UnionFind {
public:
  vector<int> tree;

  void init() {
    for (int i = 0; i < tree.size(); i++) {
      tree[i] = i;
    }
  }

  UnionFind(int n) {
    n++;
    tree.resize(n);
    init();
    return;
  }

  int root(int i) {
    if (tree[i] == i)
      return i;
    return tree[i] = root(tree[i]);
  }

  bool unite(int i, int j) {
    i = root(i);
    j = root(j);
    if (i == j)
      return false;
    tree[j] = i;
    return true;
  }

  bool same(int i, int j) { return root(i) == root(j); }
};

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

  int a, b;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    uf1.unite(a, b);
  }

  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    uf2.unite(a, b);
  }

  map<int, int> mp[100010];
  for (int i = 0; i < n; i++) {
    mp[uf1.root(i + 1)][uf2.root(i + 1)]++;
  }

  for (int i = 0; i < n; i++) {
    cout << mp[uf1.root(i + 1)][uf2.root(i + 1)];
    if (i < n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}


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

class UnionFind {
public:
  vector<int> tree;

  void init() {
    for (int i = 0; i < tree.size(); i++) {
      tree[i] = i;
    }
  }

  UnionFind(int n) {
    n++;
    tree.resize(n);
    init();
    return;
  }

  int root(int i) {
    if (tree[i] == i)
      return i;
    return tree[i] = root(tree[i]);
  }

  bool unite(int i, int j) {
    i = root(i);
    j = root(j);
    if (i == j)
      return false;
    tree[j] = i;
    return true;
  }

  bool same(int i, int j) { return root(i) == root(j); }
};

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

  int a, b;
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    uf1.unite(a, b);
  }

  for (int i = 0; i < l; i++) {
    cin >> a >> b;
    uf2.unite(a, b);
  }

  map<int, int> mp[200010];
  for (int i = 0; i < n; i++) {
    mp[uf1.root(i + 1)][uf2.root(i + 1)]++;
  }

  for (int i = 0; i < n; i++) {
    cout << mp[uf1.root(i + 1)][uf2.root(i + 1)];
    if (i < n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
using namespace std;

map<pair<int, int>, int> memo;
int parent[100005][2];

int find(int x, int i) {
  if (parent[x][i] != x)
    parent[x][i] = find(parent[x][i], i);
  return parent[x][i];
}

void merge(int x, int y, int i) {
  x = find(x, i);
  y = find(y, i);
  if (x != y)
    parent[x][i] = y;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 1; i <= N; i++) {
    parent[i][0] = i;
    parent[i][1] = i;
  }
  while (K--) {
    int u, v;
    cin >> u >> v;
    merge(u, v, 0);
  }
  while (L--) {
    int u, v;
    cin >> u >> v;
    merge(u, v, 1);
  }
  for (int i = 1; i <= N; i++)
    memo[{find(i, 0), find(i, 1)}]++;
  for (int i = 1; i <= N; i++)
    cout << memo[{find(i, 0), find(i, 1)}] << ' ';
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
using namespace std;

map<pair<int, int>, int> memo;
int parent[300005][2];

int find(int x, int i) {
  if (parent[x][i] != x)
    parent[x][i] = find(parent[x][i], i);
  return parent[x][i];
}

void merge(int x, int y, int i) {
  x = find(x, i);
  y = find(y, i);
  if (x != y)
    parent[x][i] = y;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, K, L;
  cin >> N >> K >> L;
  for (int i = 1; i <= N; i++) {
    parent[i][0] = i;
    parent[i][1] = i;
  }
  while (K--) {
    int u, v;
    cin >> u >> v;
    merge(u, v, 0);
  }
  while (L--) {
    int u, v;
    cin >> u >> v;
    merge(u, v, 1);
  }
  for (int i = 1; i <= N; i++)
    memo[{find(i, 0), find(i, 1)}]++;
  for (int i = 1; i <= N; i++)
    cout << memo[{find(i, 0), find(i, 1)}] << ' ';
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9 + 7;
const int N = 100000 + 5;
const int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
using namespace std;
int fatherRoad[N], fatherRail[N];
int Find(int father[], int x) {
  if (x != father[x])
    return father[x] = Find(father, father[x]);
  return x;
}
void Union(int father[], int x, int y) {
  x = Find(father, x);
  y = Find(father, y);
  if (x != y)
    father[y] = x;
}
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= n; i++) {
    fatherRoad[i] = i;
    fatherRail[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(fatherRoad, x, y);
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(fatherRail, x, y);
  }

  for (int i = 1; i <= n; i++) {
    Find(fatherRoad, i);
    Find(fatherRail, i);
  }
  map<pair<int, int>, int> mp;
  for (int i = 1; i <= n; i++)
    mp[make_pair(fatherRoad[i], fatherRail[i])]++;
  for (int i = 1; i <= n; i++)
    printf("%d\n", mp[make_pair(fatherRoad[i], fatherRail[i])]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9 + 7;
const int N = 1000000 + 5;
const int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
using namespace std;
int fatherRoad[N], fatherRail[N];
int Find(int father[], int x) {
  if (x != father[x])
    return father[x] = Find(father, father[x]);
  return x;
}
void Union(int father[], int x, int y) {
  x = Find(father, x);
  y = Find(father, y);
  if (x != y)
    father[y] = x;
}
int main() {
  int n, k, l;
  scanf("%d%d%d", &n, &k, &l);
  for (int i = 1; i <= n; i++) {
    fatherRoad[i] = i;
    fatherRail[i] = i;
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(fatherRoad, x, y);
  }
  for (int i = 1; i <= l; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    Union(fatherRail, x, y);
  }

  for (int i = 1; i <= n; i++) {
    Find(fatherRoad, i);
    Find(fatherRail, i);
  }
  map<pair<int, int>, int> mp;
  for (int i = 1; i <= n; i++)
    mp[make_pair(fatherRoad[i], fatherRail[i])]++;
  for (int i = 1; i <= n; i++)
    printf("%d\n", mp[make_pair(fatherRoad[i], fatherRail[i])]);
  return 0;
}
Problem p03857 (C++)

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

const int MAX_N = 1e5 + 5;
int N, K, L;
vector<int> G[2][MAX_N];
int used[2][MAX_N];

void bfs(int s, int cnt, int num) {
  queue<int> q;
  used[num][s] = cnt;
  q.push(s);

  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto v : G[num][u]) {
      if (!used[num][v]) {
        used[num][v] = cnt;
        q.push(v);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    G[0][p].push_back(q);
    G[0][q].push_back(p);
  }
  for (int i = 0; i < L; i++) {
    int p, q;
    cin >> p >> q;
    G[1][p].push_back(q);
    G[1][q].push_back(p);
  }

  int cnt = 1;
  for (int i = 1; i <= N; i++)
    if (!used[0][i])
      bfs(i, cnt++, 0);
  cnt = 1;
  for (int i = 1; i <= N; i++)
    if (!used[1][i])
      bfs(i, cnt++, 1);

  map<P, int> c;
  for (int i = 1; i <= N; i++)
    c[P(used[0][i], used[1][i])]++;

  for (int i = 1; i <= N; i++)
    cout << c[P(used[0][i], used[1][i])] << ' ';
  cout << endl;
  return 0;
}

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

const int MAX_N = 2e5 + 5;
int N, K, L;
vector<int> G[2][MAX_N];
int used[2][MAX_N];

void bfs(int s, int cnt, int num) {
  queue<int> q;
  used[num][s] = cnt;
  q.push(s);

  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto v : G[num][u]) {
      if (!used[num][v]) {
        used[num][v] = cnt;
        q.push(v);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K >> L;
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    G[0][p].push_back(q);
    G[0][q].push_back(p);
  }
  for (int i = 0; i < L; i++) {
    int p, q;
    cin >> p >> q;
    G[1][p].push_back(q);
    G[1][q].push_back(p);
  }

  int cnt = 1;
  for (int i = 1; i <= N; i++)
    if (!used[0][i])
      bfs(i, cnt++, 0);
  cnt = 1;
  for (int i = 1; i <= N; i++)
    if (!used[1][i])
      bfs(i, cnt++, 1);

  map<P, int> c;
  for (int i = 1; i <= N; i++)
    c[P(used[0][i], used[1][i])]++;

  for (int i = 1; i <= N; i++)
    cout << c[P(used[0][i], used[1][i])] << ' ';
  cout << endl;
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int Maxn = 1e5 + 1;
int f1[Maxn], f2[Maxn], n, k, l;
std::map<std::pair<int, int>, int> mp;
int p, q;
int find(int *f, int x) { return f[x] == x ? x : f[x] = find(f, f[x]); }
void merge(int *f, int x, int y) {
  int x1 = find(f, x), y1 = find(f, y);
  if (x1 == y1)
    return;
  f[y1] = x1;
  return;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin >> n >> k >> l;
  for (int i = 1; i <= n; ++i)
    f1[i] = f2[i] = i;
  for (int i = 1; i <= k; ++i) {
    std::cin >> p >> q;
    merge(f1, p, q);
  }
  for (int i = 1; i <= l; ++i) {
    std::cin >> p >> q;
    merge(f2, p, q);
  }
  for (int i = 1; i <= n; ++i)
    find(f1, i), find(f2, i);
  for (int i = 1; i <= n; ++i)
    mp[std::make_pair(f1[i], f2[i])]++;
  for (int i = 1; i <= n; ++i)
    std::cout << mp[std::make_pair(f1[i], f2[i])] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int Maxn = 2e5 + 1;
int f1[Maxn], f2[Maxn], n, k, l;
std::map<std::pair<int, int>, int> mp;
int p, q;
int find(int *f, int x) { return f[x] == x ? x : f[x] = find(f, f[x]); }
void merge(int *f, int x, int y) {
  int x1 = find(f, x), y1 = find(f, y);
  if (x1 == y1)
    return;
  f[y1] = x1;
  return;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin >> n >> k >> l;
  for (int i = 1; i <= n; ++i)
    f1[i] = f2[i] = i;
  for (int i = 1; i <= k; ++i) {
    std::cin >> p >> q;
    merge(f1, p, q);
  }
  for (int i = 1; i <= l; ++i) {
    std::cin >> p >> q;
    merge(f2, p, q);
  }
  for (int i = 1; i <= n; ++i)
    find(f1, i), find(f2, i);
  for (int i = 1; i <= n; ++i)
    mp[std::make_pair(f1[i], f2[i])]++;
  for (int i = 1; i <= n; ++i)
    std::cout << mp[std::make_pair(f1[i], f2[i])] << " ";
  return 0;
}