#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
struct fountain
{
int x, y;
size_t i;
};
template <size_t N>
struct dsu
{
int t[N];
dsu() { memset(t, 255, sizeof t); }
int repr(int u) { return t[u] < 0 ? u : t[u] = repr(t[u]); }
bool same_component(int u, int v) { return repr(u) == repr(v); }
int component_size(int u) { return -t[repr(u)]; }
void merge(int u, int v)
{
u = repr(u), v = repr(v);
if (u == v)
return;
if (t[u] > t[v])
swap(u, v);
t[u] += t[v];
t[v] = u;
}
};
constexpr size_t N = 200000;
dsu<N> d;
vector<uint32_t> g[N];
set<pair<int, int>> occupied;
vector<int> u, v, a, b;
void dfs(uint32_t node, uint32_t p, vector<int> const &x, vector<int> const &y)
{
for (uint32_t const &child : g[node])
if (child != p)
{
dfs(child, node, x, y);
pair<int, int> bench[2];
if (x[node] == x[child])
{
bench[0] = {x[node] - 1, (y[node] + y[child]) / 2};
bench[1] = {x[node] + 1, (y[node] + y[child]) / 2};
}
else
{
assert(y[node] == y[child]);
bench[0] = {(x[node] + x[child]) / 2, y[node] - 1};
bench[1] = {(x[node] + x[child]) / 2, y[node] + 1};
}
u.push_back(node);
v.push_back(child);
for (size_t i = 0; i < 2; ++i)
if (occupied.find(bench[i]) == occupied.end())
{
occupied.insert(bench[i]);
a.push_back(bench[i].first);
b.push_back(bench[i].second);
break;
}
assert(a.size() == u.size());
}
}
int construct_roads(vector<int> x, vector<int> y)
{
size_t const n = x.size();
int x_max = 2;
vector<fountain> f;
for (size_t i = 0; i < n; ++i)
f.push_back({x[i], y[i], i}), x_max = max(x_max, x[i]);
sort(f.begin(), f.end(), [](auto const &a, auto const &b)
{ return a.y == b.y ? a.x < b.x : a.y < b.y; });
if (x_max <= 6)
{
auto add_edge = [&](size_t i, size_t j)
{
u.push_back(i);
v.push_back(j);
d.merge(i, j);
};
auto add_bench = [&](int x, int y)
{
a.push_back(x);
b.push_back(y);
occupied.insert(make_pair(x, y));
};
for (size_t i = 0; i < n;)
{
size_t j = i;
while (j < n && f[j].y == f[i].y)
++j;
if (j - i >= 2 && f[i + 1].x - f[i].x == 2 && !d.same_component(f[i].i, f[i + 1].i))
{
add_edge(f[i].i, f[i + 1].i);
if (occupied.find(make_pair(f[i].x + 1, f[i].y - 1)) == occupied.end())
add_bench(f[i].x + 1, f[i].y - 1);
else
add_bench(f[i].x + 1, f[i].y + 1);
}
if (j - i >= 3 && f[i + 2].x - f[i + 1].x == 2 && !d.same_component(f[i + 1].i, f[i + 2].i))
{
add_edge(f[i + 1].i, f[i + 2].i);
if (occupied.find(make_pair(f[i + 1].x + 1, f[i + 1].y - 1)) == occupied.end())
add_bench(f[i + 1].x + 1, f[i + 1].y - 1);
else
add_bench(f[i + 1].x + 1, f[i + 1].y + 1);
}
if (j < n && f[j].y == f[i].y + 2)
{
size_t k = j;
while (i < j && k < n && f[k].y == f[j].y)
{
if (f[i].x < f[k].x)
++i;
else if (f[i].x > f[k].x)
++k;
else
{
add_edge(f[i].i, f[k].i);
if (f[i].x == 2)
add_bench(f[i].x - 1, f[i].y + 1);
else if (f[i].x == 6)
add_bench(f[i].x + 1, f[i].y + 1);
else
{
if (occupied.find(make_pair(f[i].x - 1, f[i].y + 1)) == occupied.end())
add_bench(f[i].x - 1, f[i].y + 1);
else
add_bench(f[i].x + 1, f[i].y + 1);
}
++i;
++k;
}
}
}
i = j;
}
if (d.component_size(0) != n)
return 0;
build(u, v, a, b);
return 1;
}
else
{
for (size_t i = 0; i < n;)
{
size_t j = i;
while (j < n && f[j].y == f[i].y)
++j;
size_t k = j;
while (i < j && k < n && f[k].y == f[j].y)
{
if (f[i].x < f[k].x)
++i;
else if (f[i].x > f[k].x)
++k;
else
{
g[f[i].i].push_back(f[k].i);
g[f[k].i].push_back(f[i].i);
d.merge(f[i].i, f[k].i);
++i;
++k;
}
}
i = j;
}
if (d.component_size(0) != n)
return 0;
dfs(0, -1, x, y);
build(u, v, a, b);
return 1;
}
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:158:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
158 | if (d.component_size(0) != n)
| ~~~~~~~~~~~~~~~~~~~~^~~~
parks.cpp:191:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
191 | if (d.component_size(0) != n)
| ~~~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
4 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5776 KB |
Output is correct |
22 |
Correct |
3 ms |
5752 KB |
Output is correct |
23 |
Correct |
151 ms |
32652 KB |
Output is correct |
24 |
Correct |
3 ms |
5716 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
6040 KB |
Output is correct |
27 |
Correct |
4 ms |
6100 KB |
Output is correct |
28 |
Correct |
55 ms |
16608 KB |
Output is correct |
29 |
Correct |
93 ms |
21836 KB |
Output is correct |
30 |
Correct |
119 ms |
27544 KB |
Output is correct |
31 |
Correct |
144 ms |
32688 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
4 ms |
5776 KB |
Output is correct |
34 |
Correct |
3 ms |
5784 KB |
Output is correct |
35 |
Correct |
3 ms |
5716 KB |
Output is correct |
36 |
Correct |
3 ms |
5716 KB |
Output is correct |
37 |
Correct |
3 ms |
5780 KB |
Output is correct |
38 |
Correct |
3 ms |
5716 KB |
Output is correct |
39 |
Correct |
2 ms |
5716 KB |
Output is correct |
40 |
Correct |
4 ms |
5776 KB |
Output is correct |
41 |
Correct |
2 ms |
5716 KB |
Output is correct |
42 |
Correct |
3 ms |
5716 KB |
Output is correct |
43 |
Correct |
4 ms |
5920 KB |
Output is correct |
44 |
Correct |
4 ms |
6048 KB |
Output is correct |
45 |
Correct |
70 ms |
19256 KB |
Output is correct |
46 |
Correct |
107 ms |
25988 KB |
Output is correct |
47 |
Correct |
104 ms |
25996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
4 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5776 KB |
Output is correct |
22 |
Correct |
3 ms |
5752 KB |
Output is correct |
23 |
Correct |
151 ms |
32652 KB |
Output is correct |
24 |
Correct |
3 ms |
5716 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
6040 KB |
Output is correct |
27 |
Correct |
4 ms |
6100 KB |
Output is correct |
28 |
Correct |
55 ms |
16608 KB |
Output is correct |
29 |
Correct |
93 ms |
21836 KB |
Output is correct |
30 |
Correct |
119 ms |
27544 KB |
Output is correct |
31 |
Correct |
144 ms |
32688 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
4 ms |
5776 KB |
Output is correct |
34 |
Correct |
3 ms |
5784 KB |
Output is correct |
35 |
Correct |
3 ms |
5716 KB |
Output is correct |
36 |
Correct |
3 ms |
5716 KB |
Output is correct |
37 |
Correct |
3 ms |
5780 KB |
Output is correct |
38 |
Correct |
3 ms |
5716 KB |
Output is correct |
39 |
Correct |
2 ms |
5716 KB |
Output is correct |
40 |
Correct |
4 ms |
5776 KB |
Output is correct |
41 |
Correct |
2 ms |
5716 KB |
Output is correct |
42 |
Correct |
3 ms |
5716 KB |
Output is correct |
43 |
Correct |
4 ms |
5920 KB |
Output is correct |
44 |
Correct |
4 ms |
6048 KB |
Output is correct |
45 |
Correct |
70 ms |
19256 KB |
Output is correct |
46 |
Correct |
107 ms |
25988 KB |
Output is correct |
47 |
Correct |
104 ms |
25996 KB |
Output is correct |
48 |
Correct |
3 ms |
5716 KB |
Output is correct |
49 |
Correct |
2 ms |
5716 KB |
Output is correct |
50 |
Correct |
4 ms |
5772 KB |
Output is correct |
51 |
Correct |
3 ms |
5716 KB |
Output is correct |
52 |
Correct |
3 ms |
5780 KB |
Output is correct |
53 |
Correct |
3 ms |
5776 KB |
Output is correct |
54 |
Correct |
4 ms |
5716 KB |
Output is correct |
55 |
Correct |
145 ms |
32640 KB |
Output is correct |
56 |
Correct |
5 ms |
5776 KB |
Output is correct |
57 |
Correct |
6 ms |
5940 KB |
Output is correct |
58 |
Correct |
7 ms |
6588 KB |
Output is correct |
59 |
Correct |
8 ms |
6736 KB |
Output is correct |
60 |
Correct |
67 ms |
19204 KB |
Output is correct |
61 |
Correct |
91 ms |
25136 KB |
Output is correct |
62 |
Correct |
117 ms |
28240 KB |
Output is correct |
63 |
Correct |
141 ms |
32712 KB |
Output is correct |
64 |
Correct |
3 ms |
5716 KB |
Output is correct |
65 |
Correct |
2 ms |
5776 KB |
Output is correct |
66 |
Correct |
7 ms |
5784 KB |
Output is correct |
67 |
Correct |
135 ms |
32656 KB |
Output is correct |
68 |
Correct |
153 ms |
32664 KB |
Output is correct |
69 |
Correct |
145 ms |
32608 KB |
Output is correct |
70 |
Correct |
4 ms |
6176 KB |
Output is correct |
71 |
Correct |
6 ms |
6484 KB |
Output is correct |
72 |
Correct |
69 ms |
19212 KB |
Output is correct |
73 |
Correct |
106 ms |
26352 KB |
Output is correct |
74 |
Correct |
140 ms |
32652 KB |
Output is correct |
75 |
Correct |
136 ms |
32644 KB |
Output is correct |
76 |
Correct |
145 ms |
32652 KB |
Output is correct |
77 |
Correct |
5 ms |
6304 KB |
Output is correct |
78 |
Correct |
8 ms |
6736 KB |
Output is correct |
79 |
Correct |
71 ms |
19192 KB |
Output is correct |
80 |
Correct |
114 ms |
26352 KB |
Output is correct |
81 |
Correct |
156 ms |
32728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
17 |
Incorrect |
3 ms |
5716 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
17 |
Incorrect |
62 ms |
17160 KB |
Solution announced impossible, but it is possible. |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5780 KB |
Output is correct |
4 |
Correct |
3 ms |
5776 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5776 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
65 ms |
19224 KB |
Output is correct |
10 |
Correct |
8 ms |
7164 KB |
Output is correct |
11 |
Correct |
33 ms |
13024 KB |
Output is correct |
12 |
Correct |
14 ms |
7856 KB |
Output is correct |
13 |
Correct |
16 ms |
10312 KB |
Output is correct |
14 |
Correct |
4 ms |
5844 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
67 ms |
19204 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
4 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5776 KB |
Output is correct |
22 |
Correct |
3 ms |
5752 KB |
Output is correct |
23 |
Correct |
151 ms |
32652 KB |
Output is correct |
24 |
Correct |
3 ms |
5716 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
6040 KB |
Output is correct |
27 |
Correct |
4 ms |
6100 KB |
Output is correct |
28 |
Correct |
55 ms |
16608 KB |
Output is correct |
29 |
Correct |
93 ms |
21836 KB |
Output is correct |
30 |
Correct |
119 ms |
27544 KB |
Output is correct |
31 |
Correct |
144 ms |
32688 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
4 ms |
5776 KB |
Output is correct |
34 |
Correct |
3 ms |
5784 KB |
Output is correct |
35 |
Correct |
3 ms |
5716 KB |
Output is correct |
36 |
Correct |
3 ms |
5716 KB |
Output is correct |
37 |
Correct |
3 ms |
5780 KB |
Output is correct |
38 |
Correct |
3 ms |
5716 KB |
Output is correct |
39 |
Correct |
2 ms |
5716 KB |
Output is correct |
40 |
Correct |
4 ms |
5776 KB |
Output is correct |
41 |
Correct |
2 ms |
5716 KB |
Output is correct |
42 |
Correct |
3 ms |
5716 KB |
Output is correct |
43 |
Correct |
4 ms |
5920 KB |
Output is correct |
44 |
Correct |
4 ms |
6048 KB |
Output is correct |
45 |
Correct |
70 ms |
19256 KB |
Output is correct |
46 |
Correct |
107 ms |
25988 KB |
Output is correct |
47 |
Correct |
104 ms |
25996 KB |
Output is correct |
48 |
Correct |
3 ms |
5716 KB |
Output is correct |
49 |
Correct |
2 ms |
5716 KB |
Output is correct |
50 |
Correct |
4 ms |
5772 KB |
Output is correct |
51 |
Correct |
3 ms |
5716 KB |
Output is correct |
52 |
Correct |
3 ms |
5780 KB |
Output is correct |
53 |
Correct |
3 ms |
5776 KB |
Output is correct |
54 |
Correct |
4 ms |
5716 KB |
Output is correct |
55 |
Correct |
145 ms |
32640 KB |
Output is correct |
56 |
Correct |
5 ms |
5776 KB |
Output is correct |
57 |
Correct |
6 ms |
5940 KB |
Output is correct |
58 |
Correct |
7 ms |
6588 KB |
Output is correct |
59 |
Correct |
8 ms |
6736 KB |
Output is correct |
60 |
Correct |
67 ms |
19204 KB |
Output is correct |
61 |
Correct |
91 ms |
25136 KB |
Output is correct |
62 |
Correct |
117 ms |
28240 KB |
Output is correct |
63 |
Correct |
141 ms |
32712 KB |
Output is correct |
64 |
Correct |
3 ms |
5716 KB |
Output is correct |
65 |
Correct |
2 ms |
5776 KB |
Output is correct |
66 |
Correct |
7 ms |
5784 KB |
Output is correct |
67 |
Correct |
135 ms |
32656 KB |
Output is correct |
68 |
Correct |
153 ms |
32664 KB |
Output is correct |
69 |
Correct |
145 ms |
32608 KB |
Output is correct |
70 |
Correct |
4 ms |
6176 KB |
Output is correct |
71 |
Correct |
6 ms |
6484 KB |
Output is correct |
72 |
Correct |
69 ms |
19212 KB |
Output is correct |
73 |
Correct |
106 ms |
26352 KB |
Output is correct |
74 |
Correct |
140 ms |
32652 KB |
Output is correct |
75 |
Correct |
136 ms |
32644 KB |
Output is correct |
76 |
Correct |
145 ms |
32652 KB |
Output is correct |
77 |
Correct |
5 ms |
6304 KB |
Output is correct |
78 |
Correct |
8 ms |
6736 KB |
Output is correct |
79 |
Correct |
71 ms |
19192 KB |
Output is correct |
80 |
Correct |
114 ms |
26352 KB |
Output is correct |
81 |
Correct |
156 ms |
32728 KB |
Output is correct |
82 |
Incorrect |
3 ms |
5716 KB |
Solution announced impossible, but it is possible. |
83 |
Halted |
0 ms |
0 KB |
- |