#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] + (x[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]) { assert(!mm.count(p[i])); mm[p[i]]; 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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
17 |
Incorrect |
339 ms |
20736 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
245 ms |
10680 KB |
Output is correct |
10 |
Correct |
12 ms |
1456 KB |
Output is correct |
11 |
Correct |
69 ms |
5952 KB |
Output is correct |
12 |
Correct |
17 ms |
1972 KB |
Output is correct |
13 |
Correct |
30 ms |
5072 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
208 ms |
10688 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 6 |
20 |
Halted |
0 ms |
0 KB |
- |