Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int N = 100005;
int par1[N], par2[N];
int comp1[N], comp2[N];
int getparent1(int x) {
  if (par1[x] == x)
    return x;
  int p1 = getparent1(par1[x]);
  return par1[x] = p1;
}
int getparent2(int x) {
  if (par2[x] == x)
    return x;
  int p2 = getparent2(par2[x]);
  return par2[x] = p2;
}
inline void unite1(int p1, int p2) {
  if (p1 == p2)
    return;
  if (comp1[p1] < comp1[p2])
    unite1(p2, p1);
  else {
    comp1[p1] += comp1[p2];
    par1[p2] = p1;
  }
}
inline void unite2(int p1, int p2) {
  if (p1 == p2)
    return;
  if (comp2[p1] < comp2[p2])
    unite2(p2, p1);
  else {
    comp2[p1] += comp2[p2];
    par2[p2] = p1;
  }
}
vector<int> els[N];
int ans[N], cnt[N], vis[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    par1[i] = i;
    comp1[i] = 1;
    par2[i] = i;
    comp2[i] = 1;
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    unite1(getparent1(x), getparent1(y));
  }
  for (int i = 0; i < l; i++) {
    cin >> x >> y;
    unite2(getparent2(x), getparent2(y));
  }
  for (int i = 1; i <= n; i++) {
    int p = getparent1(i);
    els[p].push_back(i);
  }
  // for(int i=1; i<=n; i++)
  // {
  // 	if(els[i].empty())
  // 		continue;
  // 	cout<<i<<":";
  // 	for(auto x: els[i])
  // 		cout<<x<<",";
  // 	cout<<endl;
  // }
  for (int i = 1; i <= n; i++) {
    if (els[i].empty())
      continue;
    for (auto x : els[i]) {
      int p = getparent2(x);
      if (vis[p] != i) {
        vis[p] = i;
        cnt[p] = 0;
      }
      cnt[p]++;
    }
    for (auto x : els[i])
      ans[x] = cnt[getparent2(x)];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << " ";
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int N = 200005;
int par1[N], par2[N];
int comp1[N], comp2[N];
int getparent1(int x) {
  if (par1[x] == x)
    return x;
  int p1 = getparent1(par1[x]);
  return par1[x] = p1;
}
int getparent2(int x) {
  if (par2[x] == x)
    return x;
  int p2 = getparent2(par2[x]);
  return par2[x] = p2;
}
inline void unite1(int p1, int p2) {
  if (p1 == p2)
    return;
  if (comp1[p1] < comp1[p2])
    unite1(p2, p1);
  else {
    comp1[p1] += comp1[p2];
    par1[p2] = p1;
  }
}
inline void unite2(int p1, int p2) {
  if (p1 == p2)
    return;
  if (comp2[p1] < comp2[p2])
    unite2(p2, p1);
  else {
    comp2[p1] += comp2[p2];
    par2[p2] = p1;
  }
}
vector<int> els[N];
int ans[N], cnt[N], vis[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, l, x, y;
  cin >> n >> k >> l;
  for (int i = 1; i <= n; i++) {
    par1[i] = i;
    comp1[i] = 1;
    par2[i] = i;
    comp2[i] = 1;
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    unite1(getparent1(x), getparent1(y));
  }
  for (int i = 0; i < l; i++) {
    cin >> x >> y;
    unite2(getparent2(x), getparent2(y));
  }
  for (int i = 1; i <= n; i++) {
    int p = getparent1(i);
    els[p].push_back(i);
  }
  // for(int i=1; i<=n; i++)
  // {
  // 	if(els[i].empty())
  // 		continue;
  // 	cout<<i<<":";
  // 	for(auto x: els[i])
  // 		cout<<x<<",";
  // 	cout<<endl;
  // }
  for (int i = 1; i <= n; i++) {
    if (els[i].empty())
      continue;
    for (auto x : els[i]) {
      int p = getparent2(x);
      if (vis[p] != i) {
        vis[p] = i;
        cnt[p] = 0;
      }
      cnt[p]++;
    }
    for (auto x : els[i])
      ans[x] = cnt[getparent2(x)];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << " ";
}