#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using ull = unsigned long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //template T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; constexpr int MAX = 2100000; class UnionFindUndo { public: vector parent; vector> history; UnionFindUndo(int n) { parent.assign(n, -1); } int root(int x) { if (parent[x] < 0) { return x; } return root(parent[x]); } long long size(int x) { return -(long long)parent[root(x)]; } bool connect(int x, int y) { x = root(x); y = root(y); history.emplace_back(x, parent[x]); history.emplace_back(y, parent[y]); if (x == y) { return false; } if (size(x) < size(y)) { swap(x, y); } parent[x] += parent[y]; parent[y] = x; return true; } void undo() { parent[history.back().first] = history.back().second; history.pop_back(); parent[history.back().first] = history.back().second; history.pop_back(); } void snapshot() { history.clear(); } void rollback() { while (!history.empty()) undo(); } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, D, W; cin >> n >> D >> W; UnionFindUndo uf(n * 2); for (int i = 0; i < D; i++) { int u, v; cin >> u >> v; u--; v--; uf.connect(u, v); } for (int i = 0; i < W; i++) { int u, v; cin >> u >> v; u--; v--; uf.connect(u + n, v + n); } uf.snapshot(); vector> g(n); for (int i = 0; i < n; i++) { g[uf.root(i)].emplace_back(i); } ll res = 0; for (auto& v : g) { if (v.size() == 0) continue; for (auto e : v) { uf.connect(e, e + n); } res += 1LL * v.size() * (((uf.size(v[0]) - v.size() * 2) + v.size()) - 1); uf.rollback(); } cout << res << endl; }