#include using namespace std; using ll = long long; using ld = long double; using VI = vector; using VL = vector; using VS = vector; template using PQ = priority_queue, greater>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define erFOR(i,a,n) for(int i=(n);i>=(a);--i) #define SORT(a) sort(a.begin(),a.end()) #define rSORT(a) sort(a.rbegin(),a.rend()) #define fSORT(a,f) sort(a.begin(),a.end(),f) #define all(a) a.begin(),a.end() #define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x)) #define tp(a,i) get(a) #ifdef _DEBUG #define line cout << "-----------------------------\n" #define stop system("pause") #define debug(x) print(x) #endif constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; templateinline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } templateinline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } templateinline istream& operator>>(istream& is, vector& v) { for (auto& a : v)is >> a; return is; } templateinline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } templateinline vector vec(size_t a) { return vector(a); } templateinline vector defvec(T def, size_t a) { return vector(a, def); } templateinline auto vec(size_t a, Ts... ts) { return vector(ts...))>(a, vec(ts...)); } templateinline auto defvec(T def, size_t a, Ts... ts) { return vector(def, ts...))>(a, defvec(def, ts...)); } templateinline void print(const T& a) { cout << a << "\n"; } templateinline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } templateinline void print(const vector& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } templateinline void print(const vector>& v) { for (auto& a : v)print(a); } inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; } templateinline T sum(const vector& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); } templateinline void END(T s) { print(s); exit(0); } void END() { exit(0); } VI dep, kind, cnt; vector nxt, g; int k = 0; void dfs1(int cur, int par = -1) { kind[cur] = k; for (const int& to : g[cur]) { if (to != par) { dep[to] = dep[cur] + 1; nxt[0][to] = cur; dfs1(to, cur); } } } int lca(int a, int b) { if (dep[a] > dep[b])swap(a, b); int d = dep[b] - dep[a]; FOR(j, 0, 18)if (d & (1 << j))b = nxt[j][b]; if (a == b)return a; rFOR(j, 0, 18)if (nxt[j][a] != nxt[j][b]) a = nxt[j][a], b = nxt[j][b]; return nxt[0][a]; } int dis(int a, int b) { return dep[a] + dep[b] - dep[lca(a, b)] * 2; } int dfs2(int cur, int par = -1) { for (const int& to : g[cur]) { if (to != par)cnt[cur] += dfs2(to, cur); } return cnt[cur]; } vector yet; vector ver; void dfs3(ll& res, ll val, int cur, int par = -1) { yet[cur] = false; for (const int& to : g[cur]) { if (to != par) { ll backup = val; chmin(res, val += ver[kind[cur]].size() - cnt[to] * 2); dfs3(res, val, to, cur); val -= ver[kind[cur]].size() - cnt[to] * 2; } } } #include #include #include namespace atcoder { struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector> groups() { std::vector leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } private: int _n; std::vector parent_or_size; }; } // namespace atcoder int main() { init(); //input-begin int n, m, q; cin >> n >> m >> q; atcoder::dsu uf(n); g.resize(n); FOR(i, 0, m) { int u, v; cin >> u >> v; uf.merge(--u, --v); g[u].push_back(v); g[v].push_back(u); } //input-end //lca-init-begin dep.resize(n, -1), kind.resize(n); nxt = defvec(-1, 18, n); FOR(i, 0, n)if (dep[i] == -1) { dep[uf.leader(i)] = 0; dfs1(uf.leader(i)); ++k; } FOR(j, 0, 17)FOR(i, 0, n)if (nxt[j][i] != -1) nxt[j + 1][i] = nxt[j][nxt[j][i]]; //lca-init-end //kueri cnt.resize(n), ver.resize(k); ll ans = 0; while (q--) { int a, b; cin >> a >> b; if (uf.same(--a, --b))ans += dis(a, b); else { ver[kind[a]].push_back(a); ver[kind[b]].push_back(b); ++cnt[a], ++cnt[b]; } } yet.resize(n, true); FOR(i, 0, n) { if (!yet[i])continue; int rt = uf.leader(i), idx = kind[rt]; ll val = 0; for (const int& v : ver[idx]) { val += dis(rt, v); } ll res = val; dfs2(rt); dfs3(res, val, rt); ans += res; } print(ans); return 0; }