#include using namespace std; template bool cmin(T &a, U b) { return a > b && (a = b, true); } template bool cmax(T &a, U b) { return a < b && (a = b, true); } class UnionFind { private: long N; vector PS; public: UnionFind() : N(0) {} UnionFind(long n) : N(n), PS(n, -1) {} bool unite(long a, long b) { long x = root(a), y = root(b); if (x == y) return false; if (-PS.at(x) < -PS.at(y)) swap(x, y); PS.at(x) += PS.at(y); PS.at(y) = x; return true; } bool same(long a, long b) { return root(a) == root(b); } long root(long a) { if (PS.at(a) < 0) return a; return PS.at(a) = root(PS.at(a)); } long size(long a) { return -PS.at(root(a)); } vector> groups() { vector B(N), S(N); for (long i = 0; i < N; i++) { B.at(i) = root(i); S.at(B.at(i))++; } vector> ret(N); for (long i = 0; i < N; i++) { ret.at(i).reserve(S.at(i)); } for (long i = 0; i < N; i++) { ret.at(B.at(i)).push_back(i); } auto f = [&](const vector &v) { return v.empty(); }; ret.erase(remove_if(ret.begin(), ret.end(), f), ret.end()); return ret; } }; signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); long N, D, W; cin >> N >> D >> W; vector> AB(D); for (long i = 0; i < D; i++) cin >> AB.at(i).first >> AB.at(i).second; vector> CD(W); for (long i = 0; i < W; i++) cin >> CD.at(i).first >> CD.at(i).second; UnionFind UF(N); UnionFind UF2(N); for (auto [a, b] : AB) { a--, b--; UF.unite(a, b); } vector> V(N); for (long i = 0; i < N; i++) { V.at(UF.root(i)).insert(i); } for (auto [a, b] : CD) { a--, b--; UF2.unite(a, b); } vector> V2(N); for (long i = 0; i < N; i++) { V2.at(UF2.root(i)).insert(i); } long ans = 0; vector tmp(N, -1); for (long i = 0; i < N; i++) { if (tmp.at(UF.root(i)) != -1) { ans += tmp.at(UF.root(i)); continue; } set SE; vector tmp2(N); for (auto a : V.at(UF.root(i))) { if (tmp2.at(UF2.root(a))) continue; for (auto b : V2.at(UF2.root(a))) SE.insert(b); tmp2.at(UF2.root(a)) = 1; } ans += SE.size() - 1; tmp.at(UF.root(i)) = SE.size() - 1; } cout << ans << "\n"; }