Submission #436163

# Submission time Handle Problem Language Result Execution time Memory
436163 2021-06-24T09:43:07 Z Calico Fountain Parks (IOI21_parks) C++17
45 / 100
861 ms 35044 KB
#include <bits/stdc++.h>
#include "parks.h"
using namespace std;

struct DSU {
	vector<int> P, S;
	DSU(int n) {
		P.resize(n+1);
		S.resize(n+1, 1);
		for (int i = 1; i <= n; i++) P[i] = i;
	}
	
	int find(int x) {
		if (x == P[x]) return x;
		return P[x] = find(P[x]);
	}
	
	bool merge(int a, int b) {
		a = find(a); b = find(b);
		if (a == b) return 0;
		if (S[a] < S[b]) swap(a, b);
		
		S[a] += S[b];
		P[b] = a;
		return 1;
	}
	
	int same(int a, int b) {
		return find(a) == find(b);
	}
};

vector<int> dx = {-2, 2, 0, 0};
vector<int> dy = {0, 0, 2, -2};

bool ok(vector<int> X, vector<int> Y, vector<pair<int, int>> edges, vector<int> &u, vector<int> &v, vector<int> &a, vector<int> &b, int pr) {
	set<pair<int, int>> st;
	for (auto [i, j]: edges) {
		if (X[i] == X[j]) {
			// vertical 
			int x = X[i] - 1;
			int y = (Y[i] + Y[j])/2;

			if ((x/2 + y/2) % 2 == pr) {
				x = X[i] + 1;
			}

			if (st.find({x, y}) != st.end()) {
				u.clear(); v.clear(); a.clear(); b.clear();
				return 0;
			}

			u.push_back(i);
			v.push_back(j);
			a.push_back(x);
			b.push_back(y);

			st.insert({x, y});
		} else {
			// horizontal
			int x = (X[i] + X[j])/2;
			int y = Y[i]-1;

			if ((x/2 + y/2) % 2 != pr) {
				y = Y[i] + 1;
			}

			if (st.find({x, y}) != st.end()) {
				u.clear(); v.clear(); a.clear(); b.clear();
				return 0;
			}
			
			u.push_back(i);
			v.push_back(j);
			a.push_back(x);
			b.push_back(y);

			st.insert({x, y});
		}
	}

	return 1;
}

int construct_roads(vector<int> X, vector<int> Y) {
	int n = X.size();

	map<pair<int, int>, int> id;
	for (int i = 0; i < n; i++) {
		id[{X[i], Y[i]}] = i;
	}

	DSU d(n);
	int cc = n;

	vector<pair<int, int>> edges;
	for (int i = 0; i < n; i++) {
		if ((X[i]/2 + Y[i]/2) % 2) continue;
		for (int j = 0; j < 4; j++) {
			int x = X[i] + dx[j];
			int y = Y[i] + dy[j];

			if (id.find({x, y}) != id.end()) {
				int u = id[{x, y}];
				if (!d.same(i, u)) {
					edges.emplace_back(i, u);
					d.merge(i, u);
					cc--;
				}
			}
		}
	}
	if (cc != 1) return 0; 

	vector<int> u, v, a, b;
	if (ok(X, Y, edges, u, v, a, b, 0)) {
		build(u, v, a, b);
		return 1;
	}
	if (ok(X, Y, edges, u, v, a, b, 1)) {
		build(u, v, a, b);
		return 1;
	}

	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Incorrect 1 ms 204 KB Wrong answer detected in grader
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Incorrect 1 ms 204 KB Wrong answer detected in grader
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 696 ms 35044 KB Output is correct
21 Correct 703 ms 34940 KB Output is correct
22 Correct 741 ms 34916 KB Output is correct
23 Correct 568 ms 29916 KB Output is correct
24 Correct 537 ms 17600 KB Output is correct
25 Correct 585 ms 19648 KB Output is correct
26 Correct 463 ms 19648 KB Output is correct
27 Correct 714 ms 34952 KB Output is correct
28 Correct 739 ms 35000 KB Output is correct
29 Correct 795 ms 35036 KB Output is correct
30 Correct 767 ms 34936 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 28 ms 2928 KB Output is correct
33 Correct 164 ms 8944 KB Output is correct
34 Correct 661 ms 34876 KB Output is correct
35 Correct 11 ms 1356 KB Output is correct
36 Correct 75 ms 5192 KB Output is correct
37 Correct 174 ms 10028 KB Output is correct
38 Correct 248 ms 14308 KB Output is correct
39 Correct 365 ms 19296 KB Output is correct
40 Correct 446 ms 25396 KB Output is correct
41 Correct 656 ms 29932 KB Output is correct
42 Correct 853 ms 35020 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 2 ms 460 KB Output is correct
55 Correct 3 ms 588 KB Output is correct
56 Correct 255 ms 17708 KB Output is correct
57 Correct 437 ms 26108 KB Output is correct
58 Correct 511 ms 26096 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 722 ms 35020 KB Output is correct
63 Correct 648 ms 34940 KB Output is correct
64 Correct 647 ms 34868 KB Output is correct
65 Correct 4 ms 588 KB Output is correct
66 Correct 7 ms 988 KB Output is correct
67 Correct 318 ms 17608 KB Output is correct
68 Correct 562 ms 26676 KB Output is correct
69 Correct 861 ms 34924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
17 Correct 667 ms 34872 KB Output is correct
18 Correct 697 ms 34884 KB Output is correct
19 Correct 695 ms 34876 KB Output is correct
20 Correct 641 ms 34344 KB Output is correct
21 Correct 621 ms 30868 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 68 ms 5704 KB Output is correct
24 Correct 24 ms 2324 KB Output is correct
25 Correct 151 ms 7360 KB Output is correct
26 Correct 316 ms 11724 KB Output is correct
27 Correct 314 ms 17664 KB Output is correct
28 Correct 516 ms 21868 KB Output is correct
29 Correct 592 ms 26708 KB Output is correct
30 Correct 719 ms 30932 KB Output is correct
31 Correct 851 ms 34940 KB Output is correct
32 Correct 721 ms 35044 KB Output is correct
33 Correct 698 ms 35004 KB Output is correct
34 Correct 5 ms 716 KB Output is correct
35 Correct 9 ms 1228 KB Output is correct
36 Correct 270 ms 17704 KB Output is correct
37 Correct 477 ms 26676 KB Output is correct
38 Correct 744 ms 34916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 231 ms 17696 KB Output is correct
10 Correct 14 ms 2172 KB Output is correct
11 Correct 81 ms 9648 KB Output is correct
12 Correct 21 ms 3052 KB Output is correct
13 Correct 36 ms 4732 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 460 KB Output is correct
16 Correct 232 ms 17580 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Incorrect 1 ms 204 KB Wrong answer detected in grader
20 Halted 0 ms 0 KB -