Submission #1059626

# Submission time Handle Problem Language Result Execution time Memory
1059626 2024-08-15T06:26:16 Z Gromp15 Fountain Parks (IOI21_parks) C++17
30 / 100
1463 ms 93300 KB
#include <bits/stdc++.h>
#include "parks.h"
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

struct dsu {
	vector<int> p, size;
	dsu(int n) : p(n), size(n, 1) {
		iota(all(p), 0);
	}
	int get(int v) {
		return v == p[v] ? v : p[v] = get(p[v]);
	}
	bool merge(int a, int b) {
		a = get(a), b = get(b);
		if (a == b) return 0;
		if (size[a] < size[b]) swap(a, b);
		size[a] += size[b], p[b] = a;
		return 1;
	}
};

int construct_roads(std::vector<int> x, std::vector<int> y) {
	int n = sz(x);
	map<ar<int, 4>, int> mp;
	map<ar<int, 2>, int> idx;
	for (int i = 0; i < n; i++) idx[{x[i], y[i]}] = i;
	map<ar<int, 2>, int> adj;
	for (int i = 0; i < n; i++) {
		if (idx.count({x[i] + 2, y[i]})) {
			mp[{x[i], y[i], x[i] + 2, y[i]}] += 2;
			adj[{x[i] + 1, y[i] + 1}]++;
			adj[{x[i] + 1, y[i] - 1}]++;
		}
		if (idx.count({x[i], y[i] + 2})) {
			mp[{x[i], y[i], x[i], y[i] + 2}] += 2;
			adj[{x[i] + 1, y[i] + 1}]++;
			adj[{x[i] - 1, y[i] + 1}]++;
		}
	}
	set<pair<int, ar<int, 2>>> q;
	for (auto [x, y] : adj) q.insert({y, x});
	dsu D(n);
	vector<int> a(n-1), b(n-1), c(n-1), d(n-1);
	set<ar<int, 2>> used;
	int now = 0;
	while (q.size()) {
		auto [val, p] = *q.begin();
		q.erase(q.begin());
		if (val != adj[p]) {
			if (adj[p]) {
				q.insert({adj[p], p});
				continue;
			}
		}
		ar<int, 4> seg2{p[0] - 1, p[1] - 1, p[0] - 1, p[1] + 1};
		ar<int, 4> seg3{p[0] + 1, p[1] - 1, p[0] + 1, p[1] + 1};
		ar<int, 4> seg4{p[0] - 1, p[1] - 1, p[0] + 1, p[1] - 1};
		ar<int, 4> seg5{p[0] - 1, p[1] + 1, p[0] + 1, p[1] + 1};
		bool F = 0;
		for (auto& seg : {seg2, seg3, seg4, seg5}) if (mp.count(seg) && mp[seg] == 1) {
			int i1 = idx[{seg[0], seg[1]}], i2 = idx[{seg[2], seg[3]}];
			if (D.get(i1) == D.get(i2)) continue;
			F = 1;
			D.merge(i1, i2);
			a[now] = i1, b[now] = i2;
			c[now] = p[0], d[now] = p[1];
			now++;
			ar<int, 2> pt, pt2;
			if (seg[0] != seg[2]) {
				pt = {seg[0] + 1, seg[1] - 1};
				pt2 = {seg[0] + 1, seg[1] + 1};
			}
			else {
				pt = {seg[0] - 1, seg[1] + 1};
				pt2 = {seg[0] + 1, seg[1] + 1};
			}
			for (auto p : {pt, pt2}) adj[p]--;
			break;
		}
		if (!F) {
			for (auto& seg : {seg2, seg3, seg4, seg5}) if (mp.count(seg) && mp[seg] >= 2) {
				int i1 = idx[{seg[0], seg[1]}], i2 = idx[{seg[2], seg[3]}];
				if (D.get(i1) == D.get(i2)) continue;
				F = 1;
				D.merge(i1, i2);
				a[now] = i1, b[now] = i2;
				c[now] = p[0], d[now] = p[1];
				now++;
				ar<int, 2> pt, pt2;
				if (seg[0] != seg[2]) {
					pt = {seg[0] + 1, seg[1] - 1};
					pt2 = {seg[0] + 1, seg[1] + 1};
				}
				else {
					pt = {seg[0] - 1, seg[1] + 1};
					pt2 = {seg[0] + 1, seg[1] + 1};
				}
				for (auto p : {pt, pt2}) adj[p]--;
				break;
			}
		}
		if (F) for (auto& seg : {seg2, seg3, seg4, seg5}) mp[seg]--;
	}
	if (D.size[D.get(0)] != n) return 0;
	build(a, b, c, d);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1437 ms 92092 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 10 ms 1796 KB Output is correct
27 Correct 14 ms 2140 KB Output is correct
28 Correct 453 ms 36900 KB Output is correct
29 Correct 745 ms 55708 KB Output is correct
30 Correct 1160 ms 73552 KB Output is correct
31 Correct 1463 ms 91984 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 6 ms 1216 KB Output is correct
44 Correct 10 ms 1368 KB Output is correct
45 Correct 504 ms 42932 KB Output is correct
46 Correct 859 ms 62288 KB Output is correct
47 Correct 836 ms 62036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1437 ms 92092 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 10 ms 1796 KB Output is correct
27 Correct 14 ms 2140 KB Output is correct
28 Correct 453 ms 36900 KB Output is correct
29 Correct 745 ms 55708 KB Output is correct
30 Correct 1160 ms 73552 KB Output is correct
31 Correct 1463 ms 91984 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 6 ms 1216 KB Output is correct
44 Correct 10 ms 1368 KB Output is correct
45 Correct 504 ms 42932 KB Output is correct
46 Correct 859 ms 62288 KB Output is correct
47 Correct 836 ms 62036 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 1450 ms 84812 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 6 ms 1060 KB Output is correct
58 Correct 22 ms 2908 KB Output is correct
59 Correct 42 ms 4432 KB Output is correct
60 Correct 601 ms 42580 KB Output is correct
61 Correct 889 ms 57424 KB Output is correct
62 Correct 1186 ms 70256 KB Output is correct
63 Correct 1434 ms 84916 KB Output is correct
64 Correct 1 ms 344 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 1320 ms 92032 KB Output is correct
68 Correct 1294 ms 92240 KB Output is correct
69 Correct 1326 ms 91408 KB Output is correct
70 Correct 13 ms 1884 KB Output is correct
71 Correct 27 ms 3400 KB Output is correct
72 Correct 540 ms 42068 KB Output is correct
73 Correct 883 ms 62912 KB Output is correct
74 Correct 1269 ms 83972 KB Output is correct
75 Correct 1419 ms 88904 KB Output is correct
76 Correct 1340 ms 92048 KB Output is correct
77 Correct 21 ms 2396 KB Output is correct
78 Correct 33 ms 3928 KB Output is correct
79 Correct 543 ms 44104 KB Output is correct
80 Correct 923 ms 66132 KB Output is correct
81 Correct 1329 ms 88144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 869 ms 79596 KB Output is correct
21 Correct 1006 ms 79440 KB Output is correct
22 Correct 835 ms 79444 KB Output is correct
23 Correct 988 ms 78476 KB Output is correct
24 Correct 185 ms 22356 KB Output is correct
25 Correct 1210 ms 85024 KB Output is correct
26 Correct 1178 ms 84820 KB Output is correct
27 Correct 1216 ms 91296 KB Output is correct
28 Correct 1188 ms 91112 KB Output is correct
29 Correct 1260 ms 91216 KB Output is correct
30 Correct 1295 ms 91076 KB Output is correct
31 Correct 0 ms 600 KB Output is correct
32 Incorrect 52 ms 5716 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
17 Correct 1289 ms 92368 KB Output is correct
18 Correct 1247 ms 93300 KB Output is correct
19 Correct 896 ms 79440 KB Output is correct
20 Correct 1172 ms 77396 KB Output is correct
21 Correct 1158 ms 78932 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 131 ms 13904 KB Output is correct
24 Correct 76 ms 7760 KB Output is correct
25 Correct 321 ms 26448 KB Output is correct
26 Correct 611 ms 45140 KB Output is correct
27 Correct 529 ms 40784 KB Output is correct
28 Incorrect 675 ms 47188 KB Solution announced impossible, but it is possible.
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 352 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 594 ms 48916 KB Output is correct
10 Correct 38 ms 5200 KB Output is correct
11 Correct 283 ms 26488 KB Output is correct
12 Correct 60 ms 7528 KB Output is correct
13 Correct 206 ms 21804 KB Output is correct
14 Correct 3 ms 856 KB Output is correct
15 Correct 7 ms 1372 KB Output is correct
16 Correct 570 ms 48892 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1437 ms 92092 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 10 ms 1796 KB Output is correct
27 Correct 14 ms 2140 KB Output is correct
28 Correct 453 ms 36900 KB Output is correct
29 Correct 745 ms 55708 KB Output is correct
30 Correct 1160 ms 73552 KB Output is correct
31 Correct 1463 ms 91984 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 600 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 6 ms 1216 KB Output is correct
44 Correct 10 ms 1368 KB Output is correct
45 Correct 504 ms 42932 KB Output is correct
46 Correct 859 ms 62288 KB Output is correct
47 Correct 836 ms 62036 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 1450 ms 84812 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 6 ms 1060 KB Output is correct
58 Correct 22 ms 2908 KB Output is correct
59 Correct 42 ms 4432 KB Output is correct
60 Correct 601 ms 42580 KB Output is correct
61 Correct 889 ms 57424 KB Output is correct
62 Correct 1186 ms 70256 KB Output is correct
63 Correct 1434 ms 84916 KB Output is correct
64 Correct 1 ms 344 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 1320 ms 92032 KB Output is correct
68 Correct 1294 ms 92240 KB Output is correct
69 Correct 1326 ms 91408 KB Output is correct
70 Correct 13 ms 1884 KB Output is correct
71 Correct 27 ms 3400 KB Output is correct
72 Correct 540 ms 42068 KB Output is correct
73 Correct 883 ms 62912 KB Output is correct
74 Correct 1269 ms 83972 KB Output is correct
75 Correct 1419 ms 88904 KB Output is correct
76 Correct 1340 ms 92048 KB Output is correct
77 Correct 21 ms 2396 KB Output is correct
78 Correct 33 ms 3928 KB Output is correct
79 Correct 543 ms 44104 KB Output is correct
80 Correct 923 ms 66132 KB Output is correct
81 Correct 1329 ms 88144 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 869 ms 79596 KB Output is correct
86 Correct 1006 ms 79440 KB Output is correct
87 Correct 835 ms 79444 KB Output is correct
88 Correct 988 ms 78476 KB Output is correct
89 Correct 185 ms 22356 KB Output is correct
90 Correct 1210 ms 85024 KB Output is correct
91 Correct 1178 ms 84820 KB Output is correct
92 Correct 1216 ms 91296 KB Output is correct
93 Correct 1188 ms 91112 KB Output is correct
94 Correct 1260 ms 91216 KB Output is correct
95 Correct 1295 ms 91076 KB Output is correct
96 Correct 0 ms 600 KB Output is correct
97 Incorrect 52 ms 5716 KB Solution announced impossible, but it is possible.
98 Halted 0 ms 0 KB -