#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int n, m, q;
vector <vector <int>> g(2e5);
vector <int> p(2e5, 0);
vector <int> d(2e5, 0);
vector <int> l(2e5, 0);
vector <bool> cut(2e5, false);
stack <int> s;
vector <vector <int>> bcc;
int k;
vector <int> comp(2e5, -1);
vector <vector <int>> t(3e5);
vector <int> D(3e5, 0);
vector <vector <int>> up(3e5, vector <int>(20, 0));
vector <int> w(3e5, -1);
vector <int> wsum(3e5, 0);
void dfs(int x) {
s.push(x);
for (int i : g[x]) {
if (p[x] == i) continue;
if (p[i] && d[i] > d[x]) continue;
if (p[i] && d[i] < d[x]) { l[x] = min(l[x], d[i]); continue; }
p[i] = x;
d[i] = l[i] = d[x] + 1;
dfs(i);
l[x] = min(l[x], l[i]);
if (l[i] >= d[x]) {
cut[x] = true;
vector <int> v;
v.push_back(x);
while (v.back() != i) { v.push_back(s.top()); s.pop(); }
bcc.push_back(v);
}
}
}
void dfs2(int x) {
for (int i : t[x]) {
if (up[x][0] == i) continue;
D[i] = D[x] + 1;
up[i][0] = x;
wsum[i] = wsum[x] + w[i];
dfs2(i);
}
}
int lca(int a, int b) {
if (D[a] < D[b]) swap(a, b);
for (int j = 19; j >= 0; j--) {
if (D[up[a][j]] >= D[b]) a = up[a][j];
}
if (a == b) return a;
for (int j = 19; j >= 0; j--) {
if (up[a][j] == up[b][j]) continue;
a = up[a][j];
b = up[b][j];
}
return up[a][0];
}
void solve() {
int a, b, c, ans;
cin >> a >> b;
a = comp[a]; b = comp[b];
c = lca(a, b);
ans = wsum[a] + wsum[b] - 2 * wsum[c] + w[c] - 2;
if (a >= bcc.size()) ans++;
if (b >= bcc.size()) ans++;
cout << ans << "\n";
}
int main() {
cin >> n >> m >> q;
int a, b;
for (int i = 0; i < m; i++) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
p[1] = l[1] = -1;
dfs(1);
int cnt = 0;
for (int i : g[1]) if (p[i] == 1) cnt++;
if (cnt < 2) cut[1] = false;
k = bcc.size();
for (int i = 0; i < bcc.size(); i++) {
for (int x : bcc[i]) {
if (!cut[x]) { comp[x] = i; continue; }
if (comp[x] == -1) comp[x] = k++;
t[i].push_back(comp[x]);
t[comp[x]].push_back(i);
}
}
for (int i = 0; i < bcc.size(); i++) w[i] = bcc[i].size();
wsum[0] = w[0];
dfs2(0);
for (int j = 1; j < 20; j++) {
for (int i = 0; i < k; i++) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
while (q--) solve();
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 52ms | 53988 KiB | ||||
2 | Elfogadva | 216ms | 58972 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Elfogadva | 67ms | 54116 KiB | ||||
4 | Elfogadva | 56ms | 54116 KiB | ||||
5 | Elfogadva | 76ms | 55140 KiB | ||||
6 | Elfogadva | 67ms | 54612 KiB | ||||
7 | Elfogadva | 128ms | 56532 KiB | ||||
8 | Elfogadva | 221ms | 58876 KiB | ||||
subtask3 | 15/15 | ||||||
9 | Elfogadva | 65ms | 54116 KiB | ||||
10 | Elfogadva | 57ms | 54244 KiB | ||||
11 | Elfogadva | 78ms | 55140 KiB | ||||
12 | Elfogadva | 71ms | 55392 KiB | ||||
13 | Elfogadva | 186ms | 60820 KiB | ||||
14 | Elfogadva | 321ms | 67584 KiB | ||||
15 | Elfogadva | 754ms | 82068 KiB | ||||
subtask4 | 15/15 | ||||||
16 | Elfogadva | 59ms | 54152 KiB | ||||
17 | Elfogadva | 61ms | 54244 KiB | ||||
18 | Elfogadva | 68ms | 54712 KiB | ||||
19 | Elfogadva | 143ms | 56456 KiB | ||||
20 | Elfogadva | 215ms | 58908 KiB | ||||
subtask5 | 15/15 | ||||||
21 | Elfogadva | 54ms | 53988 KiB | ||||
22 | Elfogadva | 67ms | 53988 KiB | ||||
23 | Elfogadva | 67ms | 54264 KiB | ||||
24 | Elfogadva | 68ms | 54244 KiB | ||||
25 | Elfogadva | 56ms | 54116 KiB | ||||
26 | Elfogadva | 54ms | 54116 KiB | ||||
subtask6 | 40/40 | ||||||
27 | Elfogadva | 64ms | 54020 KiB | ||||
28 | Elfogadva | 252ms | 58972 KiB | ||||
29 | Elfogadva | 67ms | 54116 KiB | ||||
30 | Elfogadva | 56ms | 54116 KiB | ||||
31 | Elfogadva | 76ms | 55140 KiB | ||||
32 | Elfogadva | 67ms | 54612 KiB | ||||
33 | Elfogadva | 128ms | 56532 KiB | ||||
34 | Elfogadva | 221ms | 58876 KiB | ||||
35 | Elfogadva | 65ms | 54116 KiB | ||||
36 | Elfogadva | 57ms | 54244 KiB | ||||
37 | Elfogadva | 78ms | 55140 KiB | ||||
38 | Elfogadva | 71ms | 55392 KiB | ||||
39 | Elfogadva | 186ms | 60820 KiB | ||||
40 | Elfogadva | 321ms | 67584 KiB | ||||
41 | Elfogadva | 754ms | 82068 KiB | ||||
42 | Elfogadva | 59ms | 54152 KiB | ||||
43 | Elfogadva | 61ms | 54244 KiB | ||||
44 | Elfogadva | 68ms | 54712 KiB | ||||
45 | Elfogadva | 143ms | 56456 KiB | ||||
46 | Elfogadva | 215ms | 58908 KiB | ||||
47 | Elfogadva | 54ms | 53988 KiB | ||||
48 | Elfogadva | 67ms | 53988 KiB | ||||
49 | Elfogadva | 67ms | 54264 KiB | ||||
50 | Elfogadva | 68ms | 54244 KiB | ||||
51 | Elfogadva | 56ms | 54116 KiB | ||||
52 | Elfogadva | 54ms | 54116 KiB | ||||
53 | Elfogadva | 572ms | 81808 KiB | ||||
54 | Elfogadva | 68ms | 54372 KiB | ||||
55 | Elfogadva | 79ms | 55140 KiB | ||||
56 | Elfogadva | 114ms | 54628 KiB | ||||
57 | Elfogadva | 316ms | 56624 KiB | ||||
58 | Elfogadva | 175ms | 57056 KiB | ||||
59 | Elfogadva | 958ms | 82068 KiB | ||||
60 | Elfogadva | 684ms | 59224 KiB | ||||
61 | Elfogadva | 685ms | 59228 KiB | ||||
62 | Elfogadva | 683ms | 59208 KiB |