module simple;import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, core.stdc.string; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto Q = s[2]; auto G = new int[][](N+1); auto uf = new UnionFind(N); foreach (_; 0..M) { s = readln.split.map!(to!int); G[s[0]-1] ~= s[1]-1; G[s[1]-1] ~= s[0]-1; uf.unite(s[0]-1, s[1]-1); } foreach (i; 0..N) if (uf.table[i] < 0) G[N] ~= i, G[i] ~= N; auto lca = new LowestCommonAncestor(G, N); long ans = 0; auto V = new long[](N); auto subsum = new long[](N); auto subnum = new long[](N); auto dp = new long[](N); while (Q--) { s = readln.split.map!(to!int); auto a = s[0] - 1; auto b = s[1] - 1; if (uf.find(a) == uf.find(b)) { ans += lca.dist(a, b); } else { V[a] += 1; V[b] += 1; } } void dfs1(int n, int p) { subnum[n] = V[n]; foreach (m; G[n]) if (m != p && m != N) { dfs1(m, n); subsum[n] += subnum[m] + subsum[m]; subnum[n] += subnum[m]; } } void dfs2(int n, int p, long allnum) { if (p == -1) { dp[n] = subsum[n]; } foreach (m; G[n]) if (m != p && m != N) { dp[m] = dp[n] - (subnum[m]) + (allnum - subnum[m]); dfs2(m, n, allnum); } } foreach (i; 0..N) if (uf.find(i) == i) { dfs1(i, -1); long allsum = 0; foreach (j; uf.group[i]) allsum += V[j]; dfs2(i, -1, allsum); long mn = 1L << 59; foreach (j; uf.group[i]) mn = min(mn, dp[j]); ans += mn; } ans.writeln; } class UnionFind { import std.algorithm : swap; int n; int[] table; int[][] group; this(int n) { this.n = n; table = new int[](n); table[] = -1; group = new int[][](n); foreach (i; 0..n) group[i] = [i]; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); group[x] ~= group[y]; group[y] = []; table[x] += table[y]; table[y] = x; } bool same(int x, int y) { return find(x) == find(y); } } // This is O(log(n)) implementation with doubling. class LowestCommonAncestor { import std.algorithm : swap; import std.conv : to; import std.typecons : Tuple, tuple; import core.bitop : bsr; int n, root, lgn; int[][] graph; int[] depth; int[][] dp; this(const int[][] graph, int root) { n = graph.length.to!int; this.root = root; this.graph = new int[][](n); foreach (i; 0..n) this.graph[i] = graph[i].dup; lgn = bsr(n) + 3; depth = new int[](n); dp = new int[][](n, lgn); construct; } int lca(int a, int b) { if (depth[a] < depth[b]) swap(a, b); int diff = depth[a] - depth[b]; foreach_reverse (i; 0..lgn) if (diff & (1<= 0) { auto u = stack[sp][0]; auto p = stack[sp][1]; auto d = stack[sp][2]; sp -= 1; dp[u][0] = p; depth[u] = d; foreach (v; graph[u]) if (v != p) stack[++sp] = tuple(v, u, d+1); } foreach (k; 0..lgn-1) foreach (i; 0..n) dp[i][k+1] = (dp[i][k] == -1) ? -1 : dp[dp[i][k]][k]; } }