#include "parks.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
typedef long long ll;
const int N = 2e5 + 5;
vector<ar<int, 3>> edges[N];
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
map<ar<int, 2>, int> ss, mm;
for(int i=0;i<n;i++){
ss[{x[i], y[i]}] = i;
}
vector<ar<int, 2>> e, p;
auto add = [&](int a, int b, int p1, int p2){
edges[a].push_back({b, p1, p2});
edges[b].push_back({a, p1, p2});
};
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(ss.count({x[i] - 2, y[i]})){
int j = ss[{x[i] - 2, y[i]}];
if(!merge(i, j)) continue;
e.push_back({i, j});
p.push_back({-1, -1});
int u = e.size() - 1;
if(mm.count({x[i] - 1, y[i] - 1})){
int v = mm[{x[i] - 1, y[i] - 1}];
add(u, v, x[i] - 1, y[i] - 1);
mm.erase({x[i] - 1, y[i] - 1});
} else {
mm[{x[i] - 1, y[i] - 1}] = u;
}
if(mm.count({x[i] - 1, y[i] + 1})){
int v = mm[{x[i] - 1, y[i] + 1}];
add(u, v, x[i] - 1, y[i] + 1);
mm.erase({x[i] - 1, y[i] + 1});
} else {
mm[{x[i] - 1, y[i] + 1}] = u;
}
}
if(ss.count({x[i], y[i] - 2})){
int j = ss[{x[i], y[i] - 2}];
if(!merge(i, j)) continue;
e.push_back({i, j});
p.push_back({-1, -1});
int u = e.size() - 1;
if(mm.count({x[i] - 1, y[i] - 1})){
int v = mm[{x[i] - 1, y[i] - 1}];
add(u, v, x[i] - 1, y[i] - 1);
mm.erase({x[i] - 1, y[i] - 1});
} else {
mm[{x[i] - 1, y[i] - 1}] = u;
}
if(mm.count({x[i] + 1, y[i] - 1})){
int v = mm[{x[i] + 1, y[i] - 1}];
add(u, v, x[i] + 1, y[i] - 1);
mm.erase({x[i] + 1, y[i] - 1});
} else {
mm[{x[i] + 1, y[i] - 1}] = u;
}
}
}
for(int i=0;i<n;i++){
if(ss.count({x[i] - 2, y[i]})){
int j = ss[{x[i] - 2, y[i]}];
if(!merge(i, j)) continue;
assert(false);
}
if(ss.count({x[i], y[i] - 2})){
int j = ss[{x[i], y[i] - 2}];
if(!merge(i, j)) continue;
assert(false);
}
}
if(sz[f(0)] != n) {
//~ assert(false);
return 0;
}
int m = e.size();
vector<int> used(m);
function<void(int)> dfs = [&](int i){
used[i] = 1;
for(auto [x, p1, p2] : edges[i]){
if(used[x]) continue;
p[x] = {p1, p2};
dfs(x);
}
};
for(auto [x, i] : mm){
if(used[i]) continue;
p[i] = x;
dfs(i);
}
for(int i=0;i<m;i++){
if(used[i]) continue;
p[i] = {edges[i].back()[1], edges[i].back()[2]};
edges[i].pop_back();
dfs(i);
}
vector<int> u(m), v(m), A(m), B(m);
for(int i=0;i<m;i++){
u[i] = e[i][0], v[i] = e[i][1];
A[i] = p[i][0], B[i] = p[i][1];
}
build(u, v, A, B);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Incorrect |
3 ms |
5004 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Incorrect |
3 ms |
5004 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
17 |
Correct |
4 ms |
5000 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
1000 ms |
57784 KB |
Output is correct |
21 |
Correct |
1064 ms |
53384 KB |
Output is correct |
22 |
Correct |
945 ms |
51132 KB |
Output is correct |
23 |
Correct |
812 ms |
53960 KB |
Output is correct |
24 |
Correct |
480 ms |
23928 KB |
Output is correct |
25 |
Correct |
954 ms |
51972 KB |
Output is correct |
26 |
Correct |
1059 ms |
51992 KB |
Output is correct |
27 |
Correct |
1062 ms |
62204 KB |
Output is correct |
28 |
Correct |
1115 ms |
62084 KB |
Output is correct |
29 |
Correct |
1127 ms |
62112 KB |
Output is correct |
30 |
Correct |
1095 ms |
62092 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
44 ms |
8376 KB |
Output is correct |
33 |
Correct |
152 ms |
14864 KB |
Output is correct |
34 |
Correct |
1177 ms |
59868 KB |
Output is correct |
35 |
Correct |
27 ms |
6836 KB |
Output is correct |
36 |
Correct |
164 ms |
13460 KB |
Output is correct |
37 |
Correct |
399 ms |
22036 KB |
Output is correct |
38 |
Correct |
333 ms |
22768 KB |
Output is correct |
39 |
Correct |
530 ms |
29236 KB |
Output is correct |
40 |
Correct |
721 ms |
35872 KB |
Output is correct |
41 |
Correct |
882 ms |
42456 KB |
Output is correct |
42 |
Correct |
1165 ms |
48988 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4996 KB |
Output is correct |
47 |
Correct |
3 ms |
4996 KB |
Output is correct |
48 |
Correct |
3 ms |
4948 KB |
Output is correct |
49 |
Correct |
3 ms |
4996 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
2 ms |
5000 KB |
Output is correct |
52 |
Correct |
3 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Correct |
6 ms |
5372 KB |
Output is correct |
55 |
Correct |
8 ms |
5484 KB |
Output is correct |
56 |
Correct |
422 ms |
28760 KB |
Output is correct |
57 |
Correct |
646 ms |
39376 KB |
Output is correct |
58 |
Correct |
713 ms |
39376 KB |
Output is correct |
59 |
Correct |
3 ms |
4948 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
6 ms |
4948 KB |
Output is correct |
62 |
Correct |
985 ms |
62120 KB |
Output is correct |
63 |
Correct |
1053 ms |
62348 KB |
Output is correct |
64 |
Correct |
1028 ms |
61996 KB |
Output is correct |
65 |
Correct |
9 ms |
5716 KB |
Output is correct |
66 |
Correct |
19 ms |
6520 KB |
Output is correct |
67 |
Correct |
436 ms |
27888 KB |
Output is correct |
68 |
Correct |
694 ms |
39468 KB |
Output is correct |
69 |
Correct |
1114 ms |
51024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
17 |
Correct |
988 ms |
62640 KB |
Output is correct |
18 |
Correct |
956 ms |
62696 KB |
Output is correct |
19 |
Correct |
1004 ms |
58500 KB |
Output is correct |
20 |
Correct |
1014 ms |
47660 KB |
Output is correct |
21 |
Correct |
913 ms |
49248 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Incorrect |
95 ms |
10440 KB |
Solution announced impossible, but it is possible. |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5000 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
386 ms |
33520 KB |
Output is correct |
10 |
Correct |
20 ms |
7824 KB |
Output is correct |
11 |
Correct |
120 ms |
20312 KB |
Output is correct |
12 |
Correct |
31 ms |
9352 KB |
Output is correct |
13 |
Correct |
85 ms |
15332 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5396 KB |
Output is correct |
16 |
Correct |
372 ms |
33588 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Incorrect |
3 ms |
5004 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |