#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;
}
}
}
int m = e.size();
if(m < n - 1) return 0;
assert(m == n - 1);
for(int i=0;i<m;i++){
assert((int)edges[i].size() <= 2);
}
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];
assert(~A[i] && ~B[i]);
}
build(u, v, A, B);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Incorrect |
3 ms |
4948 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Incorrect |
3 ms |
4948 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
735 ms |
55188 KB |
Output is correct |
21 |
Correct |
878 ms |
51108 KB |
Output is correct |
22 |
Correct |
737 ms |
48804 KB |
Output is correct |
23 |
Correct |
645 ms |
52280 KB |
Output is correct |
24 |
Correct |
304 ms |
22224 KB |
Output is correct |
25 |
Correct |
794 ms |
50392 KB |
Output is correct |
26 |
Correct |
762 ms |
50348 KB |
Output is correct |
27 |
Correct |
857 ms |
60604 KB |
Output is correct |
28 |
Correct |
854 ms |
60636 KB |
Output is correct |
29 |
Correct |
883 ms |
60524 KB |
Output is correct |
30 |
Correct |
841 ms |
60460 KB |
Output is correct |
31 |
Correct |
2 ms |
4948 KB |
Output is correct |
32 |
Correct |
34 ms |
8116 KB |
Output is correct |
33 |
Correct |
103 ms |
13516 KB |
Output is correct |
34 |
Correct |
828 ms |
57340 KB |
Output is correct |
35 |
Correct |
22 ms |
6612 KB |
Output is correct |
36 |
Correct |
140 ms |
12740 KB |
Output is correct |
37 |
Correct |
336 ms |
20788 KB |
Output is correct |
38 |
Correct |
303 ms |
21764 KB |
Output is correct |
39 |
Correct |
449 ms |
27820 KB |
Output is correct |
40 |
Correct |
644 ms |
34120 KB |
Output is correct |
41 |
Correct |
787 ms |
40228 KB |
Output is correct |
42 |
Correct |
1034 ms |
46584 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 |
4948 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
3 ms |
4952 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
4948 KB |
Output is correct |
52 |
Correct |
3 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Correct |
5 ms |
5332 KB |
Output is correct |
55 |
Correct |
7 ms |
5484 KB |
Output is correct |
56 |
Correct |
338 ms |
28024 KB |
Output is correct |
57 |
Correct |
586 ms |
38352 KB |
Output is correct |
58 |
Correct |
559 ms |
38284 KB |
Output is correct |
59 |
Correct |
2 ms |
4948 KB |
Output is correct |
60 |
Correct |
2 ms |
4948 KB |
Output is correct |
61 |
Correct |
2 ms |
4948 KB |
Output is correct |
62 |
Correct |
867 ms |
60540 KB |
Output is correct |
63 |
Correct |
853 ms |
60544 KB |
Output is correct |
64 |
Correct |
856 ms |
60392 KB |
Output is correct |
65 |
Correct |
11 ms |
5588 KB |
Output is correct |
66 |
Correct |
17 ms |
6296 KB |
Output is correct |
67 |
Correct |
361 ms |
27120 KB |
Output is correct |
68 |
Correct |
592 ms |
38256 KB |
Output is correct |
69 |
Correct |
944 ms |
49416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
17 |
Correct |
911 ms |
60540 KB |
Output is correct |
18 |
Correct |
907 ms |
62752 KB |
Output is correct |
19 |
Correct |
838 ms |
58628 KB |
Output is correct |
20 |
Correct |
972 ms |
47544 KB |
Output is correct |
21 |
Correct |
920 ms |
49324 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Incorrect |
74 ms |
10536 KB |
Solution announced impossible, but it is possible. |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
349 ms |
32764 KB |
Output is correct |
10 |
Correct |
18 ms |
7796 KB |
Output is correct |
11 |
Correct |
105 ms |
19952 KB |
Output is correct |
12 |
Correct |
29 ms |
9124 KB |
Output is correct |
13 |
Correct |
68 ms |
15036 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5332 KB |
Output is correct |
16 |
Correct |
332 ms |
32824 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Incorrect |
3 ms |
4948 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |