110142024-06-04 14:57:27zsomborÚtvonalakcpp17Elfogadva 100/100449ms82068 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);
	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() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	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ÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva63ms54052 KiB
2Elfogadva138ms58972 KiB
subtask215/15
3Elfogadva54ms54196 KiB
4Elfogadva56ms54116 KiB
5Elfogadva74ms55140 KiB
6Elfogadva70ms54500 KiB
7Elfogadva90ms56544 KiB
8Elfogadva140ms58740 KiB
subtask315/15
9Elfogadva54ms54064 KiB
10Elfogadva65ms54248 KiB
11Elfogadva63ms55272 KiB
12Elfogadva75ms55480 KiB
13Elfogadva118ms60820 KiB
14Elfogadva264ms67604 KiB
15Elfogadva449ms82068 KiB
subtask415/15
16Elfogadva61ms54268 KiB
17Elfogadva61ms54428 KiB
18Elfogadva64ms54776 KiB
19Elfogadva87ms56580 KiB
20Elfogadva136ms58992 KiB
subtask515/15
21Elfogadva65ms54116 KiB
22Elfogadva54ms54120 KiB
23Elfogadva67ms54308 KiB
24Elfogadva65ms54124 KiB
25Elfogadva61ms54244 KiB
26Elfogadva64ms54120 KiB
subtask640/40
27Elfogadva64ms54032 KiB
28Elfogadva128ms59136 KiB
29Elfogadva54ms54196 KiB
30Elfogadva56ms54116 KiB
31Elfogadva74ms55140 KiB
32Elfogadva70ms54500 KiB
33Elfogadva90ms56544 KiB
34Elfogadva140ms58740 KiB
35Elfogadva54ms54064 KiB
36Elfogadva65ms54248 KiB
37Elfogadva63ms55272 KiB
38Elfogadva75ms55480 KiB
39Elfogadva118ms60820 KiB
40Elfogadva264ms67604 KiB
41Elfogadva449ms82068 KiB
42Elfogadva61ms54268 KiB
43Elfogadva61ms54428 KiB
44Elfogadva64ms54776 KiB
45Elfogadva87ms56580 KiB
46Elfogadva136ms58992 KiB
47Elfogadva65ms54116 KiB
48Elfogadva54ms54120 KiB
49Elfogadva67ms54308 KiB
50Elfogadva65ms54124 KiB
51Elfogadva61ms54244 KiB
52Elfogadva64ms54120 KiB
53Elfogadva407ms81768 KiB
54Elfogadva71ms54256 KiB
55Elfogadva64ms55012 KiB
56Elfogadva79ms54628 KiB
57Elfogadva126ms56776 KiB
58Elfogadva101ms57076 KiB
59Elfogadva439ms82068 KiB
60Elfogadva189ms59352 KiB
61Elfogadva188ms59312 KiB
62Elfogadva181ms59332 KiB