#include "parks.h"
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int uf[200006];
int _find(int x) {
if (uf[x] == -1) return x;
return uf[x] = _find(uf[x]);
}
void _merge(int x, int y) {
x = _find(x), y = _find(y);
if (x != y) uf[x] = y;
}
map<pair<int, int>, int> m;
set<pair<int, int>> s;
int construct_roads(vector<int> x, vector<int> y) {
if ((int)x.size() == 1) return build({}, {}, {}, {}), 1;
int n = (int)x.size();
for (int i = 0; i < n; i++) uf[i] = -1;
vector<int> u, v, a, b;
vector<pair<int, int>> edges, n_edges;
vector<int> points;
for (int i = 0; i < n; i++) m[{ x[i], y[i] }] = i;
for (int i = 0; i < n; i++) if (m.count({ x[i], y[i] - 2 })) {
edges.push_back({ m[{ x[i], y[i] - 2 }], i });
_merge(edges.back().first, edges.back().second);
}
for (int i = 0; i < n; i++) points.push_back(i);
sort(points.begin(), points.end(), [&](const int &a, const int &b) {
return y[a] < y[b];
});
for (int t = 0; t < n; t++) {
int i = points[t];
if (m.count({ x[i] - 2, y[i] })) {
int t = m[{ x[i] - 2, y[i] }];
if (_find(t) != _find(i)) {
edges.push_back({ t, i });
_merge(t, i);
}
}
}
for (int i = 1; i < n; i++) if (_find(0) != _find(i)) return 0;
for (auto &i: edges) {
if (x[i.first] == 2 && x[i.second] == 2) {
u.push_back(i.first);
v.push_back(i.second);
a.push_back(1);
b.push_back((y[i.first] + y[i.second]) / 2);
s.insert({ a.back(), b.back() });
} else if (x[i.first] == 4 && x[i.second] == 4) {
u.push_back(i.first);
v.push_back(i.second);
a.push_back(5);
b.push_back((y[i.first] + y[i.second]) / 2);
s.insert({ a.back(), b.back() });
} else n_edges.push_back(i);
}
sort(n_edges.begin(), n_edges.end(), [&](const pair<int, int> &a, const pair<int, int> &b) {
return min(y[a.first], y[a.second]) < min(y[b.first], y[b.second]);
});
for (auto &i: n_edges) {
if (x[i.first] == x[i.second]) {
if (!s.count({ x[i.first] - 1, (y[i.first] + y[i.second]) / 2 })) {
s.insert({ x[i.first] - 1, (y[i.first] + y[i.second]) / 2 });
u.push_back(i.first);
v.push_back(i.second);
a.push_back(x[i.first] - 1);
b.push_back((y[i.first] + y[i.second]) / 2);
} else {
if (s.count({ x[i.first] + 1, (y[i.first] + y[i.second]) / 2 })) return 0;
s.insert({ x[i.first] + 1, (y[i.first] + y[i.second]) / 2 });
u.push_back(i.first);
v.push_back(i.second);
a.push_back(x[i.first] + 1);
b.push_back((y[i.first] + y[i.second]) / 2);
}
} else {
if (!s.count({ (x[i.first] + x[i.second]) / 2, y[i.first] - 1 })) {
s.insert({ (x[i.first] + x[i.second]) / 2, y[i.first] - 1 });
u.push_back(i.first);
v.push_back(i.second);
a.push_back((x[i.first] + x[i.second]) / 2);
b.push_back(y[i.first] - 1);
} else {
if (s.count({ (x[i.first] + x[i.second]) / 2, y[i.first] + 1 })) return 0;
s.insert({ (x[i.first] + x[i.second]) / 2, y[i.first] + 1 });
u.push_back(i.first);
v.push_back(i.second);
a.push_back((x[i.first] + x[i.second]) / 2);
b.push_back(y[i.first] + 1);
}
}
}
build(u, v, a, b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
615 ms |
39148 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
3 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
4 ms |
548 KB |
Output is correct |
28 |
Correct |
172 ms |
15784 KB |
Output is correct |
29 |
Correct |
286 ms |
23516 KB |
Output is correct |
30 |
Correct |
426 ms |
31656 KB |
Output is correct |
31 |
Correct |
662 ms |
39224 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
460 KB |
Output is correct |
45 |
Correct |
222 ms |
20012 KB |
Output is correct |
46 |
Correct |
370 ms |
29108 KB |
Output is correct |
47 |
Correct |
357 ms |
28948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
615 ms |
39148 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
3 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
4 ms |
548 KB |
Output is correct |
28 |
Correct |
172 ms |
15784 KB |
Output is correct |
29 |
Correct |
286 ms |
23516 KB |
Output is correct |
30 |
Correct |
426 ms |
31656 KB |
Output is correct |
31 |
Correct |
662 ms |
39224 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
460 KB |
Output is correct |
45 |
Correct |
222 ms |
20012 KB |
Output is correct |
46 |
Correct |
370 ms |
29108 KB |
Output is correct |
47 |
Correct |
357 ms |
28948 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Incorrect |
0 ms |
204 KB |
Solution announced impossible, but it is possible. |
52 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Incorrect |
528 ms |
33392 KB |
Solution announced impossible, but it is possible. |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
17 |
Correct |
620 ms |
39288 KB |
Output is correct |
18 |
Correct |
620 ms |
39456 KB |
Output is correct |
19 |
Incorrect |
600 ms |
27320 KB |
Solution announced impossible, but it is possible. |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
208 ms |
19556 KB |
Output is correct |
10 |
Correct |
12 ms |
2492 KB |
Output is correct |
11 |
Correct |
71 ms |
11304 KB |
Output is correct |
12 |
Correct |
18 ms |
3536 KB |
Output is correct |
13 |
Correct |
32 ms |
4988 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
216 ms |
19656 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
615 ms |
39148 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
3 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
4 ms |
548 KB |
Output is correct |
28 |
Correct |
172 ms |
15784 KB |
Output is correct |
29 |
Correct |
286 ms |
23516 KB |
Output is correct |
30 |
Correct |
426 ms |
31656 KB |
Output is correct |
31 |
Correct |
662 ms |
39224 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
460 KB |
Output is correct |
45 |
Correct |
222 ms |
20012 KB |
Output is correct |
46 |
Correct |
370 ms |
29108 KB |
Output is correct |
47 |
Correct |
357 ms |
28948 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Incorrect |
0 ms |
204 KB |
Solution announced impossible, but it is possible. |
52 |
Halted |
0 ms |
0 KB |
- |