CSES - HIIT Open 2024 - Results
Submission details
Task:Light rail connections
Sender:Aalto CS-A1140 Team 2
Submission time:2024-11-16 16:49:03 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.01 sdetails
#11ACCEPTED0.22 sdetails
#12ACCEPTED0.71 sdetails
#13ACCEPTED0.02 sdetails
#14ACCEPTED0.09 sdetails
#15ACCEPTED0.01 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.01 sdetails
#23ACCEPTED0.03 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.01 sdetails
#28ACCEPTED0.03 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.02 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.01 sdetails
#38ACCEPTED0.02 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 10100;

int n, m;
vector<pair<int, int>> g[N];
bool seen_as_bridge[N];

void components(int banned_edge) {
	vi depth(n, -1);
	vi mindepth(n, -1);
	auto dfs = [&](auto self, int u, int p) -> void {
		depth[u] = depth[p] + 1;
		mindepth[u] = depth[u];
		for (auto [v, ei] : g[u]) {
			if (ei == banned_edge) {
				continue;
			}
			if (depth[v] != -1) {
				// edge goes up
				mindepth[u] = min(depth[v], mindepth[u]);
			} else {
				if (mindepth[v] >= depth[v]) {
					seen_as_bridge[ei] = 1;
				}
				self(self, v, u);
				mindepth[u] = min(mindepth[v], mindepth[u]);
			}
		}
	};
	for (int i = 0; i < n; ++i) {
		if (depth[i] != -1) continue;
		dfs(dfs, i, i);
	}
}

struct UF {
	vi p;
	UF(int n) : p(n, -1) {
	}
	int find(int u) {
		while (p[u] >= 0) u = p[u];
		return u;
	}
	int same(int a, int b) {
		a = find(a);
		b = find(b);
		return a == b;
	}
	void unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) return;
		if (-p[a] > -p[b]) {
			p[a] += p[b];
			p[b] = a;
		} else {
			p[b] += p[a];
			p[a] = b;
		}
	}
};

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

	cin >> n >> m;

	vector<pii> edges;

	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back({b, i});
		g[b].push_back({a, i});
		edges.push_back({a, b});
	}

	vi vis(n);
	vi seen;
	auto dfs = [&](auto self, int i) -> void {
		if (vis[i]) return;
		vis[i] = 1;
		seen.push_back(i);
		for (auto [j, ei] : g[i]) {
			if (seen_as_bridge[ei]) continue;
			self(self, j);
		}
	};

	UF oneuf(n);

	vector<vi> ones;
	vis = vi(n, 0);
	for (int i = 0; i < n; ++i) {
		if (vis[i]) continue;
		seen.clear();
		dfs(dfs, i);
		ones.push_back(seen);
		for (int j = 0; j + 1 < (int)seen.size(); ++j) {
			oneuf.unite(seen[j], seen[j + 1]);
		}
	}

	components(-1);

	for (int i = 0; i < m; ++i) {
		components(i);
	}

	UF twouf(n);

	vector<vi> twos;
	vis = vi(n, 0);
	for (int i = 0; i < n; ++i) {
		if (vis[i]) continue;
		seen.clear();
		dfs(dfs, i);
		twos.push_back(seen);
		for (int j = 0; j + 1 < (int)seen.size(); ++j) {
			twouf.unite(seen[j], seen[j + 1]);
		}
	}


	UF uf1(n), uf2(n), uf3(n);
	vector<pii> ans;
	for (auto [a, b] : edges) {
		if (!uf1.same(a, b)) {
			ans.emplace_back(a, b);
			uf1.unite(a, b);
		} else if (!uf2.same(a, b)) {
			ans.emplace_back(a, b);
			uf2.unite(a, b);
		}else if (!uf3.same(a, b)) {
			ans.emplace_back(a, b);
			uf3.unite(a, b);
		}
	}

	cout << ans.size() << endl;
	for (auto [a, b] : ans) cout << a+1 << ' ' << b+1 << '\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
5 8
1 2
1 3
1 4
2 3
...

correct output
8
1 2
1 3
1 4
2 3
...

user output
8
1 2
1 3
1 4
2 3
...

Test 2

Verdict: ACCEPTED

input
100 2451
1 2
1 3
1 4
1 5
...

correct output
289
1 2
1 3
1 4
1 5
...

user output
289
1 2
1 3
1 4
1 5
...

Test 3

Verdict: ACCEPTED

input
100 1206
1 2
26 27
51 52
76 77
...

correct output
282
1 2
1 3
1 4
1 5
...

user output
282
1 2
26 27
51 52
76 77
...

Test 4

Verdict: ACCEPTED

input
5 8
2 3
1 3
1 5
5 4
...

correct output
8
1 3
1 5
2 1
2 3
...

user output
8
2 3
1 3
1 5
5 4
...

Test 5

Verdict: ACCEPTED

input
10 10
1 2
2 5
7 10
5 4
...

correct output
10
1 2
1 6
2 5
2 10
...

user output
10
1 2
2 5
7 10
5 4
...

Test 6

Verdict: ACCEPTED

input
10 45
1 6
1 7
10 6
4 6
...

correct output
27
1 3
1 6
1 7
1 9
...

user output
27
1 6
1 7
10 6
4 6
...

Test 7

Verdict: ACCEPTED

input
100 45
42 4
39 75
99 87
9 65
...

correct output
45
1 13
4 96
7 26
9 65
...

user output
45
42 4
39 75
99 87
9 65
...

Test 8

Verdict: ACCEPTED

input
100 400
19 66
48 11
34 85
22 9
...

correct output
296
1 27
1 38
1 63
2 44
...

user output
296
19 66
48 11
34 85
22 9
...

Test 9

Verdict: ACCEPTED

input
100 500
17 75
80 65
89 16
65 73
...

correct output
297
1 55
1 62
1 75
1 78
...

user output
297
17 75
80 65
89 16
65 73
...

Test 10

Verdict: ACCEPTED

input
1000 500
989 549
7 137
716 180
20 265
...

correct output
500
1 931
7 137
11 781
14 305
...

user output
500
989 549
7 137
716 180
20 265
...

Test 11

Verdict: ACCEPTED

input
1000 5000
911 615
730 255
283 617
821 101
...

correct output
2992
1 359
1 579
1 688
1 826
...

user output
2992
911 615
730 255
283 617
821 101
...

Test 12

Verdict: ACCEPTED

input
1000 10000
116 615
301 287
869 274
717 549
...

correct output
2997
1 165
1 496
1 661
2 22
...

user output
2997
116 615
301 287
869 274
717 549
...

Test 13

Verdict: ACCEPTED

input
1000 1000
395 816
73 640
124 192
920 419
...

correct output
1000
2 903
3 634
4 959
5 274
...

user output
1000
395 816
73 640
124 192
920 419
...

Test 14

Verdict: ACCEPTED

input
100 4000
18 66
63 12
50 12
48 78
...

correct output
297
1 8
1 34
1 40
1 43
...

user output
297
18 66
63 12
50 12
48 78
...

Test 15

Verdict: ACCEPTED

input
1 0

correct output
0

user output
0

Test 16

Verdict: ACCEPTED

input
10 0

correct output
0

user output
0

Test 17

Verdict: ACCEPTED

input
100 0

correct output
0

user output
0

Test 18

Verdict: ACCEPTED

input
1000 0

correct output
0

user output
0

Test 19

Verdict: ACCEPTED

input
5 6
5 1
4 1
5 2
4 2
...

correct output
6
4 1
4 2
4 3
5 1
...

user output
6
5 1
4 1
5 2
4 2
...

Test 20

Verdict: ACCEPTED

input
9 12
6 8
7 8
6 5
7 5
...

correct output
12
6 2
6 5
6 8
7 1
...

user output
12
6 8
7 8
6 5
7 5
...

