// {{{ TEMPLATES #include using namespace std; #define rep(i,n) for (int i = 0; i < int(n); i++) #define rep1(i,n) for (int i = 1; i <= int(n); i++) #define repR(i,n) for (int i = int(n)-1; i >= 0; i--) #define rep1R(i,n) for (int i = int(n); i >= 1; i--) #define loop(i,a,B) for (int i = a; i B; i++) #define loopR(i,a,B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define eb emplace_back #define mp make_pair #define fst first #define snd second #ifdef LOCAL #define dump(...) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] ", my_dmp(#__VA_ARGS__, __VA_ARGS__) #define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl; void my_dmp(const char*) { cerr << endl; } template void my_dmp(const char *s, T const& x, U const& ...y) { const char *o = "({[", *e = "]})"; for (int i = 0; *s != '\0'; cerr << *s++) { if (count(o,o+3,*s)) i++; if (count(e,e+3,*s)) i--; if (!i && *s == ',') break; } cerr << " = " << x; if (*s == ',') cerr << ", ", s++; my_dmp(s, y...); } #else #define dump(...) #define say(x) #endif using ll = long long; using ld = long double; #define int ll #define double ld template using pque_max = priority_queue; template using pque_min = priority_queue, greater >; template ::value>::type> ostream& operator<<(ostream& os, T const& v) { os << "{"; for (auto const& x : v) os << " " << x; return os << " }"; } template istream& operator>>(istream& is, vector& v) { for (auto& x : v) is >> x; return is; } template ostream& operator<<(ostream& os, pair const& p) { return os << "(" << p.first << ", " << p.second << ")"; } template istream& operator>>(istream& is, pair& p) { return is >> p.first >> p.second; } template typename enable_if= tuple_size::value>::type output_tuple(ostream&, T const&) { } template typename enable_if::value>::type output_tuple(ostream& os, T const& t) { os << (i ? " " : "") << get(t); output_tuple(os,t); } template ostream& operator<<(ostream& os, tuple const& t) { return output_tuple(os,t), os; } struct my_Init { my_Init() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } my_init; template struct vec_impl { using type = vector::type>; template static type make_v(size_t n, U&&... x) { return type(n, vec_impl::make_v(forward(x)...)); } }; template struct vec_impl { using type = T; static type make_v(T const& x = {}) { return x; } }; template using vec = typename vec_impl::type; template auto make_v(Args&&... args) { return vec_impl::make_v(forward(args)...); } template void quit(T const& x) { cout << x << endl; exit(0); } template constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; } template constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; } template constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits::value_type{}); } const ll INF = (1LL<<62)-1; // ~ 4.6e18 // }}} /////////////////////////////// // struct UnionFind { int n, sz; // id : 0...n-1 vector par; UnionFind(int n = 0) : n(n), sz(n), par(n,-1) { } int root(int x) { assert(0 <= x && x < n); return par[x] < 0 ? x : par[x] = root(par[x]); } bool unite(int x, int y) { x = root(x), y = root(y); if (x == y) return false; sz--; if (par[x] < par[y]) swap(x,y); par[y] += par[x]; par[x] = y; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -par[root(x)]; } int size() const { return sz; } struct groups_t { vector > grp; vector > id; }; groups_t groups() { vector > g(n); rep (i,n) if (par[i] < 0) g[i].reserve(-par[i]); rep (i,n) g[root(i)].push_back(i); vector > grp; grp.reserve(size()); rep (i,n) if (g[i].size()) grp.emplace_back(move(g[i])); vector > id(n); rep (i,grp.size()) rep (j,grp[i].size()) { id[grp[i][j]] = make_pair(i,j); } return { grp, id }; } }; // // constexpr int dst(int v) { return v; } template constexpr int dst(E const& e) { return e.to; } template struct LCA { vector > const* g; int32_t n,lg,root; vector dep; vector > anc; LCA() {} LCA(vector > const* g, int root = 0) : g(g), n(g->size()), root(root), dep(n) { lg = 1; while ((1<(n,-1)); dfs(root); rep (i,lg-1) rep (j,n) if (anc[i][j] >= 0) { anc[i+1][j] = anc[i][anc[i][j]]; } } void dfs(int x, int p = -1) { anc[0][x] = p; dep[x] = (p == -1 ? 0 : dep[p]+1); for (auto const& e : (*g)[x]) if (dst(e) != p) dfs(dst(e),x); } int operator()(int a, int b) const { return lca(a,b); } int lca(int a, int b) const { if (dep[a] > dep[b]) swap(a,b); for (int dd = dep[b]-dep[a], i = 0; dd; dd >>= 1, i++) { if (dd&1) b = anc[i][b]; } if (a == b) return a; for (int i = lg-1; i >= 0; i--) { if (anc[i][a] != anc[i][b]) a = anc[i][a], b = anc[i][b]; } return anc[0][a]; } int dist(int a, int b) const { return dep[a] + dep[b] - 2*dep[lca(a,b)]; } int climb(int v, int dist) const { for (int i = 0; dist && v >= 0; dist >>= 1, i++) { if (dist&1) v = anc[i][v]; } return v; } }; template auto get_lca(vector > const& g) { return LCA(&g); } // template struct FixPoint : private F { constexpr FixPoint(F&& f) : F(forward(f)) {} template constexpr auto operator()(Args&&... args) const { return F::operator()(*this, forward(args)...); } }; struct MakeFixPoint { template constexpr auto operator|(F&& f) const { return FixPoint(forward(f)); } }; #define MFP MakeFixPoint()| int f(vec const& g, vector const& v) { // dump(g); dump(v); // vector c(g.size()); for (int x : v) c[x]++; auto dp_cnt = g; auto dp_sum = g; for (auto &x : dp_cnt) for (int &y : x) y = -1; for (auto &x : dp_sum) for (int &y : x) y = -1; auto cnt = MFP [&](auto cnt, int p, int i) -> int { int &res = dp_cnt[p][i]; if (res >= 0) return res; int x = g[p][i]; res = c[x]; rep (j,g[x].size()) { if (g[x][j] == p) continue; res += cnt(x,j); } return res; }; auto sum = MFP [&](auto sum, int p, int i) -> int { int &res = dp_sum[p][i]; if (res >= 0) return res; int x = g[p][i]; res = 0; rep (j,g[x].size()) { if (g[x][j] == p) continue; res += sum(x,j) + cnt(x,j); } return res; }; int mi = INF; rep (i,g.size()) { int s = 0; rep (j,g[i].size()) s += sum(i,j) + cnt(i,j); chmin(mi,s); } return mi; } int32_t main() { int n,m,q; cin >> n >> m >> q; vector > es(m); UnionFind uf(n); rep (i,m) { int u,v; cin >> u >> v; u--,v--; es[i] = mp(u,v); uf.unite(u,v); } auto res = uf.groups(); auto const& id = res.id; dump(id); using Graph = vec; vector g(uf.size()); rep (k,uf.size()) g[k].resize(res.grp[k].size()); rep (_,m) { int u,v; tie(u,v) = es[_]; int k,i,j; tie(k,i) = id[u]; tie(k,j) = id[v]; g[k][i].eb(j); g[k][j].eb(i); } say("graph constructed"); int ans = 0; vector > v(uf.size()); vector > lca(uf.size()); rep (i,uf.size()) lca[i] = get_lca(g[i]); rep (_,q) { int a,b; cin >> a >> b; a--,b--; if (uf.same(a,b)) { int k,i,j; tie(k,i) = id[a]; tie(k,j) = id[b]; ans += lca[k].dist(i,j); } else { int k1,i; tie(k1,i) = id[a]; int k2,j; tie(k2,j) = id[b]; v[k1].eb(i); v[k2].eb(j); } } // dump(ans); rep (k,uf.size()) ans += f(g[k],v[k]); cout << ans << endl; }