#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
int ufs[200005];
int ge(int x) {
if (ufs[x] == -1)
return x;
return ufs[x] = ge(ufs[x]);
}
vector<pair<int, int>> pts;
map<pair<int, int>, int> idx;
vector<pair<int, int>> elist;
int n;
mt19937 rng(108616);
vector<int> bpt[200005];
int mach[800005];
int vis[200005];
int gloti = 0;
bool dfs(int u, int ti) {
vis[u] = ti;
for (int v : bpt[u]) {
if (mach[v] == -1 || ((vis[mach[v]] != ti) && dfs(mach[v], ti))) {
mach[v] = u;
return 1;
}
}
return 0;
}
bool hasmxm(int cnt) {
for (int i = 0; i < cnt; i++) {
gloti++;
if (!dfs(i, gloti))
return 0;
}
return 1;
}
bool ranAttempt() {
shuffle(elist.begin(), elist.end(), rng);
memset(ufs, -1, sizeof ufs);
vector<pair<int, int>> mst;
for (auto [u, v] : elist) {
if (ge(u) != ge(v)) {
ufs[ge(u)] = ge(v);
mst.push_back({u, v});
}
}
if (mst.size() != n - 1)
return 0;
map<pair<int, int>, int> benches;
int edid = 0, beid = 0;
for (auto [u, v] : mst) {
auto [x1, y1] = pts[u];
auto [x2, y2] = pts[v];
bpt[edid].clear();
if (x1 == x2) {
int c = min(y1, y2);
pair<int, int> p1 = {x1 - 1, c + 1}, p2 = {x1 + 1, c + 1};
if (!benches.count(p1))
benches[p1] = beid++;
if (!benches.count(p2))
benches[p2] = beid++;
bpt[edid].push_back(benches[p1]);
bpt[edid].push_back(benches[p2]);
} else {
int c = min(x1, x2);
pair<int, int> p1 = {c + 1, y1 + 1}, p2 = {c + 1, y1 - 1};
if (!benches.count(p1))
benches[p1] = beid++;
if (!benches.count(p2))
benches[p2] = beid++;
bpt[edid].push_back(benches[p1]);
bpt[edid].push_back(benches[p2]);
}
edid++;
}
for (int i = 0; i < beid; i++)
mach[i] = -1;
if (hasmxm(edid)) {
vector<int> U, V;
vector<int> mx(mst.size()), my(mst.size());
for (auto [u, v] : mst) {
U.push_back(u);
V.push_back(v);
}
for (auto [x, i] : benches) {
if (mach[i] == -1)
continue;
auto [zx, zy] = x;
mx[mach[i]] = zx;
my[mach[i]] = zy;
}
build(U, V, mx, my);
return 1;
}
return 0;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
n = x.size();
pts = vector<pair<int, int>>(n);
for (int i = 0; i < n; i++) {
pts[i] = {x[i], y[i]};
idx[pts[i]] = i;
}
for (int i = 0; i < n; i++) {
if (idx.count({x[i] + 2, y[i]}))
elist.push_back({i, idx[{x[i] + 2, y[i]}]});
if (idx.count({x[i], y[i] + 2}))
elist.push_back({i, idx[{x[i], y[i] + 2}]});
}
while (clock() < 3 * CLOCKS_PER_SEC) {
if (ranAttempt())
return 1;
}
return 0;
}
Compilation message
parks.cpp: In function 'bool ranAttempt()':
parks.cpp:52:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | if (mst.size() != n - 1)
| ~~~~~~~~~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
5 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
3000 ms |
5764 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
1006 ms |
62464 KB |
Output is correct |
24 |
Correct |
4 ms |
5764 KB |
Output is correct |
25 |
Correct |
6 ms |
6120 KB |
Output is correct |
26 |
Correct |
3000 ms |
6120 KB |
Output is correct |
27 |
Correct |
3001 ms |
6220 KB |
Output is correct |
28 |
Correct |
275 ms |
27620 KB |
Output is correct |
29 |
Correct |
470 ms |
39828 KB |
Output is correct |
30 |
Correct |
706 ms |
49556 KB |
Output is correct |
31 |
Correct |
997 ms |
62580 KB |
Output is correct |
32 |
Correct |
3 ms |
5708 KB |
Output is correct |
33 |
Correct |
4 ms |
5728 KB |
Output is correct |
34 |
Correct |
4 ms |
5708 KB |
Output is correct |
35 |
Correct |
3000 ms |
5760 KB |
Output is correct |
36 |
Correct |
3000 ms |
5760 KB |
Output is correct |
37 |
Correct |
3 ms |
5708 KB |
Output is correct |
38 |
Correct |
3 ms |
5708 KB |
Output is correct |
39 |
Correct |
3 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5708 KB |
Output is correct |
41 |
Correct |
3000 ms |
5760 KB |
Output is correct |
42 |
Correct |
3 ms |
5708 KB |
Output is correct |
43 |
Correct |
3001 ms |
5980 KB |
Output is correct |
44 |
Correct |
3001 ms |
6104 KB |
Output is correct |
45 |
Correct |
382 ms |
34324 KB |
Output is correct |
46 |
Correct |
646 ms |
46804 KB |
Output is correct |
47 |
Correct |
607 ms |
46788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
5 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
3000 ms |
5764 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
1006 ms |
62464 KB |
Output is correct |
24 |
Correct |
4 ms |
5764 KB |
Output is correct |
25 |
Correct |
6 ms |
6120 KB |
Output is correct |
26 |
Correct |
3000 ms |
6120 KB |
Output is correct |
27 |
Correct |
3001 ms |
6220 KB |
Output is correct |
28 |
Correct |
275 ms |
27620 KB |
Output is correct |
29 |
Correct |
470 ms |
39828 KB |
Output is correct |
30 |
Correct |
706 ms |
49556 KB |
Output is correct |
31 |
Correct |
997 ms |
62580 KB |
Output is correct |
32 |
Correct |
3 ms |
5708 KB |
Output is correct |
33 |
Correct |
4 ms |
5728 KB |
Output is correct |
34 |
Correct |
4 ms |
5708 KB |
Output is correct |
35 |
Correct |
3000 ms |
5760 KB |
Output is correct |
36 |
Correct |
3000 ms |
5760 KB |
Output is correct |
37 |
Correct |
3 ms |
5708 KB |
Output is correct |
38 |
Correct |
3 ms |
5708 KB |
Output is correct |
39 |
Correct |
3 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5708 KB |
Output is correct |
41 |
Correct |
3000 ms |
5760 KB |
Output is correct |
42 |
Correct |
3 ms |
5708 KB |
Output is correct |
43 |
Correct |
3001 ms |
5980 KB |
Output is correct |
44 |
Correct |
3001 ms |
6104 KB |
Output is correct |
45 |
Correct |
382 ms |
34324 KB |
Output is correct |
46 |
Correct |
646 ms |
46804 KB |
Output is correct |
47 |
Correct |
607 ms |
46788 KB |
Output is correct |
48 |
Correct |
4 ms |
5708 KB |
Output is correct |
49 |
Correct |
3 ms |
5708 KB |
Output is correct |
50 |
Correct |
3 ms |
5708 KB |
Output is correct |
51 |
Correct |
3 ms |
5708 KB |
Output is correct |
52 |
Correct |
4 ms |
5708 KB |
Output is correct |
53 |
Correct |
4 ms |
5708 KB |
Output is correct |
54 |
Correct |
4 ms |
5708 KB |
Output is correct |
55 |
Incorrect |
3387 ms |
51864 KB |
Solution announced impossible, but it is possible. |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
17 |
Correct |
3 ms |
5708 KB |
Output is correct |
18 |
Correct |
3 ms |
5708 KB |
Output is correct |
19 |
Correct |
3001 ms |
5764 KB |
Output is correct |
20 |
Correct |
844 ms |
58508 KB |
Output is correct |
21 |
Correct |
1085 ms |
62652 KB |
Output is correct |
22 |
Correct |
851 ms |
58236 KB |
Output is correct |
23 |
Correct |
744 ms |
60924 KB |
Output is correct |
24 |
Correct |
3025 ms |
22980 KB |
Output is correct |
25 |
Correct |
3026 ms |
28188 KB |
Output is correct |
26 |
Correct |
3033 ms |
28296 KB |
Output is correct |
27 |
Correct |
1040 ms |
70764 KB |
Output is correct |
28 |
Correct |
1012 ms |
70932 KB |
Output is correct |
29 |
Correct |
1016 ms |
70880 KB |
Output is correct |
30 |
Correct |
981 ms |
70844 KB |
Output is correct |
31 |
Correct |
3 ms |
5708 KB |
Output is correct |
32 |
Correct |
52 ms |
9732 KB |
Output is correct |
33 |
Correct |
3008 ms |
14392 KB |
Output is correct |
34 |
Correct |
1049 ms |
61932 KB |
Output is correct |
35 |
Correct |
3001 ms |
6928 KB |
Output is correct |
36 |
Correct |
3006 ms |
11548 KB |
Output is correct |
37 |
Correct |
3012 ms |
17340 KB |
Output is correct |
38 |
Correct |
285 ms |
27772 KB |
Output is correct |
39 |
Correct |
434 ms |
36208 KB |
Output is correct |
40 |
Correct |
640 ms |
44272 KB |
Output is correct |
41 |
Correct |
789 ms |
52304 KB |
Output is correct |
42 |
Correct |
1029 ms |
61012 KB |
Output is correct |
43 |
Correct |
4 ms |
5708 KB |
Output is correct |
44 |
Correct |
3 ms |
5708 KB |
Output is correct |
45 |
Correct |
4 ms |
5708 KB |
Output is correct |
46 |
Correct |
3000 ms |
5776 KB |
Output is correct |
47 |
Correct |
3000 ms |
5760 KB |
Output is correct |
48 |
Correct |
4 ms |
5708 KB |
Output is correct |
49 |
Correct |
3 ms |
5708 KB |
Output is correct |
50 |
Correct |
3 ms |
5708 KB |
Output is correct |
51 |
Correct |
3 ms |
5708 KB |
Output is correct |
52 |
Correct |
3000 ms |
5760 KB |
Output is correct |
53 |
Correct |
3 ms |
5708 KB |
Output is correct |
54 |
Correct |
3001 ms |
5964 KB |
Output is correct |
55 |
Correct |
3001 ms |
6096 KB |
Output is correct |
56 |
Correct |
378 ms |
34340 KB |
Output is correct |
57 |
Correct |
602 ms |
47020 KB |
Output is correct |
58 |
Correct |
610 ms |
46700 KB |
Output is correct |
59 |
Correct |
3000 ms |
5828 KB |
Output is correct |
60 |
Correct |
4 ms |
5708 KB |
Output is correct |
61 |
Correct |
3000 ms |
5764 KB |
Output is correct |
62 |
Correct |
946 ms |
70824 KB |
Output is correct |
63 |
Correct |
912 ms |
70828 KB |
Output is correct |
64 |
Correct |
900 ms |
70708 KB |
Output is correct |
65 |
Correct |
3001 ms |
6220 KB |
Output is correct |
66 |
Correct |
3001 ms |
6604 KB |
Output is correct |
67 |
Correct |
358 ms |
33844 KB |
Output is correct |
68 |
Correct |
635 ms |
47712 KB |
Output is correct |
69 |
Correct |
965 ms |
62204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
17 |
Correct |
910 ms |
71260 KB |
Output is correct |
18 |
Correct |
916 ms |
71500 KB |
Output is correct |
19 |
Correct |
1010 ms |
66588 KB |
Output is correct |
20 |
Correct |
947 ms |
60372 KB |
Output is correct |
21 |
Correct |
818 ms |
59020 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
93 ms |
14956 KB |
Output is correct |
24 |
Correct |
3003 ms |
8204 KB |
Output is correct |
25 |
Correct |
3010 ms |
14172 KB |
Output is correct |
26 |
Correct |
3012 ms |
19572 KB |
Output is correct |
27 |
Correct |
384 ms |
33944 KB |
Output is correct |
28 |
Correct |
530 ms |
42088 KB |
Output is correct |
29 |
Correct |
686 ms |
47988 KB |
Output is correct |
30 |
Correct |
833 ms |
55156 KB |
Output is correct |
31 |
Correct |
963 ms |
62264 KB |
Output is correct |
32 |
Correct |
937 ms |
64124 KB |
Output is correct |
33 |
Correct |
943 ms |
70976 KB |
Output is correct |
34 |
Correct |
3001 ms |
6348 KB |
Output is correct |
35 |
Correct |
3002 ms |
6820 KB |
Output is correct |
36 |
Correct |
383 ms |
34112 KB |
Output is correct |
37 |
Correct |
662 ms |
48116 KB |
Output is correct |
38 |
Correct |
952 ms |
62480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
3000 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
3000 ms |
5764 KB |
Output is correct |
7 |
Correct |
3001 ms |
5768 KB |
Output is correct |
8 |
Correct |
3000 ms |
5764 KB |
Output is correct |
9 |
Correct |
409 ms |
38316 KB |
Output is correct |
10 |
Correct |
26 ms |
9048 KB |
Output is correct |
11 |
Correct |
161 ms |
23148 KB |
Output is correct |
12 |
Correct |
42 ms |
10704 KB |
Output is correct |
13 |
Correct |
3003 ms |
10892 KB |
Output is correct |
14 |
Correct |
3000 ms |
5872 KB |
Output is correct |
15 |
Correct |
3001 ms |
5964 KB |
Output is correct |
16 |
Correct |
394 ms |
38328 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5708 KB |
Output is correct |
19 |
Correct |
5 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
3000 ms |
5764 KB |
Output is correct |
22 |
Correct |
4 ms |
5708 KB |
Output is correct |
23 |
Correct |
1006 ms |
62464 KB |
Output is correct |
24 |
Correct |
4 ms |
5764 KB |
Output is correct |
25 |
Correct |
6 ms |
6120 KB |
Output is correct |
26 |
Correct |
3000 ms |
6120 KB |
Output is correct |
27 |
Correct |
3001 ms |
6220 KB |
Output is correct |
28 |
Correct |
275 ms |
27620 KB |
Output is correct |
29 |
Correct |
470 ms |
39828 KB |
Output is correct |
30 |
Correct |
706 ms |
49556 KB |
Output is correct |
31 |
Correct |
997 ms |
62580 KB |
Output is correct |
32 |
Correct |
3 ms |
5708 KB |
Output is correct |
33 |
Correct |
4 ms |
5728 KB |
Output is correct |
34 |
Correct |
4 ms |
5708 KB |
Output is correct |
35 |
Correct |
3000 ms |
5760 KB |
Output is correct |
36 |
Correct |
3000 ms |
5760 KB |
Output is correct |
37 |
Correct |
3 ms |
5708 KB |
Output is correct |
38 |
Correct |
3 ms |
5708 KB |
Output is correct |
39 |
Correct |
3 ms |
5708 KB |
Output is correct |
40 |
Correct |
4 ms |
5708 KB |
Output is correct |
41 |
Correct |
3000 ms |
5760 KB |
Output is correct |
42 |
Correct |
3 ms |
5708 KB |
Output is correct |
43 |
Correct |
3001 ms |
5980 KB |
Output is correct |
44 |
Correct |
3001 ms |
6104 KB |
Output is correct |
45 |
Correct |
382 ms |
34324 KB |
Output is correct |
46 |
Correct |
646 ms |
46804 KB |
Output is correct |
47 |
Correct |
607 ms |
46788 KB |
Output is correct |
48 |
Correct |
4 ms |
5708 KB |
Output is correct |
49 |
Correct |
3 ms |
5708 KB |
Output is correct |
50 |
Correct |
3 ms |
5708 KB |
Output is correct |
51 |
Correct |
3 ms |
5708 KB |
Output is correct |
52 |
Correct |
4 ms |
5708 KB |
Output is correct |
53 |
Correct |
4 ms |
5708 KB |
Output is correct |
54 |
Correct |
4 ms |
5708 KB |
Output is correct |
55 |
Incorrect |
3387 ms |
51864 KB |
Solution announced impossible, but it is possible. |
56 |
Halted |
0 ms |
0 KB |
- |