Test 21

Verdict: ACCEPTED

input
37 54
37 31
14 31
37 15
14 15
...

correct output
54
6 2
6 7
6 19
6 30
...

user output
54
37 31
14 31
37 15
14 15
...

Test 22

Verdict: ACCEPTED

input
397 594
370 81
16 81
370 215
16 215
...

correct output
594
4 19
4 31
4 60
4 110
...

user output
594
370 81
16 81
370 215
16 215
...

Test 23

Verdict: ACCEPTED

input
997 1494
555 594
295 594
555 422
295 422
...

correct output
1494
6 214
6 280
6 455
6 472
...

user output
1494
555 594
295 594
555 422
295 422
...

Test 24

Verdict: ACCEPTED

input
6 8
5 3
6 3
5 4
6 4
...

correct output
8
5 1
5 2
5 3
5 4
...

user output
8
5 3
6 3
5 4
6 4
...

Test 25

Verdict: ACCEPTED

input
11 16
6 5
11 5
6 3
11 3
...

correct output
16
2 1
2 7
2 8
2 9
...

user output
16
6 5
11 5
6 3
11 3
...

Test 26

Verdict: ACCEPTED

input
46 72
20 46
33 46
20 15
33 15
...

correct output
72
2 4
2 5
2 10
2 11
...

user output
72
20 46
33 46
20 15
33 15
...

Test 27

Verdict: ACCEPTED

input
496 792
129 244
128 244
129 230
128 230
...

correct output
792
3 15
3 29
3 428
3 447
...

user output
792
129 244
128 244
129 230
128 230
...

Test 28

Verdict: ACCEPTED

input
996 1592
940 446
864 446
940 50
864 50
...

correct output
1592
2 362
2 482
2 545
2 572
...

user output
1592
940 446
864 446
940 50
864 50
...

Test 29

Verdict: ACCEPTED

input
3 2
3 1
2 1

correct output
2
2 1
3 1

user output
2
3 1
2 1

Test 30

Verdict: ACCEPTED

input
5 4
5 3
1 3
1 2
4 2

correct output
4
1 2
1 3
4 2
5 3

user output
4
5 3
1 3
1 2
4 2

Test 31

Verdict: ACCEPTED

input
19 18
5 2
1 2
1 4
15 4
...

correct output
18
1 2
1 4
5 2
7 10
...

user output
18
5 2
1 2
1 4
15 4
...

Test 32

Verdict: ACCEPTED

input
199 198
20 93
53 93
53 7
152 7
...

correct output
198
3 44
3 128
5 18
5 184
...

user output
198
20 93
53 93
53 7
152 7
...

Test 33

Verdict: ACCEPTED

input
999 998
481 471
242 471
242 95
525 95
...

correct output
998
2 267
2 303
3 378
3 644
...

user output
998
481 471
242 471
242 95
525 95
...

Test 34

Verdict: ACCEPTED

input
4 4
2 4
1 4
2 3
1 3

correct output
4
1 3
1 4
2 3
2 4

user output
4
2 4
1 4
2 3
1 3

Test 35

Verdict: ACCEPTED

input
7 8
2 7
4 7
2 5
4 5
...

correct output
8
2 5
2 7
4 1
4 3
...

user output
8
2 7
4 7
2 5
4 5
...

Test 36

Verdict: ACCEPTED

input
28 36
22 9
24 9
22 5
24 5
...

correct output
36
3 8
3 18
3 21
3 27
...

user output
36
22 9
24 9
22 5
24 5
...

Test 37

Verdict: ACCEPTED

input
298 396
293 290
133 290
293 228
133 228
...

correct output
396
1 62
1 112
1 174
1 250
...

user output
396
293 290
133 290
293 228
133 228
...

Test 38

Verdict: ACCEPTED

input
1000 1332
949 882
156 882
949 515
156 515
...

correct output
1332
2 508
2 774
2 885
2 921
...

user output
1332
949 882
156 882
949 515
156 515
...