Submission #1059616

# Submission time Handle Problem Language Result Execution time Memory
1059616 2024-08-15T06:16:07 Z Gromp15 Fountain Parks (IOI21_parks) C++17
15 / 100
1393 ms 78884 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, 4>>> q;
	for (auto [x, y] : mp) 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, seg] = *q.begin();
		q.erase(q.begin());
		if (mp[seg] != val) {
			if (mp[seg]) {
				q.insert({mp[seg], seg});
				continue;
			}
		}
		int i1 = idx[{seg[0], seg[1]}], i2 = idx[{seg[2], seg[3]}];
		if (D.get(i1) == D.get(i2)) continue;
		if (val) {
			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};
			}
			bool F = 0;
			for (auto p : {pt, pt2}) if (adj[p] == 1) {
				if (!used.count(p)) {
					F = 1;
					used.insert(p);
					D.merge(i1, i2);
					a[now] = i1, b[now] = i2;
					c[now] = p[0], d[now] = p[1];
					now++;
					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};
					if (mp.count(seg2)) mp[seg2]--;
					if (mp.count(seg3)) mp[seg3]--;
					if (mp.count(seg4)) mp[seg4]--;
					if (mp.count(seg5)) mp[seg5]--;
					break;
				}
			}
			if (!F) {
				for (auto p : {pt, pt2}) if (adj[p] == 2) {
					if (!used.count(p)) {
						F = 1;
						used.insert(p);
						D.merge(i1, i2);
						a[now] = i1, b[now] = i2;
						c[now] = p[0], d[now] = p[1];
						now++;
						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};
						if (mp.count(seg2)) mp[seg2]--;
						if (mp.count(seg3)) mp[seg3]--;
						if (mp.count(seg4)) mp[seg4]--;
						if (mp.count(seg5)) mp[seg5]--;
						break;
					}
				}
				assert(F);
			}
			for (auto p : {pt, pt2}) {
				adj[p]--;
			}
		}
	}
	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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 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 1393 ms 78884 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 8 ms 1628 KB Output is correct
27 Correct 16 ms 1884 KB Output is correct
28 Correct 415 ms 31752 KB Output is correct
29 Correct 708 ms 47456 KB Output is correct
30 Correct 970 ms 63232 KB Output is correct
31 Correct 1302 ms 78856 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 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 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 7 ms 1076 KB Output is correct
44 Correct 6 ms 1368 KB Output is correct
45 Correct 464 ms 34168 KB Output is correct
46 Correct 756 ms 49236 KB Output is correct
47 Correct 748 ms 49304 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 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 1393 ms 78884 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 8 ms 1628 KB Output is correct
27 Correct 16 ms 1884 KB Output is correct
28 Correct 415 ms 31752 KB Output is correct
29 Correct 708 ms 47456 KB Output is correct
30 Correct 970 ms 63232 KB Output is correct
31 Correct 1302 ms 78856 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 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 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 7 ms 1076 KB Output is correct
44 Correct 6 ms 1368 KB Output is correct
45 Correct 464 ms 34168 KB Output is correct
46 Correct 756 ms 49236 KB Output is correct
47 Correct 748 ms 49304 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 0 ms 440 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Runtime error 1 ms 604 KB Execution killed with signal 6
54 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 901 ms 63924 KB Output is correct
21 Correct 1140 ms 63824 KB Output is correct
22 Correct 1012 ms 63828 KB Output is correct
23 Correct 835 ms 64712 KB Output is correct
24 Correct 250 ms 22504 KB Output is correct
25 Correct 1171 ms 72256 KB Output is correct
26 Correct 962 ms 72432 KB Output is correct
27 Correct 1108 ms 75536 KB Output is correct
28 Correct 1052 ms 75448 KB Output is correct
29 Correct 1116 ms 75424 KB Output is correct
30 Correct 1130 ms 75336 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 44 ms 5420 KB Output is correct
33 Correct 79 ms 11712 KB Output is correct
34 Correct 994 ms 64044 KB Output is correct
35 Runtime error 27 ms 6992 KB Execution killed with signal 6
36 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 KB Output is correct
17 Correct 981 ms 76172 KB Output is correct
18 Correct 966 ms 76184 KB Output is correct
19 Correct 1078 ms 63824 KB Output is correct
20 Correct 1219 ms 70808 KB Output is correct
21 Correct 873 ms 65616 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 100 ms 12120 KB Output is correct
24 Correct 61 ms 7348 KB Output is correct
25 Runtime error 174 ms 48624 KB Execution killed with signal 6
26 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 353 ms 37956 KB Output is correct
10 Correct 21 ms 4172 KB Output is correct
11 Correct 145 ms 20564 KB Output is correct
12 Correct 32 ms 6228 KB Output is correct
13 Correct 110 ms 16464 KB Output is correct
14 Correct 2 ms 804 KB Output is correct
15 Correct 4 ms 1116 KB Output is correct
16 Correct 398 ms 38112 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 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 1393 ms 78884 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 8 ms 1628 KB Output is correct
27 Correct 16 ms 1884 KB Output is correct
28 Correct 415 ms 31752 KB Output is correct
29 Correct 708 ms 47456 KB Output is correct
30 Correct 970 ms 63232 KB Output is correct
31 Correct 1302 ms 78856 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 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 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 7 ms 1076 KB Output is correct
44 Correct 6 ms 1368 KB Output is correct
45 Correct 464 ms 34168 KB Output is correct
46 Correct 756 ms 49236 KB Output is correct
47 Correct 748 ms 49304 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 0 ms 440 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Runtime error 1 ms 604 KB Execution killed with signal 6
54 Halted 0 ms 0 KB -