# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1029765 | aufan | Fountain Parks (IOI21_parks) | C++17 | 0 ms | 0 KiB |
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;
const auto fx[] = {1, 1, -1, -1};
const auto fy[] = {1, -1, 1, -1};
const auto dx[] = {2, 0, -2, 0};
const auto dy[] = {0, 2, 0, -2};
int construct_roads(vector<int> x, vector<int> y) {
int n = (int)x.size();
map<pair<int, int>, int> id;
set<pair<int, int>> pos;
for (int i = 0; i < n; i++) {
id[{x[i], y[i]}] = i;
for (int k = 0; k < 4; k++) {
int nx = x[i] + fx[k];
int ny = y[i] + fy[k];
pos.insert({nx, ny});
}
}
vector<int> vis(n);
function<void(int)> dfs = [&](int z) {
vis[z] = 1;
for (int k = 0; k < 4; k++) {
int nx = x[z] + dx[k];
int ny = y[z] + dy[k];
if (id.count({nx, ny})) {
int p = id[{nx, ny}];
if (!vis[p]) {
dfs(p);
}
}
}
};
dfs(0);
if (count(vis.begin(), vis.end(), 1) != n) {
return 0;
}
vector<int> u, v, a, b;
for (auto [cx, cy] : pos) {
if ((cx + cy) % 4 == 0) {
if (id.count({cx - 1, cy - 1}) && id.count({cx - 1, cy + 1})) {
u.push_back(id[{cx - 1, cy - 1}]);
v.push_back(id[{cx - 1, cy + 1}]);
a.push_back(cx);
b.push_back(cy);
} else if (id.count({cx + 1, cy - 1}) && id.count({cx + 1, cy + 1})) {
u.push_back(id[{cx + 1, cy - 1}]);
v.push_back(id[{cx + 1, cy + 1}]);
a.push_back(cx);
b.push_back(cy);
}
} else if ((cx + cy) % 4 == 2) {
if (id.count({cx - 1, cy - 1}) && id.count({cx + 1, cy - 1})) {
u.push_back(id[{cx - 1, cy - 1}]);
v.push_back(id[{cx + 1, cy - 1}]);
a.push_back(cx);
b.push_back(cy);
} else if (id.count({cx - 1, cy + 1}) && id.count({cx + 1, cy + 1})) {
u.push_back(id[{cx - 1, cy + 1}]);
v.push_back(id[{cx + 1, cy + 1}]);
a.push_back(cx);
b.push_back(cy);
}
}
}
build(u, v, a, b);
return 1;
}