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;
bitset<N> occupied[8];
int construct_roads(vector<int> x, vector<int> y)
{
size_t const n = x.size();
vector<fountain> f;
for (size_t i = 0; i < n; ++i)
f.push_back({x[i], y[i], 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; });
vector<int> u, v, a, b;
auto add_edge = [&](size_t i, size_t j)
{
u.push_back(i);
v.push_back(j);
d.merge(i, j);
};
auto add_bench = [&](size_t x, size_t y)
{
assert(!occupied[x][y]);
a.push_back(x);
b.push_back(y);
occupied[x][y] = 1;
};
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[f[i].x + 1][f[i].y - 1])
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[f[i + 1].x + 1][f[i + 1].y - 1])
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[f[i].x - 1][f[i].y + 1])
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;
}
Compilation message (stderr)
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:123:29: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
123 | 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... |