/** * @FileName a.cpp * @Author kanpurin * @Created 2020.09.29 20:23:41 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; struct LowestCommonAncestor { private: int n; int log; vector> parent; vector dep; vector> G; void dfs(const vector> &G, int v, int p, int d) { parent[0][v] = p; dep[v] = d; for (int to : G[v]) { if (to != p) dfs(G, to, v, d + 1); } } public: LowestCommonAncestor(int n) : n(n) { G.resize(n); } void add_edge(int from, int to) { G[from].push_back(to); G[to].push_back(from); } void build(int root = 0) { log = log2(n) + 1; parent.resize(log, vector(n)); dep.resize(n); dfs(G, root, -1, 0); for (int k = 0; k + 1 < log; k++) { for (int v = 0; v < G.size(); v++) { if (parent[k][v] < 0) { parent[k + 1][v] = -1; } else { parent[k + 1][v] = parent[k][parent[k][v]]; } } } } int depth(int v) { return dep[v]; } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int k = 0; k < log; k++) if ((dep[v] - dep[u]) >> k & 1) v = parent[k][v]; if (u == v) return u; for (int k = log - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; class UnionFind { private: vector par; public: UnionFind(int n) { par.resize(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (size(rx) < size(ry)) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } int size(int x) { return -par[root(x)]; } }; using T = ll; int n; vector> tree; vector dist; vector dist2; vector ans; vector cost; vector used; void dfs1(int v, int p = -1) { used[v] = true; T res = 0, res2 = cost[v]; for (auto &u : tree[v]) { if (u == p) continue; dfs1(u, v); res += dist[u] + dist2[u]; res2 += dist2[u]; } dist2[v] = res2; dist[v] = res; } T dfs2(int v, T d_par, T d_par2, int p = -1) { used[v] = true; ans[v] = d_par + dist[v] + d_par2; T res = ans[v]; auto f = [](T a, T b) { return a + b; }; T e = 0; vector lf(tree[v].size(), e), ri(tree[v].size(), e); vector lf2(tree[v].size(), e), ri2(tree[v].size(), e); for (int i = 1; i < tree[v].size(); i++) { if (tree[v][i - 1] == p) lf[i] = f(d_par, lf[i - 1]); else lf[i] = f(dist[tree[v][i - 1]], lf[i - 1]); if (tree[v][i - 1] == p) lf2[i] = f(d_par2, lf2[i - 1]); else lf2[i] = f(dist2[tree[v][i - 1]], lf2[i - 1]); } for (int i = tree[v].size() - 2; i >= 0; i--) { if (tree[v][i + 1] == p) ri[i] = f(d_par, ri[i + 1]); else ri[i] = f(dist[tree[v][i + 1]], ri[i + 1]); if (tree[v][i + 1] == p) ri2[i] = f(d_par2, ri2[i + 1]); else ri2[i] = f(dist2[tree[v][i + 1]], ri2[i + 1]); } for (int i = 0; i < tree[v].size(); i++) { if (tree[v][i] == p) continue; res = min(res,dfs2(tree[v][i], f(lf[i], ri[i]) + f(lf2[i], ri2[i]), f(lf2[i], ri2[i]) + cost[v], v)); } return res; } int main(){ int m,q;cin >> n >> m >> q; tree.resize(n); dist.resize(n); dist2.resize(n); ans.resize(n); cost.resize(n,0); UnionFind uf(n),uf2(n); LowestCommonAncestor lca(n); ll sum = 0; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; tree[u].push_back(v); tree[v].push_back(u); uf.unite(u,v); uf2.unite(u,v); lca.add_edge(u,v); } for (int i = 1; i < n; i++) { if (!uf2.same(0,i)) uf2.unite(0,i),lca.add_edge(0,i); } lca.build(); for (int i = 0; i < q; i++) { int a,b;cin >> a >> b; a--;b--; if (uf.same(a,b)) { sum += lca.dist(a,b); } else { cost[a]++; cost[b]++; } } used.resize(n,false); for (int i = 0; i < n; i++) { if (used[i]) continue; dfs1(i); } used.assign(n,false); for (int i = 0; i < n; i++) { if (used[i]) continue; sum += dfs2(i, 0, 0); } cout << sum << endl; return 0; }