110112024-06-04 14:45:31zsomborÚtvonalakcpp17Accepted 100/100903ms82080 KiB
#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);
	//cout << a << " " << b << " " << c << endl;
	//cout << wsum[a] << " " << wsum[b] << " " << wsum[c] << "\n";
	if (a != c && a >= bcc.size()) a = up[a][0];
	if (b != c && b >= bcc.size()) b = up[b][0];
	ans = wsum[a] + wsum[b] - 2 * wsum[c] + w[c] - 2;
	if (c >= bcc.size() && (a == c || b == c)) 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;

	/*cout << "\n";
	for (int i = 1; i <= n; i++) if (cut[i]) cout << i << " ";
	cout << "\n\n";
	for (auto v : bcc) {
		for (int i : v) cout << i << " ";
		cout << "\n";
	}
	cout << "\n";
	for (int i = 1; i <= n; i++) cout << p[i] << " ";*/

	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();
}

/*

10 13 5
1 8
1 10
10 9
9 8
8 10
8 6
8 7
7 5
6 5
5 4
6 3
3 2
2 6
8 10
1 7
4 5
6 4
7 8

3 3 1
1 2
2 3
1 3
1 2

5 5 1
1 2
3 4
2 3
4 5
1 3
2 4

*/
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted63ms53988 KiB
2Accepted233ms59020 KiB
subtask215/15
3Accepted65ms54116 KiB
4Accepted57ms54180 KiB
5Accepted65ms55192 KiB
6Accepted75ms54500 KiB
7Accepted134ms56548 KiB
8Accepted209ms58876 KiB
subtask315/15
9Accepted56ms54116 KiB
10Accepted57ms54304 KiB
11Accepted64ms55164 KiB
12Accepted82ms55372 KiB
13Accepted172ms60676 KiB
14Accepted356ms67648 KiB
15Accepted774ms82080 KiB
subtask415/15
16Accepted70ms54176 KiB
17Accepted72ms54244 KiB
18Accepted78ms54544 KiB
19Accepted146ms56436 KiB
20Accepted230ms58844 KiB
subtask515/15
21Accepted54ms53988 KiB
22Accepted54ms54116 KiB
23Accepted64ms54444 KiB
24Accepted54ms54136 KiB
25Accepted64ms53988 KiB
26Accepted54ms54116 KiB
subtask640/40
27Accepted64ms54116 KiB
28Accepted244ms59000 KiB
29Accepted65ms54116 KiB
30Accepted57ms54180 KiB
31Accepted65ms55192 KiB
32Accepted75ms54500 KiB
33Accepted134ms56548 KiB
34Accepted209ms58876 KiB
35Accepted56ms54116 KiB
36Accepted57ms54304 KiB
37Accepted64ms55164 KiB
38Accepted82ms55372 KiB
39Accepted172ms60676 KiB
40Accepted356ms67648 KiB
41Accepted774ms82080 KiB
42Accepted70ms54176 KiB
43Accepted72ms54244 KiB
44Accepted78ms54544 KiB
45Accepted146ms56436 KiB
46Accepted230ms58844 KiB
47Accepted54ms53988 KiB
48Accepted54ms54116 KiB
49Accepted64ms54444 KiB
50Accepted54ms54136 KiB
51Accepted64ms53988 KiB
52Accepted54ms54116 KiB
53Accepted563ms81832 KiB
54Accepted68ms54116 KiB
55Accepted70ms55140 KiB
56Accepted114ms54664 KiB
57Accepted298ms56628 KiB
58Accepted186ms57060 KiB
59Accepted903ms82064 KiB
60Accepted703ms59088 KiB
61Accepted703ms59316 KiB
62Accepted662ms59212 KiB