/*
Mayoeba Yabureru
*/
#include "parks.h"
#include<bits/stdc++.h>
using namespace std;
static vector<int> _u, _v, _a, _b;
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
vector<vector<int>> gr(n + 1);
vector<int> dsu(n + 1);
map<pair<int, int>, int> mp;
map<pair<int, int>, pair<int, int>> mpp;
vector<pair<int, int>> vv;
for (int i = 0; i < n; i ++) {
mp[{x[i], y[i]}] = i + 1;
vv.push_back({x[i], y[i]});
dsu[i] = i;
}
vector<int> u, v, a, b;
map<pair<int, int>, int> taken;
map<pair<int, int>, int> connected;
function<int(int)> fp = [&](int x) {
if (dsu[x] == x) return x;
return dsu[x] = fp(dsu[x]);
};
function cmp = [&] (pair<int, int> a, pair<int, int> b) {
if (a.first + a.second < b.first + b.second) return 1;
return 0;
};
sort(vv.begin(), vv.end(), cmp);
for (int i = 1; i <= n; i ++) {
vector nx = {0, 2, 0, -2}, ny = {2, 0, -2, 0};
for (int j = 0; j < 4; j ++) {
int xx = x[i] + nx[j], yy = y[i] + ny[j];
if (mp[{xx, yy}]) {
int fx = fp(mp[{x[i], y[i]}]), fy = fp(mp[{xx, yy}]);
if (fx != fy) {
if (x[i] < xx || y[i] < yy) {
u.push_back(mp[{x[i], y[i]}]);
v.push_back(mp[{xx, yy}]);
}
else {
v.push_back(mp[{x[i], y[i]}]);
u.push_back(mp[{xx, yy}]);
}
gr[mp[{x[i], y[i]}]].push_back(mp[{xx, yy}]);
gr[mp[{xx, yy}]].push_back(mp[{x[i], y[i]}]);
dsu[fx] = fy;
connected[{mp[{x[i], y[i]}], mp[{xx, yy}]}] = 1;
connected[{mp[{xx, yy}], mp[{x[i], y[i]}]}] = 1;
}
}
}
}
int num = 0, st = 1;
vector<int> vis(n + 1);
function<void(int)> go = [&] (int v) {
num ++;
vis[v] = 1;
vector nx = {0, 2, 0, -2}, ny = {2, 0, -2, 0};
int ii = 0;
for (int i = 0; i < 4; i ++) {
int u = mp[{x[v] + nx[i], y[v] + ny[i]}];
if (u == 0) continue;
if (vis[u]) {
ii = (i + 1) % 4;
break;
}
}
int nummmm = 10;
while (nummmm --) {
int i = ii;
ii = (ii + 1) % 4;
int u = mp[{x[v] + nx[i], y[v] + ny[i]}];
if (u == 0 || vis[u] || connected[{u, v}] == 0) continue;
if (x[v] != x[u]) {
int xx = v, uu = u;
if (x[xx] > x[uu]) {
if (taken[{x[uu] + 1, y[uu] - 1}] == 0) {
mpp[{uu, xx}] = {x[uu] + 1, y[uu] - 1};
taken[{x[uu] + 1, y[uu] - 1}] = 1;
}
else if (taken[{x[uu] + 1, y[uu] + 1}] == 0) {
mpp[{uu, xx}] = {x[uu] + 1, y[uu] + 1};
taken[{x[uu] + 1, y[uu] + 1}] = 1;
}
else assert(0);
}
else {
if (taken[{x[xx] + 1, y[xx] + 1}] == 0) {
mpp[{xx, uu}] = {x[xx] + 1, y[xx] + 1};
taken[{x[xx] + 1, y[xx] + 1}] = 1;
}
else if (taken[{x[xx] + 1, y[xx] - 1}] == 0) {
mpp[{xx, uu}] = {x[xx] + 1, y[xx] - 1};
taken[{x[xx] + 1, y[xx] - 1}] = 1;
}
else assert(0);
}
}
else {
int xx = v, uu = u;
if (y[xx] > y[uu]) {
if (taken[{x[uu] + 1, y[uu] + 1}] == 0) {
mpp[{uu, xx}] = {x[uu] + 1, y[uu] + 1};
taken[{x[uu] + 1, y[uu] + 1}] = 1;
}
else if (taken[{x[uu] - 1, y[uu] + 1}] == 0) {
mpp[{uu, xx}] = {x[uu] - 1, y[uu] + 1};
taken[{x[uu] - 1, y[uu] + 1}] = 1;
}
else assert(0);
}
else {
if (taken[{x[xx] - 1, y[xx] + 1}] == 0) {
mpp[{xx, uu}] = {x[xx] - 1, y[xx] + 1};
taken[{x[xx] - 1, y[xx] + 1}] = 1;
}
else if (taken[{x[xx] + 1, y[xx] + 1}] == 0) {
mpp[{xx, uu}] = {x[xx] + 1, y[xx] + 1};
taken[{x[xx] + 1, y[xx] + 1}] = 1;
}
else assert(0);
}
}
go(u);
}
};
go(st);
for (int i = 0; i < u.size(); i ++) {
a.push_back(mpp[{u[i], v[i]}].first);
b.push_back(mpp[{u[i], v[i]}].second);
u[i] --;
v[i] --;
}
if (num == n) {
build(u, v, a, b);
return 1;
}
return 0;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:135:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for (int i = 0; i < u.size(); i ++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Solution announced impossible, but it is possible. |
3 |
Halted |
0 ms |
0 KB |
- |