#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]);
}
int size(int x) {
return -e[get(x)];
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
};
vector<vector<int>> adj;
vector<int> matched_to;
int idx;
bool try_kuhn(int v, vector<bool> &visited, vector<int> &matched_to) {
if (visited.at(v)) return false;
visited.at(v) = true;
for (int to : adj.at(v)) {
if (matched_to.at(to) == -1 || try_kuhn(matched_to.at(to), visited, matched_to)) {
matched_to.at(to) = v;
return true;
}
}
return false;
}
int bipartite_matching(int n) {
int matches = 0;
for (int i = idx; i < n; i++) {
vector<bool> visited = vector<bool>(n, false);
if (try_kuhn(i, visited, matched_to)) {
matches += 1;
}
}
return matches;
}
void add_edge(int i, int j) {
adj[i].push_back(j);
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int N = x.size();
const int MAXN = 2e5;
vector<map<int, int>> mp(MAXN + 10);
for (int i = 0; i < N; ++i) {
mp[x[i]][y[i]] = i;
}
idx = N;
vector<map<int, int>> pl(MAXN + 10);
vector<array<int, 2>> rev;
vector<int> dx = {1, 1, -1, -1}, dy = {1, -1, 1, -1};
vector<array<int, 5>> edges;
for (int i = 0; i < N; ++i) {
for (int k = 0; k < 4; ++k) {
int nx = x[i] + dx[k];
int ny = y[i] + dy[k];
if (!pl[nx].count(ny)) {
pl[nx][ny] = idx++;
rev.push_back({nx, ny});
}
}
}
DSU dsu(N);
for (int i = 0; i < N; ++i) {
if (mp[x[i]].count(y[i] + 2)) {
dsu.unite(i, mp[x[i]][y[i] + 2]);
int j = mp[x[i]][y[i] + 2];
int ny = y[i] + 1;
int nx;
if (((x[i] + y[i]) / 2) & 1) {
nx = y[i] + 1;
} else {
nx = y[i] - 1;
}
edges.push_back({nx, ny, x[i] + y[i], i, j});
}
if (mp[x[i] + 2].count(y[i])) {
dsu.unite(i, mp[x[i] + 2][y[i]]);
int j = mp[x[i] + 2][y[i]];
int ny;
int nx = x[i] + 1;
if (((x[i] + y[i]) / 2) & 1) {
ny = y[i] - 1;
} else {
ny = y[i] + 1;
}
edges.push_back({nx, ny, x[i] + y[i], i, j});
}
}
if (dsu.size(0) != N) return 0;
sort(begin(edges), end(edges));
vector<int> u, v, a, b;
for (int it = 0; it < (int)edges.size(); ++it) {
int i = edges[it][3];
int j = edges[it][4];
int x = edges[it][0];
int y = edges[it][1];
if (it > 0 && x == edges[it - 1][0] && y == edges[it - 1][1]) continue;
u.push_back(i);
v.push_back(j);
a.push_back(x);
b.push_back(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... |