#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
struct Coord {
int x, y, i;
bool operator<(Coord other) const {
if (y == other.y) {
return x < other.x;
}
return y < other.y;
}
};
int n;
vector<Coord> coords;
std::vector<int> u, v, a, b;
vector<int> link;
int find(int x) {
if (link[x] != x) link[x] = find(link[x]);
return link[x];
}
void unite(int a, int b) {
int x = find(a), y = find(b);
link[x] = y;
}
bool same(int a, int b) {
return find(a) == find(b);
}
void connect(int i, int j) {
if (coords[i].x == coords[j].x) {
u.pb(coords[i].i);
v.pb(coords[j].i);
if (coords[i].x == 4)
a.pb(coords[i].x+1);
else
a.pb(coords[i].x-1);
b.pb(coords[i].y+1);
} else if (coords[i].y == coords[j].y) {
u.pb(coords[i].i);
v.pb(coords[j].i);
a.pb(coords[i].x+1);
b.pb(coords[i].y-1);
}
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
n = x.size();
int maxX = 0;
for (int i = 0; i < n; i++) maxX = max(maxX, x[i]);
bool res = true;
if (maxX <= 4) {
coords = vector<Coord>(n);
for (int i = 0; i < n; i++) {
coords[i] = {x[i], y[i], i};
}
sort(coords.begin(), coords.end());
link = vector<int>(n);
iota(link.begin(), link.end(), 0);
for (int i = 0; i < n-1; i++) {
if ((coords[i].y+2 == coords[i+1].y && coords[i].x == coords[i+1].x) || (coords[i].y == coords[i+1].y && abs(coords[i+1].x-coords[i].x) == 2)) {
connect(i, i+1);
unite(i, i+1);
}
if (i+2 < n && ((coords[i].y+2 == coords[i+2].y && coords[i].x == coords[i+2].x) || (coords[i].y == coords[i+2].y && abs(coords[i+2].x-coords[i].x) == 2))) {
connect(i, i+2);
unite(i, i+2);
}
}
for (int i = 0; i < n-1; i++) {
res &= find(i) == find(i+1);
}
} else {
int maxY = 0;
for (int i = 0; i < n; i++) maxY = max(maxY, y[i]);
link = vector<int>(n);
iota(link.begin(), link.end(), 0);
vector<vector<int>> mat(maxY/2, vector<int>(3)), matIdx(maxY/2, vector<int>(3));
for (int i = 0; i < n; i++) {
mat[y[i]/2-1][x[i]/2-1] = 1;
matIdx[y[i]/2-1][x[i]/2-1] = i;
}
map<int, map<int, int>> bench;
for (int i = 0; i < maxY/2; i++) {
if (mat[i][0] && mat[i][1]) {
if (bench[3][2*i+1]) {
if (!same(matIdx[i][0], matIdx[i][1])) {
if (bench[3][2*i+3]) res = false;
u.pb(matIdx[i][0]);
v.pb(matIdx[i][1]);
a.pb(3);
b.pb(2*i+3);
unite(matIdx[i][0], matIdx[i][1]);
}
} else {
u.pb(matIdx[i][0]);
v.pb(matIdx[i][1]);
a.pb(3);
b.pb(2*i+1);
unite(matIdx[i][0], matIdx[i][1]);
}
if (a.size()) {
bench[a[a.size()-1]][b[b.size()-1]] = true;
}
}
if (mat[i][1] && mat[i][2]) {
if (bench[5][2*i+1]) {
if (!same(matIdx[i][1], matIdx[i][2])) {
if (bench[5][2*i+3]) res = false;
u.pb(matIdx[i][1]);
v.pb(matIdx[i][2]);
a.pb(5);
b.pb(2*i+3);
unite(matIdx[i][1], matIdx[i][2]);
}
} else {
u.pb(matIdx[i][1]);
v.pb(matIdx[i][2]);
a.pb(5);
b.pb(2*i+1);
unite(matIdx[i][1], matIdx[i][2]);
}
if (a.size()) {
bench[a[a.size()-1]][b[b.size()-1]] = true;
}
}
if (i == maxY/2-1) continue;
bool isValid = false;
if (mat[i][0] && mat[i+1][0]) {
u.pb(matIdx[i][0]);
v.pb(matIdx[i+1][0]);
a.pb(1);
b.pb(2*i+3);
unite(matIdx[i][0], matIdx[i+1][0]);
isValid = true;
if (a.size()) {
bench[a[a.size()-1]][b[b.size()-1]] = true;
}
}
if (mat[i][2] && mat[i+1][2]) {
u.pb(matIdx[i][2]);
v.pb(matIdx[i+1][2]);
a.pb(7);
b.pb(2*i+3);
unite(matIdx[i][2], matIdx[i+1][2]);
isValid = true;
if (a.size()) {
bench[a[a.size()-1]][b[b.size()-1]] = true;
}
}
if (!isValid && mat[i][1] && mat[i+1][1]) {
u.pb(matIdx[i][1]);
v.pb(matIdx[i+1][1]);
if (!bench[3][2*i+3])
a.pb(3);
else if (!bench[5][2*i+3])
a.pb(5);
else
res = false;
b.pb(2*i+3);
unite(matIdx[i][1], matIdx[i+1][1]);
if (a.size()) {
bench[a[a.size()-1]][b[b.size()-1]] = true;
}
}
}
for (int i = 0; i < n-1; i++) {
res &= find(i) == find(i+1);
}
}
if (!res) return 0;
build(u, v, a, b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
149 ms |
23324 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
55 ms |
9496 KB |
Output is correct |
29 |
Correct |
84 ms |
14076 KB |
Output is correct |
30 |
Correct |
112 ms |
18696 KB |
Output is correct |
31 |
Correct |
141 ms |
23204 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
55 ms |
9172 KB |
Output is correct |
46 |
Correct |
80 ms |
13172 KB |
Output is correct |
47 |
Correct |
79 ms |
13204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
149 ms |
23324 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
55 ms |
9496 KB |
Output is correct |
29 |
Correct |
84 ms |
14076 KB |
Output is correct |
30 |
Correct |
112 ms |
18696 KB |
Output is correct |
31 |
Correct |
141 ms |
23204 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
55 ms |
9172 KB |
Output is correct |
46 |
Correct |
80 ms |
13172 KB |
Output is correct |
47 |
Correct |
79 ms |
13204 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
211 ms |
31140 KB |
Output is correct |
56 |
Correct |
12 ms |
10284 KB |
Output is correct |
57 |
Correct |
14 ms |
10680 KB |
Output is correct |
58 |
Correct |
10 ms |
5940 KB |
Output is correct |
59 |
Correct |
17 ms |
11248 KB |
Output is correct |
60 |
Correct |
95 ms |
15968 KB |
Output is correct |
61 |
Correct |
130 ms |
21048 KB |
Output is correct |
62 |
Correct |
174 ms |
30076 KB |
Output is correct |
63 |
Correct |
214 ms |
33488 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
167 ms |
30092 KB |
Output is correct |
68 |
Correct |
168 ms |
30620 KB |
Output is correct |
69 |
Correct |
171 ms |
30624 KB |
Output is correct |
70 |
Correct |
16 ms |
11260 KB |
Output is correct |
71 |
Correct |
16 ms |
11220 KB |
Output is correct |
72 |
Correct |
80 ms |
15012 KB |
Output is correct |
73 |
Correct |
127 ms |
25920 KB |
Output is correct |
74 |
Correct |
179 ms |
30476 KB |
Output is correct |
75 |
Correct |
194 ms |
31392 KB |
Output is correct |
76 |
Correct |
167 ms |
30196 KB |
Output is correct |
77 |
Correct |
18 ms |
11612 KB |
Output is correct |
78 |
Correct |
16 ms |
10552 KB |
Output is correct |
79 |
Correct |
80 ms |
15188 KB |
Output is correct |
80 |
Correct |
134 ms |
26272 KB |
Output is correct |
81 |
Correct |
187 ms |
32160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
17 |
Runtime error |
0 ms |
340 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
17 |
Runtime error |
48 ms |
19408 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
9396 KB |
Output is correct |
10 |
Correct |
5 ms |
1364 KB |
Output is correct |
11 |
Correct |
27 ms |
5280 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2880 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
55 ms |
9396 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
149 ms |
23324 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
55 ms |
9496 KB |
Output is correct |
29 |
Correct |
84 ms |
14076 KB |
Output is correct |
30 |
Correct |
112 ms |
18696 KB |
Output is correct |
31 |
Correct |
141 ms |
23204 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
55 ms |
9172 KB |
Output is correct |
46 |
Correct |
80 ms |
13172 KB |
Output is correct |
47 |
Correct |
79 ms |
13204 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
211 ms |
31140 KB |
Output is correct |
56 |
Correct |
12 ms |
10284 KB |
Output is correct |
57 |
Correct |
14 ms |
10680 KB |
Output is correct |
58 |
Correct |
10 ms |
5940 KB |
Output is correct |
59 |
Correct |
17 ms |
11248 KB |
Output is correct |
60 |
Correct |
95 ms |
15968 KB |
Output is correct |
61 |
Correct |
130 ms |
21048 KB |
Output is correct |
62 |
Correct |
174 ms |
30076 KB |
Output is correct |
63 |
Correct |
214 ms |
33488 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
167 ms |
30092 KB |
Output is correct |
68 |
Correct |
168 ms |
30620 KB |
Output is correct |
69 |
Correct |
171 ms |
30624 KB |
Output is correct |
70 |
Correct |
16 ms |
11260 KB |
Output is correct |
71 |
Correct |
16 ms |
11220 KB |
Output is correct |
72 |
Correct |
80 ms |
15012 KB |
Output is correct |
73 |
Correct |
127 ms |
25920 KB |
Output is correct |
74 |
Correct |
179 ms |
30476 KB |
Output is correct |
75 |
Correct |
194 ms |
31392 KB |
Output is correct |
76 |
Correct |
167 ms |
30196 KB |
Output is correct |
77 |
Correct |
18 ms |
11612 KB |
Output is correct |
78 |
Correct |
16 ms |
10552 KB |
Output is correct |
79 |
Correct |
80 ms |
15188 KB |
Output is correct |
80 |
Correct |
134 ms |
26272 KB |
Output is correct |
81 |
Correct |
187 ms |
32160 KB |
Output is correct |
82 |
Runtime error |
0 ms |
340 KB |
Execution killed with signal 11 |
83 |
Halted |
0 ms |
0 KB |
- |