Language: C++

#include <map>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i,y) for(int i=0;i<(int)(y);i++)

int N,K,L;

class UnionFind {
  vector<int> p;  // rank
public:
  UnionFind (int n) : p(n, -1) {}
  int root(int x) {
    return p[x] < 0 ? x : p[x] = root(p[x]);
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  bool unite(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return false;
    if (p[y] < p[x]) swap(x, y);
    if (p[x] == p[y]) --p[x];
    p[y] = x;
    return true;
  }
};

multimap<int, int> mp1;
multimap<int, int> mp2;
//int memo[200010][200010];
map<pair<int,int>,int> memo;

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> N >> K >> L;
  UnionFind uf1(N), uf2(N);
  REP(i,K) {
    int p,q; cin>>p>>q; p--,q--;
    uf1.unite(p,q);
  }
  REP(i,L) {
    int r,s; cin>>r>>s; r--,s--;
    uf2.unite(r,s);
  }
  REP(i,N) {
    mp1.emplace(uf1.root(i), i);
    mp2.emplace(uf2.root(i), i);
  }
  REP(i,N) {
    int r1 = uf1.root(i), r2 = uf2.root(i);
    if (memo[r1][r2])
      cout << memo[r1][r2] << " ";
    else {
      int cnt=0;
      auto p1 = mp1.equal_range(r1);
      auto p2 = mp2.equal_range(r2);
      auto it1 = p1.first;
      auto it2 = p2.first;
      while (it1!=p1.second && it2!=p2.second) {
        int s = it1->second;
        int t = it2->second;
        if (s==t) cnt++, it2++;
        if (s<t)  it1++;
        if (s>t)  it2++;
      }
      cout << cnt << " ";
    }
  }
  cout << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:54:13: error: no match for 'operator[]' (operand types are 'std::map<std::pair<int, int>, int>' and 'int')
   54 |     if (memo[r1][r2])
      |             ^
In file included from /usr/include/c++/14/map:63,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_map.h:504:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:504:34: note:   no known conversion for argument 1 from 'int' to 'const std::map<std::pair<int, int>, int>::key_type&' {aka 'const std::pair<int, int>&'}
  504 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_map.h:524:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](key_type&&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  524 |       operator[](key_type&& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:524:29: note:   no known conversion for argument 1 from 'int' to 'std::map<std::pair<int, int>, int>::key_type&&' {aka 'std::pair<int, int>&&'}
  524 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~
a.cc:55:19: error: no match for 'operator[]' (operand types are 'std::map<std::pair<int, int>, int>' and 'int')
   55 |       cout << memo[r1][r2] << " ";
      |                   ^
/usr/include/c++/14/bits/stl_map.h:504:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:504:34: note:   no known conversion for argument 1 from 'int' to 'const std::map<std::pair<int, int>, int>::key_type&' {aka 'const std::pair<int, int>&'}
  504 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_map.h:524:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](key_type&&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  524 |       operator[](key_type&& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:524:29: note:   no known conversion for argument 1 from 'int' to 'std::map<std::pair<int, int>, int>::key_type&&' {aka 'std::pair<int, int>&&'}
  524 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~