#include<bits/stdc++.h>
#include "parks.h"
using namespace std;
struct DSU {
int n;
vector<int> parent;
vector<int> size;
DSU(int n) : n(n), parent(n), size(n, 1) {
iota(parent.begin(), parent.end(), 0);
}
int get(int i) {
return parent[i] == i ? i : parent[i] = get(parent[i]);
}
void merge(int v, int u) {
v = get(v);
u = get(u);
if (u == v) return;
if (size[v] < size[u]) swap(v, u);
parent[u] = v;
size[v] += size[u];
}
};
int construct_roads(std::vector<int> x, std::vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
int n = x.size();
std::vector<int> u, v, a, b;
vector<pair<int,int>> d;
map<pair<int,int>, int> ft;
for (int i = 0; i < n;i ++)
ft[{x[i], y[i]}] = i;
vector<tuple<int,int,int>> road;
set<pair<int,int>> bench;
for (int i = 0; i < n; i++) {
for (auto [dx, dy] : {pair{0, -2}, {0, +2}, {-2, 0}, {+2, 0}}) {
pair pos{x[i]+dx, y[i]+dy};
if (!ft.count(pos)) continue;
int j = ft[pos];
if (j < i) continue;
u.push_back(i);
v.push_back(j);
road.push_back({x[i]+dx/2, y[i]+dy/2, u.size()-1});
d.push_back({dx, dy});
}
}
a.resize(u.size());
b.resize(u.size());
sort(road.begin(), road.end());
for (auto [_x, _y, ri] : road) {
auto [dx, dy] = d[ri];
int i = u[ri], j = v[ri];
vector<pair<int,int>> pos_bench;
if (dx) {
pos_bench = {{x[i]+dx/2, y[i]+1}, {x[i]+dx/2, y[i]-1}};
} else {
pos_bench = {{x[i]-1, y[i]+dy/2}, {x[i]+1, y[i]+dy/2}};
}
bool done = 0;
for (auto pos : pos_bench) {
if (bench.count(pos) == 0) {
done = 1;
a[ri] = pos.first;
b[ri] = pos.second;
break;
}
bench.insert(pos);
}
if (!done) return 0;
}
DSU cc(n);
for (int i = 0; i < (int)u.size(); i++) {
cc.merge(u[i], v[i]);
}
if (cc.size[cc.get(0)] != n) return 0;
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:63:24: warning: unused variable 'j' [-Wunused-variable]
63 | int i = u[ri], j = v[ri];
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 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 |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 3 |
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 |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 3 |
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 |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Tree @(199999, 3) appears more than once: for edges on positions 0 and 1 |
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 |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 KB |
Output is correct |
17 |
Incorrect |
386 ms |
30784 KB |
Tree @(100001, 3) appears more than once: for edges on positions 58686 and 136652 |
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 |
0 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
153 ms |
15580 KB |
Output is correct |
10 |
Correct |
9 ms |
1884 KB |
Output is correct |
11 |
Correct |
55 ms |
8568 KB |
Output is correct |
12 |
Correct |
16 ms |
2724 KB |
Output is correct |
13 |
Correct |
36 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
136 ms |
15564 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |