#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, 0, 2, 0};
vector<int> dy = {0, -2, 0, 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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 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 |
Incorrect |
1 ms |
204 KB |
Wrong answer detected in grader |
21 |
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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 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 |
Incorrect |
1 ms |
204 KB |
Wrong answer detected in grader |
21 |
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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 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 |
756 ms |
35012 KB |
Output is correct |
21 |
Correct |
726 ms |
35100 KB |
Output is correct |
22 |
Correct |
779 ms |
34988 KB |
Output is correct |
23 |
Correct |
618 ms |
29968 KB |
Output is correct |
24 |
Correct |
554 ms |
17508 KB |
Output is correct |
25 |
Correct |
625 ms |
19644 KB |
Output is correct |
26 |
Correct |
459 ms |
19704 KB |
Output is correct |
27 |
Correct |
738 ms |
34952 KB |
Output is correct |
28 |
Correct |
796 ms |
34896 KB |
Output is correct |
29 |
Correct |
843 ms |
34920 KB |
Output is correct |
30 |
Correct |
800 ms |
35068 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
28 ms |
2908 KB |
Output is correct |
33 |
Correct |
200 ms |
8884 KB |
Output is correct |
34 |
Correct |
643 ms |
35004 KB |
Output is correct |
35 |
Correct |
15 ms |
1356 KB |
Output is correct |
36 |
Correct |
67 ms |
5172 KB |
Output is correct |
37 |
Correct |
215 ms |
10020 KB |
Output is correct |
38 |
Correct |
203 ms |
14272 KB |
Output is correct |
39 |
Correct |
375 ms |
19232 KB |
Output is correct |
40 |
Correct |
544 ms |
25456 KB |
Output is correct |
41 |
Correct |
672 ms |
29880 KB |
Output is correct |
42 |
Correct |
987 ms |
34908 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 |
3 ms |
460 KB |
Output is correct |
55 |
Correct |
3 ms |
588 KB |
Output is correct |
56 |
Correct |
276 ms |
17632 KB |
Output is correct |
57 |
Correct |
534 ms |
26116 KB |
Output is correct |
58 |
Correct |
488 ms |
26100 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 |
746 ms |
34992 KB |
Output is correct |
63 |
Correct |
736 ms |
34996 KB |
Output is correct |
64 |
Correct |
656 ms |
34816 KB |
Output is correct |
65 |
Correct |
4 ms |
688 KB |
Output is correct |
66 |
Correct |
10 ms |
1024 KB |
Output is correct |
67 |
Correct |
285 ms |
17764 KB |
Output is correct |
68 |
Correct |
629 ms |
26680 KB |
Output is correct |
69 |
Correct |
797 ms |
35068 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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 KB |
Output is correct |
17 |
Correct |
687 ms |
35008 KB |
Output is correct |
18 |
Correct |
721 ms |
34996 KB |
Output is correct |
19 |
Correct |
732 ms |
35108 KB |
Output is correct |
20 |
Correct |
700 ms |
34344 KB |
Output is correct |
21 |
Correct |
719 ms |
30900 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
67 ms |
5768 KB |
Output is correct |
24 |
Correct |
30 ms |
2392 KB |
Output is correct |
25 |
Correct |
115 ms |
7408 KB |
Output is correct |
26 |
Correct |
238 ms |
11688 KB |
Output is correct |
27 |
Correct |
283 ms |
17616 KB |
Output is correct |
28 |
Correct |
453 ms |
21832 KB |
Output is correct |
29 |
Correct |
623 ms |
26708 KB |
Output is correct |
30 |
Correct |
686 ms |
30724 KB |
Output is correct |
31 |
Correct |
868 ms |
35104 KB |
Output is correct |
32 |
Correct |
673 ms |
34896 KB |
Output is correct |
33 |
Correct |
668 ms |
35048 KB |
Output is correct |
34 |
Correct |
5 ms |
716 KB |
Output is correct |
35 |
Correct |
9 ms |
1228 KB |
Output is correct |
36 |
Correct |
335 ms |
17624 KB |
Output is correct |
37 |
Correct |
541 ms |
26656 KB |
Output is correct |
38 |
Correct |
806 ms |
35000 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 |
241 ms |
17644 KB |
Output is correct |
10 |
Correct |
14 ms |
2044 KB |
Output is correct |
11 |
Correct |
84 ms |
9612 KB |
Output is correct |
12 |
Correct |
21 ms |
3044 KB |
Output is correct |
13 |
Correct |
38 ms |
4688 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
234 ms |
17576 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 |
Incorrect |
1 ms |
204 KB |
Wrong answer detected in grader |
21 |
Halted |
0 ms |
0 KB |
- |