110052024-06-04 12:04:51zsomborÚtvonalakcpp17Wrong answer 0/1001.103s81148 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, -1);

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;
			while (l[s.top()] >= d[x]) { v.push_back(s.top()); s.pop(); }
			v.push_back(x);
			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;
	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";
	cout << wsum[a] + wsum[b] - 2 * wsum[c] + w[c] - 2 << "\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";*/

	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);
			cout << i << " " << comp[x] << endl;
		}
	}

	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 4
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

*/
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer63ms54016 KiB
2Wrong answer228ms58752 KiB
subtask20/15
3Wrong answer64ms54248 KiB
4Wrong answer57ms54176 KiB
5Wrong answer93ms55056 KiB
6Wrong answer79ms54508 KiB
7Wrong answer126ms56548 KiB
8Wrong answer209ms58700 KiB
subtask30/15
9Wrong answer70ms54244 KiB
10Wrong answer64ms54388 KiB
11Wrong answer90ms55140 KiB
12Wrong answer120ms55584 KiB
13Wrong answer441ms61876 KiB
14Wrong answer782ms69556 KiB
15Time limit exceeded1.098s78016 KiB
subtask40/15
16Wrong answer59ms54320 KiB
17Wrong answer65ms54244 KiB
18Wrong answer75ms54500 KiB
19Wrong answer148ms56480 KiB
20Wrong answer241ms58780 KiB
subtask50/15
21Wrong answer65ms53864 KiB
22Wrong answer65ms53988 KiB
23Wrong answer70ms54452 KiB
24Wrong answer57ms54136 KiB
25Wrong answer64ms53988 KiB
26Wrong answer64ms54116 KiB
subtask60/40
27Wrong answer63ms54120 KiB
28Wrong answer256ms58768 KiB
29Wrong answer64ms54248 KiB
30Wrong answer57ms54176 KiB
31Wrong answer93ms55056 KiB
32Wrong answer79ms54508 KiB
33Wrong answer126ms56548 KiB
34Wrong answer209ms58700 KiB
35Wrong answer70ms54244 KiB
36Wrong answer64ms54388 KiB
37Wrong answer90ms55140 KiB
38Wrong answer120ms55584 KiB
39Wrong answer441ms61876 KiB
40Wrong answer782ms69556 KiB
41Time limit exceeded1.098s78016 KiB
42Wrong answer59ms54320 KiB
43Wrong answer65ms54244 KiB
44Wrong answer75ms54500 KiB
45Wrong answer148ms56480 KiB
46Wrong answer241ms58780 KiB
47Wrong answer65ms53864 KiB
48Wrong answer65ms53988 KiB
49Wrong answer70ms54452 KiB
50Wrong answer57ms54136 KiB
51Wrong answer64ms53988 KiB
52Wrong answer64ms54116 KiB
53Time limit exceeded1.093s79484 KiB
54Wrong answer59ms54244 KiB
55Wrong answer107ms55212 KiB
56Wrong answer116ms54628 KiB
57Wrong answer347ms56640 KiB
58Wrong answer196ms57076 KiB
59Time limit exceeded1.103s81148 KiB
60Wrong answer656ms59112 KiB
61Wrong answer680ms59368 KiB
62Wrong answer575ms59244 KiB