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;
typedef vector<int> vi;
typedef long long ll;
typedef vector<signed> vi32;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef unordered_set<ll> si;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<ii> vii;
// typedef unordered_set<iii> siii;
// typedef unordered_set<ii> sii;
typedef vector<vii> vvii;
#define pb push_back
#define loop(x, i) for (int i = 0; i < x; i++)
#define ALL(x) begin(x), end(x)
#define sz(x) (int)x.size()
vii adj = {{-2, 0}, {2, 0}, {0, 2}, {0, -2}};
si benchPoints; // neighbour count
ll MX = 200'002;
ll toInt(iii p)
{
auto [a, b, c] = p;
return MX * MX * 4 * a + MX * b + c;
}
ll toInt(ii p)
{
auto [b, c] = p;
return MX * b + c;
}
void addNeighbour(ll x, ll y, int dif = 1)
{
if (benchPoints.count(toInt({1, x, y})))
{
benchPoints.erase(toInt({1, x, y}));
if (dif == 1)
{
benchPoints.insert(toInt({2, x, y}));
}
}
else if (benchPoints.count(toInt({2, x, y})))
{
benchPoints.erase(toInt({2, x, y}));
benchPoints.insert(toInt({2 + dif, x, y}));
}
else if (benchPoints.count(toInt({3, x, y})))
{
benchPoints.erase(toInt({3, x, y}));
if (dif == 1)
throw;
benchPoints.insert(toInt({2, x, y}));
}
else if (dif == 1)
{
benchPoints.insert(toInt({1, x, y}));
}
}
int construct_roads(vi xs, vi ys)
{
unordered_map<ll, int> fountains;
unordered_map<ll, int> roads;
// unordered_set<ii> gotBench;
int n = sz(xs);
loop(n, i)
{
fountains[toInt({xs[i], ys[i]})] = i;
}
vb vis(n);
vi u, v, a, b;
a = b = vi(n - 1);
int visCnt = 1;
auto dfs = [&](auto &&self, int i) -> void
{
int x = xs[i], y = ys[i];
for (const auto &[dx, dy] : adj)
{
ii pos = {x + dx, y + dy};
if (fountains.count(toInt(pos)))
{
int j = fountains[toInt(pos)];
if (vis[j])
continue;
if (dx != 0)
{
roads[toInt({x + dx / 2, y})] = sz(u);
addNeighbour(x + dx / 2, y + 1);
addNeighbour(x + dx / 2, y - 1);
}
else
{
roads[toInt({x, y + dy / 2})] = sz(u);
addNeighbour(x + 1, y + dy / 2);
addNeighbour(x - 1, y + dy / 2);
}
u.pb(i);
v.pb(j);
vis[j] = 1;
visCnt++;
self(self, j);
}
}
};
vis[0] = 1;
dfs(dfs, 0);
if (visCnt != n)
return 0;
for (int benchesBuilt = 0; benchesBuilt < n - 1; benchesBuilt++)
{
auto tupl = *benchPoints.begin();
ll x = (tupl % (MX * MX * 4LL)) / MX, y = tupl % MX;
benchPoints.erase(benchPoints.begin());
ii loc;
if (roads.count(toInt({x + 1, y})))
{
loc = {x + 1, y};
addNeighbour(x + 2, y, -1);
}
else if (roads.count(toInt({x - 1, y})))
{
loc = {x - 1, y};
addNeighbour(x - 2, y, -1);
}
else if (roads.count(toInt({x, y + 1})))
{
loc = {x, y + 1};
addNeighbour(x, y + 2, -1);
}
else
{
loc = {x, y - 1};
addNeighbour(x, y - 2, -1);
}
int ix = roads[toInt(loc)];
roads.erase(toInt(loc));
a[ix] = x;
b[ix] = y;
}
build(u, v, a, b);
return 1;
}
# | 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... |