#define STOPIT #include #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt) using lint = long long; auto mt = std::mt19937_64(std::random_device{}()); auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #ifdef STOPIT #define debug(...) 0 #else #define debug(...)\ do {\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha;\ } while (false) #endif template auto make_vector_impl(size_t sz, T t) {return std::vector(sz, t);} template = nullptr> auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));} template = nullptr> auto make_vector(size_t sz) {return std::vector(sz);} template = nullptr> auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector(args...));} template auto& at(T& t, Size_t i) {return t[i];} template auto& at(T& t, Size_t i, Args... args) {return at(t[i], args...);} template < typename Container, typename Value = typename Container::value_type, std::enable_if_t::value, std::nullptr_t> = nullptr> std::istream& operator>> (std::istream& is, Container& v) { for (auto & x : v) { is >> x; } return is; } template < typename Container, typename Value = typename Container::value_type, std::enable_if_t::value, std::nullptr_t> = nullptr > std::ostream& operator<< (std::ostream& os, Container const& v) { os << "{"; for (auto it = v.begin(); it != v.end(); it++) {os << (it != v.begin() ? "," : "") << *it;} return os << "}"; } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& tuple_input_impl(std::istream& os, Tuple& tuple, std::integer_sequence) { (void)std::initializer_list{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::istream& operator>> (std::istream& os, Tuple& tuple) { return tuple_input_impl(os, tuple, std::index_sequence_for()); } template < template < typename ... > class Tuple, typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& tuple_output_impl(std::ostream& os, const Tuple& tuple, std::integer_sequence) { os << "("; (void)std::initializer_list{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; } template < template < typename ... > class Tuple, typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value > std::ostream& operator<< (std::ostream& os, const Tuple& tuple) { return tuple_output_impl(os, tuple, std::index_sequence_for()); } class quick_find { int n; std::vector prt; std::vector> child; public: quick_find (int n) : n(n), prt(n), child(n) { std::iota(prt.begin(), prt.end(), 0); for (int i = 0; i < n; i++) { child[i] = {i}; } } auto collect () const {return prt;} bool is_root (int x) const {return find(x) == x;} int size (int x) const {return child[prt[x]].size();} bool same (int x, int y) const {return find(x) == find(y);} int find (int x) const {return prt[x];} // Returns `true` if x and y are newly connected. bool unite (int x, int y) { if ((x = find(x)) == (y = find(y))) return false; if (size(x) > size(y)) std::swap(x, y); for (auto z : child[x]) { prt[z] = y; child[y].push_back(z); } decltype(child)::value_type{}.swap(child[x]); return true; } }; template < class Value > struct vending_machine{ Value i; vending_machine(Value i) : i(i){} auto issue() { return i++; } auto peek() const { return i; } }; template < class Value > auto make_vending_machine(Value i) { return vending_machine< Value >(i); } template < class Value > class lowest_common_ancestor { struct edge { int to; Value cost; edge(int to, Value cost) : to(to), cost(cost){} }; int n, lg, powlg; std::vector< std::vector< edge > > graph; std::vector< int > depth; std::vector< Value > w_depth; std::vector< std::vector< int > > table; std::vector< int > & tail; public: using cost_type = Value; lowest_common_ancestor(int n) : n(n), lg(std::log2(n)), powlg(std::pow(2, lg)), graph(n), depth(n, 0), w_depth(n, 0), table(lg + 1, std::vector< int >(n)), tail(table.back()) {} void insert (int u, int v, Value cost = 1) { graph[u].emplace_back(v, cost); graph[v].emplace_back(u, cost); } void build (int root = 0) { auto dfs = [&](auto f, int crr, int p) -> void { tail[crr] = p; for (auto const& e : graph[crr]) { if (e.to == p) continue; depth[e.to] = depth[crr] + 1; w_depth[e.to] = w_depth[crr] + e.cost; f(f, e.to, crr); } }; dfs(dfs, root, root); for (int p = lg; p >= 1; p--) { auto & crr = table[p]; auto & nxt = table[p - 1]; for (int i = 0; i < n; i++) { nxt[i] = crr[crr[i]]; } } } auto query(int u, int v) const -> int { if (depth[u] < depth[v]) std::swap(u, v); auto diff = depth[u] - depth[v]; if (diff > 0) { int coeff = powlg; for (auto const & row : table) { if (coeff < diff) { u = row[u]; diff -= coeff; } coeff /= 2; } u = tail[u]; diff--; } assert(diff == 0); assert(depth[u] == depth[v]); if (u == v) return u; for (auto const & row : table) { auto next_u = row[u]; auto next_v = row[v]; if (next_u != next_v) { u = next_u; v = next_v; } } assert(u != v); u = tail[u], v = tail[v], assert(u == v); return u; } auto row_distance (int u, int v) const -> int { return depth[u] + depth[v] - 2 * depth[query(u, v)]; } auto weighted_distance (int u, int v) const -> Value { return w_depth[u] + w_depth[v] - 2 * w_depth[query(u, v)]; } }; int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n, m, q; std::cin >> n >> m >> q; auto graph = make_vector< 2, int >(n, 0); auto qf = quick_find(n); loop(m) { int u, v; std::cin >> u >> v; u--, v--; graph[u].emplace_back(v); graph[v].emplace_back(u); qf.unite(u, v); } vending_machine< int > vm{0}; std::vector< int > cmp(n); rep(i, 0, n) { if (qf.is_root(i)) { cmp[i] = vm.issue(); } } rep(i, 0, n) { cmp[i] = cmp[qf.find(i)]; } int sz = vm.peek(); std::vector< vending_machine< int > > id_seq(sz, {0}); std::vector< int > id(n); rep(i, 0, n) { id[i] = id_seq[cmp[i]].issue(); } auto internal = make_vector< 2, std::pair< int, int > >(sz, 0); auto external = make_vector< 2, int >(sz, 0); loop(q) { int u, v; std::cin >> u >> v; u--, v--; int x = cmp[u], y = cmp[v]; int i = id[u], j = id[v]; if (x == y) { internal[x].emplace_back(i, j); } else { external[x].emplace_back(i); external[y].emplace_back(j); } } auto sgraph = make_vector< 3, int >(sz, 0, 0); rep(i, 0, sz) sgraph[i].resize(id_seq[i].peek()); rep(i, 0, n) { for (auto j : graph[i]) { int x = cmp[i], y = cmp[j]; assert(x == y); at(sgraph, x, id[i]).emplace_back(id[j]); } } auto cal = [&] (auto&& internal, auto&& external, auto&& graph, auto n) -> long long { debug(n); // debug(internal); // debug(external); // debug(graph); auto lca = lowest_common_ancestor< int >(n); rep(i, 0, n) { for (auto j : graph[i]) { if (i > j) continue; lca.insert(i, j); } } lca.build(); lint ans = 0; for (auto pair : internal) { int u, v; std::tie(u, v) = pair; lint tmp = lca.row_distance(u, v); ans += tmp; } int root = rand(0, n - 1); std::vector< lint > sz(n, 0), dp(n); for (auto i : external) sz[i]++; auto dfs = [&] (auto&& dfs, auto crr, auto prv) -> void { for (auto nxt : graph[crr]) { if (nxt == prv) continue; dfs(dfs, nxt, crr); sz[crr] += sz[nxt]; dp[crr] += dp[nxt] + sz[nxt]; } }; dfs(dfs, root, root); debug(sz); debug(dp); auto efs = [&](auto&& efs, auto crr, auto prv) -> void { for (auto nxt : graph[crr]) { if (nxt == prv) continue; dp[nxt] = dp[crr] + external.size() - 2 * sz[nxt]; efs(efs, nxt, crr); } }; efs(efs, root, root); debug(dp); ans += *std::min_element(all(dp)); return ans; }; long long ans = 0; rep(i, 0, sz) { ans += cal(internal[i], external[i], sgraph[i], id_seq[i].peek()); } std::cout << ans << std::endl; return 0; }