#include using namespace std; using ll = long long; struct LCA { using Graph = vector< vector >; const Graph& g; const int LOGV; const int root; vector< vector > parent; vector depth; LCA(const Graph& g, int root = 0) : g(g), LOGV(32 - __builtin_clz(g.size())), root(root) { int V = g.size(); parent.resize(LOGV, vector(V)); depth.resize(V); dfs(root, -1, 0); for (int k = 0; k + 1 < LOGV; k++) { for (int v = 0; v < V; v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (int i : g[v]) { if (i != p) dfs(i, v, d + 1); } } int operator()(int u, int v) const { if (depth[u] > depth[v]) swap(u, v); for (int k = 0; k < LOGV; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = LOGV - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; struct UnionFind { //各要素が属する集合の代表(根)を管理する //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい vector data; UnionFind(int sz) : data(sz, -1) {} bool unite(int x, int y) { x = find(x); y = find(y); bool is_union = (x != y); if (is_union) { if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } return is_union; } int find(int x) { if (data[x] < 0) { //要素xが根である return x; } else { data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される return data[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return -data[find(x)]; } }; void pre_dfs(int cur, int par, const vector< vector >& g, const vector& cnt, vector& cnt_sub, vector& ans) { cnt_sub[cur] += cnt[cur]; for (int nex : g[cur]) { if (nex == par) continue; pre_dfs(nex, cur, g, cnt, cnt_sub, ans); cnt_sub[cur] += cnt_sub[nex]; ans[cur] += ans[nex] + cnt_sub[nex]; } } void dfs(int cur, int par, const vector< vector >& g, const vector& cnt_sub, const vector& ans, ll par_ans, ll par_cnt, ll& total_ans) { // cerr << cur << " " << par_ans + ans[cur] << " " << cnt_sub[cur] << "\n"; total_ans = min(total_ans, ans[cur] + par_ans); for (int nex : g[cur]) { if (nex == par) continue; dfs(nex, cur, g, cnt_sub, ans, ans[cur] + par_ans - ans[nex] - cnt_sub[nex] + par_cnt + cnt_sub[cur] - cnt_sub[nex], par_cnt + cnt_sub[cur] - cnt_sub[nex], total_ans); } } const ll INF = 1e15; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; vector< vector > g(n + 1), G(n + 1); UnionFind uf(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; uf.unite(u, v); g[u].push_back(v); g[v].push_back(u); G[u].push_back(v); G[v].push_back(u); } for (int i = 0; i < n; i++) { if (uf.find(i) != i) continue; G[n].push_back(i); G[i].push_back(n); } LCA lca(G, n); const vector& depth = lca.depth; vector cnt(n, 0); ll final_ans = 0; for (int i = 0; i < q; i++) { int u, v; cin >> u >> v; u--; v--; if (!uf.same(u, v)) { cnt[u]++; cnt[v]++; } else { final_ans += depth[u] + depth[v] - depth[lca(u, v)] * 2; } } vector cnt_sub(n, 0); vector ans(n, 0); for (int i = 0; i < n; i++) { if (uf.find(i) != i) continue; pre_dfs(i, -1, g, cnt, cnt_sub, ans); ll total_ans = INF; dfs(i, -1, g, cnt_sub, ans, 0, 0, total_ans); final_ans += total_ans; } cout << final_ans << "\n"; return 0; }