Language: C++

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

// template {{{
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; })

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

template<typename T> void ckmin(T& a, const T& b) { a = min(a, b); }
template<typename T> void ckmax(T& a, const T& b) { a = max(a, b); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

namespace __input {
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }

    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace __input;

namespace __output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const deque<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T, bool pretty = true> void prContain(const T& x) {
        if (pretty) pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?pretty?", ":" ":"",a), fst = 0;
        if (pretty) pr("}");
    }
    template<class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const deque<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps();
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...);
    }
}
using namespace __output;

#define TRACE(x) x
#define __pn(x) pr(#x, " = ")
#define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush

namespace __algorithm {
    template<typename T> void dedup(vector<T>& v) {
        sort(all(v)); v.erase(unique(all(v)), v.end());
    }
    template<typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) {
        auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end();
    }
    template<typename T> size_t index(vector<T>& v, const T& x) {
        auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin();
    }
    template<typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0R (i, sz(v)) r[i+1] = op(r[i], v[i]); return r;
    }
    template<typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0Rd (i, sz(v)) r[i] = op(v[i], r[i+1]); return r;
    }
}
using namespace __algorithm;

struct monostate {
    friend istream& operator>>(istream& is, const __attribute__((unused))monostate& ms) { return is; }
    friend ostream& operator<<(ostream& os, const __attribute__((unused))monostate& ms) { return os; }
} ms;

template<typename W=monostate> struct wedge {
    int u, v, i; W w;
    wedge<W>(int _u=-1, int _v=-1, int _i=-1) : u(_u), v(_v), i(_i) {}
    int operator[](int loc) const { return u ^ v ^ loc; }
    friend void re(wedge& e) { re(e.u, e.v, e.w); --e.u, --e.v; }
    friend void pr(const wedge& e) { pr(e.u, "<-", e.w, "->", e.v); }
};

namespace __io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0);
        cout << fixed << setprecision(15);
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
    }
}
using namespace __io;
// }}}

// union_find {{{
template<typename V = vi, typename E = monostate>
struct union_find {
    struct node {
        int par, rnk, size; V state;
        node(int id = 0) : par(id), rnk(0), size(1), state({id}) {}
        void merge(node& o, E& e) {
            if (rnk == o.rnk) rnk++;
            size += o.size;
            state.insert(state.end(), all(o.state));
        }
    };

    vector<node> uf;
    union_find(int N = 0) : uf(N) {
        for (int i = 0; i < N; i++)
            uf[i] = node(i);
    }

    int rep(int i) {
        if (i != uf[i].par)
            uf[i].par = rep(uf[i].par);
        return uf[i].par;
    }

    bool unio(int a, int b, E& e = ms) {
        a = rep(a), b = rep(b);
        if (a == b) return false;
        if (uf[a].rnk < uf[b].rnk) swap(a, b);
        uf[a].merge(uf[b], e);
        uf[b].par = a;
        return true;
    }

    node& operator[](int i) { return uf[rep(i)]; }
};
// }}}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

struct sp64_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = splitmix64(((uint64_t) new char | 1)
                * chrono::steady_clock::now().time_since_epoch().count());
        return splitmix64(x ^ FIXED_RANDOM);
    }
};
template<class K> struct sp64_pair_hash {
    size_t operator()(const pair<K, K>& x) const {
        static_assert(sizeof(K) <= 4);
        static sp64_hash sp64;
        return sp64(((uint64_t) x.first << 32) ^ x.second);
    }
};

template<class K, class V> using umap = gp_hash_table<K, V, sp64_hash>;

int main() {
    setIO();

    int N, K, L; re(N, K, L);

    union_find<> roads(N);
    F0R (k, K) {
        int p, q; re(p, q); --p, --q;
        roads.unio(p, q);
    }

    union_find<> rails(N);
    F0R (l, L) {
        int r, s; re(r, s); --r, --s;
        rails.unio(r, s);
    }

    gp_hash_table<pii, int, sp64_pair_hash<int>> ct;
    F0R (i, N) ct[{ roads.rep(i), rails.rep(i) }]++;

    F0R (i, N) {
        if (i) pr(" ");
        pr(ct[{ roads.rep(i), rails.rep(i) }]);
    }
    ps();

    // did you check N=1? did you mix up N,M?
    return 0;
}



