#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define all(x) (x).begin(), (x).end()
struct dsu {
vector<int> p, sz;
dsu(int n) {
p.assign(n, 0);
sz.assign(n, 1);
iota(p.begin(), p.end(), 0);
}
int find(int x) {
return (x == p[x] ? x : p[x] = find(p[x]));
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
if (sz[x] > sz[y]) swap(x, y);
p[x] = y;
sz[y] += sz[x];
return true;
}
};
int construct_roads(vector<int> x, vector<int> y) {
int n = (int)x.size();
if (n == 1) {
build({}, {}, {}, {});
return 1;
}
map<pair<int, int>, int> id;
for (int i = 0; i < n; i++) id[MP(x[i], y[i])] = i;
vector<int> U, V, A, B;
vector<int> perm(n);
iota(all(perm), 0);
sort(all(perm), [&](int i, int j){
return MP(x[i], y[i]) < MP(x[j], y[j]);
});
dsu d(n);
for (int i : perm){
if (id.find(MP(x[i], y[i] + 2)) == id.end()) continue;
int j = id[MP(x[i], y[i] + 2)];
if (d.unite(i, j)){
U.push_back(i);
V.push_back(j);
}
}
for (int i : perm) {
if (id.find(MP(x[i] + 2, y[i])) == id.end()) continue;
int j = id[MP(x[i] + 2, y[i])];
if (d.unite(i, j)) {
U.push_back(i);
V.push_back(j);
}
}
if (U.size() != n - 1) return 0;
A.resize(n - 1);
B.resize(n - 1);
set<pair<int, int>> benches;
int k = 0;
for (int it = 0; it < n - 1; it++){
int i = U[it], j = V[it];
if (x[i] == x[j]){
B[it] = (y[i] + y[j]) / 2;
if (x[i] == 2 || x[i] == 6){
A[it] = (x[i] == 2 ? 1 : 7);
} else {
if (k){
A[it] = x[i] - 1;
} else {
A[it] = x[i] + 1;
}
k ^= 1;
}
benches.insert(MP(A[it], B[it]));
}
}
for (int it = 0; it < n - 1; it++) {
int i = U[it], j = V[it];
if (y[i] == y[j]) {
int xx = (x[i] + x[j]) / 2;
A[it] = xx;
if (!benches.count(MP(xx, y[i] + 1))){
B[it] = y[i] + 1;
} else
if (!benches.count(MP(xx, y[i] - 1))){
B[it] = y[i] - 1;
} else {
assert(false);
}
benches.insert(MP(A[it], B[it]));
}
}
build(U, V, A, B);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:68:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if (U.size() != n - 1) return 0;
| ~~~~~~~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
369 ms |
38712 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
744 KB |
Output is correct |
28 |
Correct |
119 ms |
15608 KB |
Output is correct |
29 |
Correct |
200 ms |
23424 KB |
Output is correct |
30 |
Correct |
279 ms |
31080 KB |
Output is correct |
31 |
Correct |
402 ms |
38748 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
304 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
164 ms |
19444 KB |
Output is correct |
46 |
Correct |
242 ms |
28172 KB |
Output is correct |
47 |
Correct |
231 ms |
28168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
369 ms |
38712 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
744 KB |
Output is correct |
28 |
Correct |
119 ms |
15608 KB |
Output is correct |
29 |
Correct |
200 ms |
23424 KB |
Output is correct |
30 |
Correct |
279 ms |
31080 KB |
Output is correct |
31 |
Correct |
402 ms |
38748 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
304 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
164 ms |
19444 KB |
Output is correct |
46 |
Correct |
242 ms |
28172 KB |
Output is correct |
47 |
Correct |
231 ms |
28168 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
300 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
304 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
387 ms |
38672 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
2 ms |
568 KB |
Output is correct |
58 |
Correct |
7 ms |
1468 KB |
Output is correct |
59 |
Correct |
9 ms |
1332 KB |
Output is correct |
60 |
Correct |
158 ms |
19448 KB |
Output is correct |
61 |
Correct |
236 ms |
26108 KB |
Output is correct |
62 |
Correct |
310 ms |
32056 KB |
Output is correct |
63 |
Correct |
359 ms |
38760 KB |
Output is correct |
64 |
Correct |
1 ms |
300 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
342 ms |
38688 KB |
Output is correct |
68 |
Correct |
350 ms |
38852 KB |
Output is correct |
69 |
Correct |
377 ms |
38540 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
71 |
Correct |
6 ms |
1080 KB |
Output is correct |
72 |
Correct |
178 ms |
19440 KB |
Output is correct |
73 |
Correct |
258 ms |
29112 KB |
Output is correct |
74 |
Correct |
376 ms |
38696 KB |
Output is correct |
75 |
Correct |
389 ms |
38732 KB |
Output is correct |
76 |
Correct |
392 ms |
38716 KB |
Output is correct |
77 |
Correct |
4 ms |
852 KB |
Output is correct |
78 |
Correct |
9 ms |
1236 KB |
Output is correct |
79 |
Correct |
152 ms |
19480 KB |
Output is correct |
80 |
Correct |
255 ms |
29088 KB |
Output is correct |
81 |
Correct |
434 ms |
38768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
17 |
Correct |
2 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Runtime error |
280 ms |
55384 KB |
Execution killed with signal 6 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
17 |
Correct |
372 ms |
39152 KB |
Output is correct |
18 |
Correct |
395 ms |
39256 KB |
Output is correct |
19 |
Runtime error |
277 ms |
55096 KB |
Execution killed with signal 6 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
170 ms |
19492 KB |
Output is correct |
10 |
Correct |
11 ms |
2292 KB |
Output is correct |
11 |
Correct |
74 ms |
10624 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
34 ms |
5152 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
144 ms |
19492 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
369 ms |
38712 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
744 KB |
Output is correct |
28 |
Correct |
119 ms |
15608 KB |
Output is correct |
29 |
Correct |
200 ms |
23424 KB |
Output is correct |
30 |
Correct |
279 ms |
31080 KB |
Output is correct |
31 |
Correct |
402 ms |
38748 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
304 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
164 ms |
19444 KB |
Output is correct |
46 |
Correct |
242 ms |
28172 KB |
Output is correct |
47 |
Correct |
231 ms |
28168 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
300 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
304 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
387 ms |
38672 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
2 ms |
568 KB |
Output is correct |
58 |
Correct |
7 ms |
1468 KB |
Output is correct |
59 |
Correct |
9 ms |
1332 KB |
Output is correct |
60 |
Correct |
158 ms |
19448 KB |
Output is correct |
61 |
Correct |
236 ms |
26108 KB |
Output is correct |
62 |
Correct |
310 ms |
32056 KB |
Output is correct |
63 |
Correct |
359 ms |
38760 KB |
Output is correct |
64 |
Correct |
1 ms |
300 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
342 ms |
38688 KB |
Output is correct |
68 |
Correct |
350 ms |
38852 KB |
Output is correct |
69 |
Correct |
377 ms |
38540 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
71 |
Correct |
6 ms |
1080 KB |
Output is correct |
72 |
Correct |
178 ms |
19440 KB |
Output is correct |
73 |
Correct |
258 ms |
29112 KB |
Output is correct |
74 |
Correct |
376 ms |
38696 KB |
Output is correct |
75 |
Correct |
389 ms |
38732 KB |
Output is correct |
76 |
Correct |
392 ms |
38716 KB |
Output is correct |
77 |
Correct |
4 ms |
852 KB |
Output is correct |
78 |
Correct |
9 ms |
1236 KB |
Output is correct |
79 |
Correct |
152 ms |
19480 KB |
Output is correct |
80 |
Correct |
255 ms |
29088 KB |
Output is correct |
81 |
Correct |
434 ms |
38768 KB |
Output is correct |
82 |
Correct |
2 ms |
300 KB |
Output is correct |
83 |
Correct |
1 ms |
296 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Runtime error |
280 ms |
55384 KB |
Execution killed with signal 6 |
86 |
Halted |
0 ms |
0 KB |
- |