#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 400'001;
const int MAXK = 20;
vector<int> g[MAXN], g2[MAXN];
vector<int> st;
vector<vector<int>> comp;
int tin2[MAXN], low[MAXN], siz[MAXN], bcc[MAXN], timer = 1, nodes = 0, up[MAXN][MAXK], pref[MAXN], tin[MAXN], tout[MAXN], fok[MAXN];
bool is_art[MAXN];
void dfs(int u, int p = -1) {
tin2[u] = low[u] = timer++;
st.emplace_back(u);
int cc = 0;
for (int v : g[u]) {
if (v == p) continue;
if (tin2[v]) {
low[u] = min(low[u], tin2[v]);
} else {
cc++;
dfs(v, u);
low[u] = min(low[u], low[v]);
if (tin2[u] <= low[v]) {
is_art[u] = (tin2[u] > 1 || tin2[v] > 2);
comp.push_back({u});
while (st.back() != v) {
comp.back().emplace_back(st.back());
st.pop_back();
}
st.pop_back();
comp.back().emplace_back(v);
}
}
}
}
void dfs2(int u, int p = -1) {
tin[u] = timer++;
pref[u] += siz[u];
for (int v : g2[u]) {
if (v != p) {
pref[v] += pref[u];
up[v][0] = u;
dfs2(v, u);
}
}
tout[u] = timer++;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int k = MAXK - 1; k >= 0; k--) {
if (up[u][k] && !is_ancestor(up[u][k], v)) {
u = up[u][k];
}
}
return up[u][0];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m, q; cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
fok[u]++;
fok[v]++;
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (is_art[i]) {
bcc[i] = ++nodes;
siz[bcc[i]] = 1;
}
}
vector<int> idx(MAXN);
for (int i = 0; i < comp.size(); i++) {
int t = ++nodes;
idx[i] = t;
for (int j : comp[i]) {
if (!is_art[j]) {
bcc[j] = t;
}
}
siz[t] = comp[i].size() - 2;
}
for (int i = 0; i < comp.size(); i++) {
for (int j : comp[i]) {
if (is_art[j]) {
g2[idx[i]].emplace_back(bcc[j]);
g2[bcc[j]].emplace_back(idx[i]);
}
}
}
for (int i = 1; i <= n; i++) {
sort(g2[i].begin(), g2[i].end());
g2[i].erase(unique(g2[i].begin(), g2[i].end()), g2[i].end());
}
timer = 1;
dfs2(1);
/*
for (int i = 1; i <= n; i++) {
cerr << pref[bcc[i]] << " ";
}
cerr << endl;*/
for (int k = 1; k < MAXK; k++) {
for (int i = 1; i <= nodes; i++) {
up[i][k] = up[up[i][k-1]][k-1];
}
}
while (q--) {
int u, v; cin >> u >> v;
int nu = bcc[u], nv = bcc[v];
int l = lca(nu, nv);
int ans = pref[nu] + pref[nv] - 2 * pref[l] + siz[l];
if (is_art[u]) ans--;
if (is_art[v]) ans--;
cout << ans << "\n";
// cerr << "lca = " << lca(u, v) << endl;
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 21ms | 20836 KiB | ||||
2 | Accepted | 94ms | 27292 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 23ms | 20964 KiB | ||||
4 | Accepted | 19ms | 20968 KiB | ||||
5 | Accepted | 23ms | 22632 KiB | ||||
6 | Accepted | 28ms | 21604 KiB | ||||
7 | Accepted | 54ms | 24288 KiB | ||||
8 | Accepted | 82ms | 27276 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Accepted | 18ms | 21288 KiB | ||||
10 | Accepted | 25ms | 21420 KiB | ||||
11 | Accepted | 28ms | 22628 KiB | ||||
12 | Accepted | 29ms | 23840 KiB | ||||
13 | Accepted | 81ms | 35476 KiB | ||||
14 | Accepted | 208ms | 50032 KiB | ||||
15 | Accepted | 312ms | 63944 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Accepted | 24ms | 21008 KiB | ||||
17 | Accepted | 25ms | 21240 KiB | ||||
18 | Accepted | 25ms | 22008 KiB | ||||
19 | Accepted | 56ms | 24564 KiB | ||||
20 | Accepted | 96ms | 27288 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Accepted | 17ms | 20984 KiB | ||||
22 | Accepted | 18ms | 20708 KiB | ||||
23 | Accepted | 24ms | 21196 KiB | ||||
24 | Accepted | 23ms | 21008 KiB | ||||
25 | Accepted | 21ms | 20836 KiB | ||||
26 | Accepted | 18ms | 20840 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Accepted | 18ms | 20708 KiB | ||||
28 | Accepted | 100ms | 27280 KiB | ||||
29 | Accepted | 23ms | 20964 KiB | ||||
30 | Accepted | 19ms | 20968 KiB | ||||
31 | Accepted | 23ms | 22632 KiB | ||||
32 | Accepted | 28ms | 21604 KiB | ||||
33 | Accepted | 54ms | 24288 KiB | ||||
34 | Accepted | 82ms | 27276 KiB | ||||
35 | Accepted | 18ms | 21288 KiB | ||||
36 | Accepted | 25ms | 21420 KiB | ||||
37 | Accepted | 28ms | 22628 KiB | ||||
38 | Accepted | 29ms | 23840 KiB | ||||
39 | Accepted | 81ms | 35476 KiB | ||||
40 | Accepted | 208ms | 50032 KiB | ||||
41 | Accepted | 312ms | 63944 KiB | ||||
42 | Accepted | 24ms | 21008 KiB | ||||
43 | Accepted | 25ms | 21240 KiB | ||||
44 | Accepted | 25ms | 22008 KiB | ||||
45 | Accepted | 56ms | 24564 KiB | ||||
46 | Accepted | 96ms | 27288 KiB | ||||
47 | Accepted | 17ms | 20984 KiB | ||||
48 | Accepted | 18ms | 20708 KiB | ||||
49 | Accepted | 24ms | 21196 KiB | ||||
50 | Accepted | 23ms | 21008 KiB | ||||
51 | Accepted | 21ms | 20836 KiB | ||||
52 | Accepted | 18ms | 20840 KiB | ||||
53 | Accepted | 233ms | 65592 KiB | ||||
54 | Accepted | 19ms | 20964 KiB | ||||
55 | Accepted | 27ms | 22628 KiB | ||||
56 | Accepted | 27ms | 21860 KiB | ||||
57 | Accepted | 67ms | 24544 KiB | ||||
58 | Accepted | 68ms | 24844 KiB | ||||
59 | Accepted | 307ms | 67964 KiB | ||||
60 | Accepted | 159ms | 27904 KiB | ||||
61 | Accepted | 123ms | 28432 KiB | ||||
62 | Accepted | 165ms | 28636 KiB |