Compiler output:
a.cc:134:21: error: reference to 'monostate' is ambiguous
  134 | template<typename W=monostate> struct wedge {
      |                     ^~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1237:10: note: candidates are: 'struct std::monostate'
 1237 |   struct monostate { };
      |          ^~~~~~~~~
a.cc:129:8: note:                 'struct monostate'
  129 | struct monostate {
      |        ^~~~~~~~~
a.cc:155:40: error: reference to 'monostate' is ambiguous
  155 | template<typename V = vi, typename E = monostate>
      |                                        ^~~~~~~~~
/usr/include/c++/14/variant:1237:10: note: candidates are: 'struct std::monostate'
 1237 |   struct monostate { };
      |          ^~~~~~~~~
a.cc:129:8: note:                 'struct monostate'
  129 | struct monostate {
      |        ^~~~~~~~~
a.cc: In function 'int main()':
a.cc:223:16: error: template argument 2 is invalid
  223 |     union_find<> roads(N);
      |                ^
a.cc:226:15: error: request for member 'unio' in 'roads', which is of non-class type 'int'
  226 |         roads.unio(p, q);
      |               ^~~~
a.cc:229:16: error: template argument 2 is invalid
  229 |     union_find<> rails(N);
      |                ^
a.cc:232:15: error: request for member 'unio' in 'rails', which is of non-class type 'int'
  232 |         rails.unio(r, s);
      |               ^~~~
a.cc:236:27: error: request for member 'rep' in 'roads', which is of non-class type 'int'
  236 |     F0R (i, N) ct[{ roads.rep(i), rails.rep(i) }]++;
      |                           ^~~
a.cc:236:41: error: request for member 'rep' in 'rails', which is of non-class type 'int'
  236 |     F0R (i, N) ct[{ roads.rep(i), rails.rep(i) }]++;
      |                                         ^~~
a.cc:236:18: error: no match for 'operator[]' (operand types are '__gnu_pbds::gp_hash_table<std::pair<int, int>, int, sp64_pair_hash<int> >' and '<brace-enclosed initializer list>')
  236 |     F0R (i, N) ct[{ roads.rep(i), rails.rep(i) }]++;
      |                  ^
In file included from /usr/include/c++/14/ext/pb_ds/detail/container_base_dispatch.hpp:72,
                 from /usr/include/c++/14/ext/pb_ds/assoc_container.hpp:48,
                 from a.cc:192:
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:334:7: note: candidate: '__gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::mapped_reference __gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::operator[](key_const_reference) [with Key = std::pair<int, int>; Mapped = int; Hash_Fn = sp64_pair_hash<int>; Eq_Fn = std::equal_to<std::pair<int, int> >; _Alloc = std::allocator<char>; bool Store_Hash = false; Comb_Probe_Fn = __gnu_pbds::direct_mask_range_hashing<>; Probe_Fn = __gnu_pbds::linear_probe_fn<long unsigned int>; Resize_Policy = __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int>; mapped_reference = int&; key_const_reference = const std::pair<int, int>&]'
  334 |       operator[](key_const_reference r_key)
      |       ^~~~~~~~
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:334:38: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to '__gnu_pbds::detail::gp_ht_map<std::pair<int, int>, int, sp64_pair_hash<int>, std::equal_to<std::pair<int, int> >, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >::key_const_reference' {aka 'const std::pair<int, int>&'}
  334 |       operator[](key_const_reference r_key)
      |                  ~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:240:23: error: request for member 'rep' in 'roads', which is of non-class type 'int'
  240 |         pr(ct[{ roads.rep(i), rails.rep(i) }]);
      |                       ^~~
a.cc:240:37: error: request for member 'rep' in 'rails', which is of non-class type 'int'
  240 |         pr(ct[{ roads.rep(i), rails.rep(i) }]);
      |                                     ^~~
a.cc:240:14: error: no match for 'operator[]' (operand types are '__gnu_pbds::gp_hash_table<std::pair<int, int>, int, sp64_pair_hash<int> >' and '<brace-enclosed initializer list>')
  240 |         pr(ct[{ roads.rep(i), rails.rep(i) }]);
      |              ^
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:334:7: note: candidate: '__gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::mapped_reference __gnu_pbds::detail::gp_ht_map<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>::operator[](key_const_reference) [with Key = std::pair<int, int>; Mapped = int; Hash_Fn = sp64_pair_hash<int>; Eq_Fn = std::equal_to<std::pair<int, int> >; _Alloc = std::allocator<char>; bool Store_Hash = false; Comb_Probe_Fn = __gnu_pbds::direct_mask_range_hashing<>; Probe_Fn = __gnu_pbds::linear_probe_fn<long unsigned int>; Resize_Policy = __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int>; mapped_reference = int&; key_const_reference = const std::pair<int, int>&]'
  334 |       operator[](key_const_reference r_key)
      |       ^~~~~~~~
/usr/include/c++/14/ext/pb_ds/detail/gp_hash_table_map_/gp_ht_map_.hpp:334:38: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to '__gnu_pbds::detail::gp_ht_map<std::pair<int, int>, int, sp64_pair_hash<int>, std::equal_to<std::pair<int, int> >, std::allocator<char>, false, __gnu_pbds::direct_mask_range_hashing<>, __gnu_pbds::linear_probe_fn<long unsigned int>, __gnu_pbds::hash_standard_resize_policy<__gnu_pbds::hash_exponential_size_policy<>, __gnu_pbds::hash_load_check_resize_trigger<>, false, long unsigned int> >::key_const_reference' {aka 'const std::pair<int, int>&'}
  334 |       operator[](key_const_reference r_key)
      |                  ~~~~~~~~~~~~~~~~~~~~^~~~~