#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 #include #include #include #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; using size_type = ssize_t; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template struct multi_dim_array { using type = std::array::type, Head>; }; template struct multi_dim_array { using type = std::array; }; template using mdarray = typename multi_dim_array::type; template void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable::value) { t = f(args...); } else { for (size_type i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } template vec make_v(size_type sz) { return vec(sz); } template auto make_v(size_type hs, Tail&&... ts) { auto v = std::move(make_v(std::forward(ts)...)); return vec(hs, v); } namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } template T ceil_pow2(T bound) { T ret = 1; while (ret < bound) ret *= 2; return ret; } template T ceil_div(T a, T b) { return a / b + !!(a % b); } namespace graph { using Node = ll; using Weight = ll; using Edge = std::pair; template struct Graph : public vvec { using vvec::vvec; void add_edge(Node f, Node t, Weight w = 1) { (*this)[f].emplace_back(t, w); if (!Directed) (*this)[t].emplace_back(f, w); } Graph build_inv() const { Graph ret(this->size()); for (Node i = 0; i < this->size(); i++) { for (const Edge &e : (*this)[i]) { Node j; Weight w; std::tie(j, w) = e; if (!Directed && j < i) continue; ret.add_edge(j, i, w); } } return ret; } }; template class dst_iterator { Iterator ite; public: dst_iterator(Iterator ite) : ite(ite) { } bool operator ==(const dst_iterator &oth) const { return ite == oth.ite; } bool operator !=(const dst_iterator &oth) const { return !(*this == oth); } bool operator <(const dst_iterator &oth) const { return ite < oth.ite; } bool operator >(const dst_iterator &oth) const { return ite > oth.ite; } bool operator <=(const dst_iterator &oth) const { return ite <= oth.ite; } bool operator >=(const dst_iterator &oth) const { return ite >= oth.ite; } const Node& operator *() { return ite->first; } const Node& operator *() const { return ite->first; } dst_iterator operator ++() { ++ite; return ite; } }; class dst_iteration { using ite_type = vec::const_iterator; const vec &edges; public: dst_iteration(const vec &edges) : edges(edges) { } auto begin() const { return dst_iterator(edges.cbegin()); } auto end() const { return dst_iterator(edges.cend()); } }; class dst_reverse_iteration { using ite_type = vec::const_reverse_iterator; const vec &edges; public: dst_reverse_iteration(const vec &edges) : edges(edges) { } auto begin() const { return dst_iterator(edges.crbegin()); } auto end() const { return dst_iterator(edges.crend()); } }; dst_iteration dst(const vec &edges) { return dst_iteration(edges); } dst_reverse_iteration rdst(const vec &edges) { return dst_reverse_iteration(edges); } } struct DisjointSetUnion { vvec set; vec id; DisjointSetUnion(ll n) : set(n), id(n) { for (ll i = 0; i < n; i++) { set[i].push_back(i); id[i] = i; } } void unit(ll x, ll y) { ll px = id[x], py = id[y]; if (px == py) return; if (set[px].size() < set[py].size()) std::swap(px, py); for (ll e : set[py]) { set[px].push_back(e); id[e] = px; } set[py].clear(); } }; int main() { ll n, d, w; std::cin >> n >> d >> w; vec e1(d), e2(w); auto scan = [&](vec &v) { for (auto &[ u, v ] : v) { std::cin >> u >> v; u--; v--; } }; scan(e1); scan(e2); graph::Graph g(n); for (auto [ u, v ] : e1) g.add_edge(u, v); DisjointSetUnion dsu(n); for (auto [ u, v ] : e2) dsu.unit(u, v); ll ans = 0; vec used(n, false); vec used_id(n, -1); for (ll i = 0; i < n; i++) { if (used[i]) continue; std::queue que; que.push(i); used[i] = true; ll cnt1 = 0, cnt2 = 0; while (que.size()) { ll c = que.front(); cnt1++; que.pop(); ll id = dsu.id[c]; if (used_id[id] != i) { used_id[id] = i; cnt2 += dsu.set[id].size(); } for (ll nxt : graph::dst(g[c])) if (!used[nxt]) { used[nxt] = true; que.push(nxt); } } ans += cnt1 * cnt2; } ans -= n; std::cout << ans << "\n"; return 0; }