#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
DSU (int n) {
e = vector<int>(n, -1);
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
void unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
}
};
int construct_roads(std::vector<int> x, std::vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
vector<array<int, 3>> st;
int N = x.size();
for (int i = 0; i < N; ++i) {
st.push_back({x[i], y[i], i});
}
sort(begin(st), end(st));
vector<array<int, 2>> con;
for (int i = 1; i < N; ++i) {
if (st[i][0] == st[i - 1][0] && st[i][1] - st[i - 1][1] == 2) {
con.push_back({st[i][2], st[i - 1][2]});
}
}
st.clear();
for (int i = 0; i < N; ++i) {
st.push_back({y[i], x[i], i});
}
sort(begin(st), end(st));
for (int i = 1; i < N; ++i) {
if (st[i][0] == st[i - 1][0] && st[i][1] - st[i - 1][1] == 2) {
con.push_back({st[i][2], st[i - 1][2]});
}
}
for (int i = 0; i < N; ++i) {
if (con[i][0] > con[i][1]) swap(con[i][0], con[i][1]);
}
sort(begin(con), end(con));
con.erase(unique(begin(con), end(con)), end(con));
DSU dsu(N);
for (auto& [x, y] : con) {
dsu.unite(x, y);
}
if (-dsu.e[dsu.get(0)] != N) return 0;
vector<int> v, u, a, b;
for (auto& [i, j] : con) {
v.push_back(i);
u.push_back(j);
if (x[i] == x[j]) {
int ny = min(y[i], y[j]) + 1;
b.push_back(ny);
if (x[i] == 2) {
a.push_back(x[i] - 1);
} else {
a.push_back(x[i] + 1);
}
} else {
assert(y[i] == y[j]);
int nx = min(x[i], x[j]) + 1;
a.push_back(nx);
b.push_back(y[i] + 1);
}
}
build(v, u, 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... |