#include #include #include #include #include #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; /*-------------------------------------------------*/ struct UnionFind { UnionFind(int n) : data(n, -1) {} int root(int ver) { return data[ver] < 0 ? ver : data[ver] = root(data[ver]); } void unite(int ver1, int ver2) { ver1 = root(ver1); ver2 = root(ver2); if (ver1 != ver2) { if (data[ver1] > data[ver2]) swap(ver1, ver2); data[ver1] += data[ver2]; data[ver2] = ver1; } } bool same(int ver1, int ver2) { return root(ver1) == root(ver2); } int size(int ver) { return -data[root(ver)]; } private: vector data; }; using CostType = int; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; } inline bool operator>(const Edge &rhs) const { return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src; } inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; } }; struct LCA { vector depth; vector dist; LCA() {}; LCA(const vector > &graph) : graph(graph) { n = graph.size(); depth.resize(n); dist.resize(n); while ((1 << table_h) <= n) ++table_h; parent.resize(table_h, vector(n)); } void build(int root = 0) { dfs(-1, root, 0, 0); for (int i = 0; i + 1 < table_h; ++i) REP(ver, n) { parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]); } } int query(int u, int v) { if (depth[u] > depth[v]) swap(u, v); REP(i, table_h) { if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v]; } if (u == v) return u; for (int i = table_h - 1; i >= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; } private: int n, table_h = 1; vector > graph; vector > parent; void dfs(int par, int ver, int now_depth, CostType now_dist) { depth[ver] = now_depth; dist[ver] = now_dist; parent[0][ver] = par; for (const Edge &e : graph[ver]) { if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost); } } }; int main() { int n, m, q; cin >> n >> m >> q; vector > graph(n); UnionFind uf(n); while (m--) { int u, v; cin >> u >> v; --u; --v; graph[u].emplace_back(v); graph[v].emplace_back(u); uf.unite(u, v); } long long ans = 0; vector sum(n, 0); map > block; REP(i, n) { int rt = uf.root(i), sz = block[rt].size(); block[rt][i] = sz; } map lca; for (const auto &pr : block) { auto mp = pr.second; vector > g(mp.size()); for (auto &pr : mp) { for (int e : graph[pr.first]) { g[mp[pr.first]].emplace_back(mp[pr.first], mp[e], 1); } } lca[pr.first] = LCA(g); lca[pr.first].build(); } while (q--) { int a, b; cin >> a >> b; --a; --b; if (uf.same(a, b)) { int rt = uf.root(a); ans += lca[rt].distance(block[rt][a], block[rt][b]); } else { ++sum[a]; ++sum[b]; } } vector visited(n); vector dp(n, 0); function dfs1 = [&](int par, int ver) { for (int e : graph[ver]) { if (e != par) { visited[e] = true; dfs1(ver, e); sum[ver] += sum[e]; dp[ver] += dp[e] + sum[e]; } } }; int root; long long tmp; function dfs2 = [&](int par, int ver, long long par_dp) { tmp = min(tmp, par_dp + dp[ver]); for (int e : graph[ver]) if (e != par) { long long nx_dp = par_dp + dp[ver] - (dp[e] + sum[e]); nx_dp += root - sum[e]; dfs2(ver, e, nx_dp); } }; REP(i, n) { if (!visited[i]) { dfs1(-1, i); root = sum[i]; tmp = dp[i]; dfs2(-1, i, 0); ans += tmp; } } cout << ans << '\n'; return 0; }