Problem p03857 (C++)

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

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define FILE "file"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int INF = numeric_limits<int>::max();
const ll LLINF = numeric_limits<ll>::max();
const ull ULLINF = numeric_limits<ull>::max();
const double PI = acos(-1.0);

vector<int> g1[200010], g2[200010];
bool used[200010];
int where1[200010], where2[200010];
int numComp = 0;
vector<int> comp1[200010], comp2[200010];
bool flag1[200010], flag2[200010];
int ans[200010];
bool found[200010];

void dfs1(int v) {
  used[v] = 1;
  for (int to : g1[v])
    if (!used[to])
      dfs1(to);
  where1[v] = numComp;
  comp1[numComp].pb(v);
}

void dfs2(int v) {
  used[v] = 1;
  for (int to : g2[v])
    if (!used[to])
      dfs2(to);
  where2[v] = numComp;
  comp2[numComp].pb(v);
}

int main() {
  //    freopen(FILE".in", "r", stdin);
  //    freopen(FILE".out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g1[a].pb(b);
    g1[b].pb(a);
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g2[a].pb(b);
    g2[b].pb(a);
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      dfs1(i);
      numComp++;
    }
  }
  for (int i = 0; i < numComp; i++)
    sort(all(comp1[i]));
  memset(used, 0, sizeof(used));
  numComp = 0;
  for (int i = 0; i < n; i++) {
    ans[i] = 1;
    if (!used[i]) {
      dfs2(i);
      numComp++;
    }
  }
  for (int i = 0; i < numComp; i++)
    sort(all(comp2[i]));
  for (int i = 0; i < n; i++) {
    if (found[i])
      continue;
    vector<int> intersection;
    for (int j : comp1[where1[i]]) {
      if (binary_search(all(comp2[where2[i]]), j))
        intersection.pb(j);
    }
    for (int j : intersection) {
      ans[j] = intersection.size();
      found[j] = 1;
    }
    //        cout << i << ": { ";
    //        for(int j: comp1[where1[i]])
    //            cout << j << " ";
    //        cout << "} {";
    //        for(int j: comp2[where2[i]])
    //            cout << j << " ";
    //        cout << "}";
    //        cout << endl;
    //        for(int j: intersection)
    //            cout << j << " ";
    //        cout << endl;
    flag1[where1[i]] = 1;
    flag2[where2[i]] = 1;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  return 0;
}


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

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define FILE "file"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int INF = numeric_limits<int>::max();
const ll LLINF = numeric_limits<ll>::max();
const ull ULLINF = numeric_limits<ull>::max();
const double PI = acos(-1.0);

vector<int> g1[200010], g2[200010];
bool used[200010];
int where1[200010], where2[200010];
int numComp = 0;
vector<int> comp1[200010], comp2[200010];
bool flag1[200010], flag2[200010];
int ans[200010];
bool found[200010];

void dfs1(int v) {
  used[v] = 1;
  for (int to : g1[v])
    if (!used[to])
      dfs1(to);
  where1[v] = numComp;
  comp1[numComp].pb(v);
}

void dfs2(int v) {
  used[v] = 1;
  for (int to : g2[v])
    if (!used[to])
      dfs2(to);
  where2[v] = numComp;
  comp2[numComp].pb(v);
}

int main() {
  //    freopen(FILE".in", "r", stdin);
  //    freopen(FILE".out", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k, l;
  cin >> n >> k >> l;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g1[a].pb(b);
    g1[b].pb(a);
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g2[a].pb(b);
    g2[b].pb(a);
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      dfs1(i);
      numComp++;
    }
  }
  for (int i = 0; i < numComp; i++)
    sort(all(comp1[i]));
  memset(used, 0, sizeof(used));
  numComp = 0;
  for (int i = 0; i < n; i++) {
    ans[i] = 1;
    if (!used[i]) {
      dfs2(i);
      numComp++;
    }
  }
  for (int i = 0; i < numComp; i++)
    sort(all(comp2[i]));
  for (int i = 0; i < numComp; i++) {
    map<int, vector<int>> smallc;
    for (int j : comp2[i])
      smallc[where1[j]].pb(j);
    for (auto k : smallc)
      for (int j : k.second)
        ans[j] = k.second.size();
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  return 0;
}