This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
for (size_t k = i + 1; k < j; ++k)
if (f[k].x == f[k - 1].x + 2)
{
g[f[k - 1].i].push_back(f[k].i);
g[f[k].i].push_back(f[k - 1].i);
d.merge(f[k - 1].i, f[k].i);
}
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
{
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 (stderr)
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:159:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
159 | if (d.component_size(0) != n)
| ~~~~~~~~~~~~~~~~~~~~^~~~
parks.cpp:203:33: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
203 | if (d.component_size(0) != n)
| ~~~~~~~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |