#include "parks.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
typedef long long ll;
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
map<ar<int, 2>, int> mm;
for(int i=0;i<n;i++){
swap(x[i], y[i]);
mm[{x[i], y[i]}] = i;
}
vector<ar<int, 2>> e, p;
vector<int> par(n), sz(n, 1);
iota(par.begin(), par.end(), 0);
function<int(int)> f = [&](int x) { return (par[x] == x ? x : par[x] = f(par[x])); };
auto merge = [&](int a, int b){
a = f(a), b = f(b);
if(a == b) return false;
if(sz[a] < sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b];
return true;
};
for(int i=0;i<n;i++){
if(mm.count({x[i], y[i] - 2})){
int j = mm[{x[i], y[i] - 2}];
if(merge(i, j)){
e.push_back({j, i});
p.push_back({-1, -1});
}
}
}
for(int i=0;i<n;i++){
if(y[i] == 2 || y[i] == 6){
if(mm.count({x[i] - 2, y[i]})){
int j = mm[{x[i] - 2, y[i]}];
if(merge(i, j)){
e.push_back({j, i});
p.push_back({x[i] - 1, y[i] + (y[i] == 2 ? -1 : +1)});
}
}
}
}
for(int i=0;i<n;i++){
if(y[i] == 4){
if(mm.count({x[i] - 2, y[i]})){
int j = mm[{x[i] - 2, y[i]}];
if(merge(i, j)){
e.push_back({j, i});
p.push_back({-1, -1});
}
}
}
}
if(sz[f(0)] != n) return 0;
assert((int)e.size() == n - 1);
par.pop_back();
iota(par.begin(), par.end(), 0);
sort(par.begin(), par.end(), [&](int i, int j){
if(x[e[i][0]] != x[e[j][0]]){
return x[e[i][0]] < x[e[j][0]];
} else {
int a = (x[e[i][0]] != x[e[i][1]]), b = (x[e[j][0]] != x[e[j][1]]);
return a < b;
}
});
mm.clear();
for(auto i : par){
if(~p[i][0]) continue;
//~ cout<<e[i][0]<<" "<<e[i][1]<<"\n";
if(x[e[i][0]] == x[e[i][1]]){
int Y = (y[e[i][0]] + y[e[i][1]]) / 2;
p[i] = {x[e[i][0]] - 1, Y};
if(mm.count(p[i])){
p[i][0] += 2;
assert(!mm.count(p[i]));
}
mm[p[i]] = 1;
} else {
int X = (x[e[i][0]] + x[e[i][1]]) / 2;
p[i] = {X, y[e[i][0]] + 1};
if(mm.count(p[i])){
p[i][1] -= 2;
assert(!mm.count(p[i]));
}
mm[p[i]] = 1;
}
}
mm.clear();
vector<int> u(n - 1), v(n - 1), A(n - 1), B(n - 1);
for(int i=0;i+1<n;i++){
u[i] = e[i][0], v[i] = e[i][1], A[i] = p[i][0], B[i] = p[i][1];
swap(A[i], B[i]);
}
build(u, v, A, B);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 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 |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
0 ms |
304 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
532 ms |
22580 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
4 ms |
696 KB |
Output is correct |
28 |
Correct |
141 ms |
9472 KB |
Output is correct |
29 |
Correct |
262 ms |
13792 KB |
Output is correct |
30 |
Correct |
383 ms |
18816 KB |
Output is correct |
31 |
Correct |
524 ms |
22528 KB |
Output is correct |
32 |
Correct |
0 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
300 KB |
Output is correct |
38 |
Correct |
0 ms |
212 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 |
304 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 |
195 ms |
11544 KB |
Output is correct |
46 |
Correct |
345 ms |
17320 KB |
Output is correct |
47 |
Correct |
333 ms |
17324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 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 |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
0 ms |
304 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
532 ms |
22580 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
4 ms |
696 KB |
Output is correct |
28 |
Correct |
141 ms |
9472 KB |
Output is correct |
29 |
Correct |
262 ms |
13792 KB |
Output is correct |
30 |
Correct |
383 ms |
18816 KB |
Output is correct |
31 |
Correct |
524 ms |
22528 KB |
Output is correct |
32 |
Correct |
0 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
300 KB |
Output is correct |
38 |
Correct |
0 ms |
212 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 |
304 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 |
195 ms |
11544 KB |
Output is correct |
46 |
Correct |
345 ms |
17320 KB |
Output is correct |
47 |
Correct |
333 ms |
17324 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
304 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
566 ms |
22564 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
3 ms |
468 KB |
Output is correct |
58 |
Correct |
8 ms |
1044 KB |
Output is correct |
59 |
Correct |
10 ms |
1460 KB |
Output is correct |
60 |
Correct |
205 ms |
11496 KB |
Output is correct |
61 |
Correct |
321 ms |
16184 KB |
Output is correct |
62 |
Correct |
438 ms |
19128 KB |
Output is correct |
63 |
Correct |
537 ms |
22576 KB |
Output is correct |
64 |
Correct |
1 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 |
459 ms |
22528 KB |
Output is correct |
68 |
Correct |
468 ms |
22456 KB |
Output is correct |
69 |
Correct |
458 ms |
22304 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
71 |
Correct |
7 ms |
1236 KB |
Output is correct |
72 |
Correct |
192 ms |
11444 KB |
Output is correct |
73 |
Correct |
345 ms |
17760 KB |
Output is correct |
74 |
Correct |
530 ms |
22472 KB |
Output is correct |
75 |
Correct |
516 ms |
23052 KB |
Output is correct |
76 |
Correct |
464 ms |
22440 KB |
Output is correct |
77 |
Correct |
4 ms |
852 KB |
Output is correct |
78 |
Correct |
8 ms |
1364 KB |
Output is correct |
79 |
Correct |
198 ms |
11460 KB |
Output is correct |
80 |
Correct |
367 ms |
17716 KB |
Output is correct |
81 |
Correct |
530 ms |
22560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 KB |
Output is correct |
17 |
Incorrect |
305 ms |
20984 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 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 |
192 ms |
10640 KB |
Output is correct |
10 |
Correct |
9 ms |
1456 KB |
Output is correct |
11 |
Correct |
55 ms |
5936 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
31 ms |
5004 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
10692 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 |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
0 ms |
304 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
532 ms |
22580 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
568 KB |
Output is correct |
27 |
Correct |
4 ms |
696 KB |
Output is correct |
28 |
Correct |
141 ms |
9472 KB |
Output is correct |
29 |
Correct |
262 ms |
13792 KB |
Output is correct |
30 |
Correct |
383 ms |
18816 KB |
Output is correct |
31 |
Correct |
524 ms |
22528 KB |
Output is correct |
32 |
Correct |
0 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
300 KB |
Output is correct |
38 |
Correct |
0 ms |
212 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 |
304 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 |
195 ms |
11544 KB |
Output is correct |
46 |
Correct |
345 ms |
17320 KB |
Output is correct |
47 |
Correct |
333 ms |
17324 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
304 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
566 ms |
22564 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
3 ms |
468 KB |
Output is correct |
58 |
Correct |
8 ms |
1044 KB |
Output is correct |
59 |
Correct |
10 ms |
1460 KB |
Output is correct |
60 |
Correct |
205 ms |
11496 KB |
Output is correct |
61 |
Correct |
321 ms |
16184 KB |
Output is correct |
62 |
Correct |
438 ms |
19128 KB |
Output is correct |
63 |
Correct |
537 ms |
22576 KB |
Output is correct |
64 |
Correct |
1 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 |
459 ms |
22528 KB |
Output is correct |
68 |
Correct |
468 ms |
22456 KB |
Output is correct |
69 |
Correct |
458 ms |
22304 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
71 |
Correct |
7 ms |
1236 KB |
Output is correct |
72 |
Correct |
192 ms |
11444 KB |
Output is correct |
73 |
Correct |
345 ms |
17760 KB |
Output is correct |
74 |
Correct |
530 ms |
22472 KB |
Output is correct |
75 |
Correct |
516 ms |
23052 KB |
Output is correct |
76 |
Correct |
464 ms |
22440 KB |
Output is correct |
77 |
Correct |
4 ms |
852 KB |
Output is correct |
78 |
Correct |
8 ms |
1364 KB |
Output is correct |
79 |
Correct |
198 ms |
11460 KB |
Output is correct |
80 |
Correct |
367 ms |
17716 KB |
Output is correct |
81 |
Correct |
530 ms |
22560 KB |
Output is correct |
82 |
Incorrect |
0 ms |
212 KB |
Solution announced impossible, but it is possible. |
83 |
Halted |
0 ms |
0 KB |
- |