#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
vector<int> sz,parent;
int getParent(int a){
if(parent[a]==a) return a;
return parent[a]=getParent(parent[a]);
}
bool same(int a, int b){
a=getParent(a);
b=getParent(b);
return (a==b);
}
void unite(int a, int b){
a=getParent(a);
b=getParent(b);
if(a==b) return;
if(sz[a]<sz[b]){
swap(a,b);
}
sz[a]+=sz[b];
parent[b]=a;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n=sz(x);
vector<pair<pair<int,int>,int>> f(n);
sz.resize(n,1);
parent.resize(n);
for (int i = 0; i < n; i++) parent[i]=i;
for (int i = 0; i < n; i++) f[i]={{x[i],y[i]},i};
map<pair<int,int>, int> mp;
for (int i = 0; i < n; i++) mp[{x[i],y[i]}]=i;
std::vector<int> u, v, a, b;
vector<int> dJ(n);
sort(all(f));
map<pair<int,int>,int> bch;
for (int i = 0; i < n; i++)
{
pair<int,int> und={f[i].first.first,f[i].first.second+2};
if(mp.find(und)!=mp.end()){
if(!same(f[i].second,mp[und])){
unite(f[i].second,mp[und]);
u.push_back(f[i].second);
v.push_back(mp[und]);
if(f[i].first.first<=4){
a.push_back(f[i].first.first-1);
b.push_back(f[i].first.second+1);
bch[{f[i].first.first-1,f[i].first.second+1}]=sz(a)-1;
}else{
a.push_back(f[i].first.first+1);
b.push_back(f[i].first.second+1);
}
}
}
}
for (int i = 0; i < n; i++)
{
if(f[i].first.first==4) continue;
pair<int,int> rgt={f[i].first.first+2,f[i].first.second};
if(mp.find(rgt)!=mp.end()){
if(!same(f[i].second,mp[rgt])){
unite(f[i].second,mp[rgt]);
u.push_back(f[i].second);
v.push_back(mp[rgt]);
if(bch.find({f[i].first.first+1,f[i].first.second-1})!=bch.end()){
a[bch[{f[i].first.first+1,f[i].first.second-1}]]+=2;
bch[{f[i].first.first+1,f[i].first.second-1}]=bch[{f[i].first.first+1,f[i].first.second-1}];
}
a.push_back(f[i].first.first+1);
b.push_back(f[i].first.second-1);
bch[{f[i].first.first+1,f[i].first.second-1}]=sz(a)-1;
}
}
}
for (int i = 0; i < n; i++)
{
if(f[i].first.first==2) continue;
pair<int,int> rgt={f[i].first.first+2,f[i].first.second};
if(mp.find(rgt)!=mp.end()){
if(!same(f[i].second,mp[rgt])){
unite(f[i].second,mp[rgt]);
u.push_back(f[i].second);
v.push_back(mp[rgt]);
if(bch.find({f[i].first.first+1,f[i].first.second-1})!=bch.end()){
a.push_back(f[i].first.first+1);
b.push_back(f[i].first.second+1);
}else{
a.push_back(f[i].first.first+1);
b.push_back(f[i].first.second+1);
}
}
}
}
if (sz[getParent(0)] != n) {
return 0;
}
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... |