//#include "parks.h"
#include<iostream>
#include<cmath>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
vector <int> v[200005],vx,vy,u2,c2,v2,a2,b2;
vector <pair<pair<int,int>,int> > v1;
set <pair<int,int> > s;
vector <pair<int,int> > c;
map <pair<int,int>,int> m1;
bool comp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b) {
return a.first.second<b.first.second;
}
int bio[200005],dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1},cnt;
void dfs(int x) {
bio[x]=1;
cnt+=1;
c2.push_back(x);
for(int i=0;i<v[x].size();i++) {
if(!bio[v[x][i]]) {
c.push_back(make_pair(x,v[x][i]));
dfs(v[x][i]);
}
}
}
int ok(int x,int y,int a,int b) {
if(vx[a]==vx[b] && abs(x-vx[a])==1 && y==(vy[a]+vy[b])/2) return 1;
if(vy[a]==vy[b] && abs(y-vy[a])==1 && x==(vx[a]+vx[b])/2) return 1;
return 0;
}
int construct_roads(std::vector<int> x,std::vector<int> y) {
vx=x;
vy=y;
int n=x.size(),minx=1e9,maxx=-1e9;
if(n==1) {
build({},{},{},{});
return 1;
}
for(int i=0;i<n;i++) {
v1.push_back(make_pair(make_pair(x[i],y[i]),i));
s.insert(make_pair(x[i]-1,y[i]-1));
s.insert(make_pair(x[i]+1,y[i]-1));
s.insert(make_pair(x[i]-1,y[i]+1));
s.insert(make_pair(x[i]+1,y[i]+1));
m1[make_pair(x[i],y[i])]=i+1;
maxx=max(maxx,x[i]);
minx=min(minx,x[i]);
}
sort(v1.begin(),v1.end(),comp);
for(int i=0;i<v1.size();i++) {
for(int j=i+1;j<v1.size() && j-i<=20;j++) {
int x1=v1[i].first.first,y1=v1[i].first.second,x2=v1[j].first.first,y2=v1[j].first.second,i2=v1[i].second,j2=v1[j].second;
if((abs(x1-x2)==2 && y1==y2) || (abs(y1-y2)==2 && x1==x2)) {
v[i2].push_back(j2);
v[j2].push_back(i2);
u2.push_back(i2);
v2.push_back(j2);
//cout << i2 << " " << j2 << "\n";
}
}
}
if(!(maxx<=4 && minx>=2)) {
u2.clear();
v2.clear();
for(int i=0;i<n;i++) {
v[i].clear();
}
for(int i=0;i<n;i++) {
if(m[make_pair(x[i]+2,y[i])]) {
v[m[make_pair(x[i]+2,y[i])]-1].push_back(i);
u2.push_back(m[make_pair(x[i]+2,y[i])]-1);
v2.push_back(i);
}
if(m[make_pair(x[i],y[i]+2)]) {
v[m[make_pair(x[i],y[i]+2)]-1].push_back(i);
u2.push_back(m[make_pair(x[i],y[i]+2)]-1);
v2.push_back(i);
}
if(m[make_pair(x[i]-2,y[i])]) {
v[m[make_pair(x[i]-2,y[i])]-1].push_back(i);
u2.push_back(m[make_pair(x[i]-2,y[i])]-1);
v2.push_back(i);
}
if(m[make_pair(x[i],y[i]-2)]) {
v[m[make_pair(x[i],y[i]-2)]-1].push_back(i);
u2.push_back(m[make_pair(x[i],y[i]-2)]-1);
v2.push_back(i);
}
}
}
dfs(0);
//cout << cnt << "\n";
if(cnt<n) return 0;
if(minx>=2 && maxx<=4) {
for(int i=0;i<u2.size();i++) {
if(y[u2[i]]==y[v2[i]]) {
a2.push_back(3);
b2.push_back(y[v2[i]]-1);
}
else if(x[u2[i]]==4) {
a2.push_back(5);
b2.push_back(y[v2[i]]-1);
}
else {
a2.push_back(1);
b2.push_back(y[v2[i]]-1);
}
}
}
else {
u2.clear();
v2.clear();
for(int i=0;i<n-1;i++) {
a2.push_back(-1);
b2.push_back(-1);
u2.push_back(c[i].first);
v2.push_back(c[i].second);
}
for(int i=0;i<n-1;i++) {
for(int j=0;j<4;j++) {
if(s.find(make_pair(x[c2[i]]+dx[j],y[c2[i]]+dy[j]))!=s.end() && ok(x[c2[i]]+dx[j],y[c2[i]]+dy[j],u2[i],v2[i])) {
a2[i]=x[c2[i]]+dx[j];
b2[i]=y[c2[i]]+dy[j];
s.erase(make_pair(x[c2[i]]+dx[j],y[c2[i]]+dy[j]));
break;
}
if(s.find(make_pair(x[c2[i+1]]+dx[j],y[c2[i+1]]+dy[j]))!=s.end() && ok(x[c2[i+1]]+dx[j],y[c2[i+1]]+dy[j],u2[i],v2[i])) {
a2[i]=x[c2[i+1]]+dx[j];
b2[i]=y[c2[i+1]]+dy[j];
s.erase(make_pair(x[c2[i+1]]+dx[j],y[c2[i+1]]+dy[j]));
break;
}
}
}
/*cout << "\n\n";
for(int i=0;i<u2.size();i++) {
cout << u2[i] << " " << v2[i] << "\n";
}
cout << "\n";
for(int i=0;i<a2.size();i++) {
cout << a2[i] << " " << b2[i] << "\n";
}*/
}
build(u2,v2,a2,b2);
return 1;
}
int main()
{
int n;
cin >> n;
vector <int> x,y;
for(int i=0;i<n;i++) {
int x1,y1;
cin >> x1 >> y1;
x.push_back(x1);
y.push_back(y1);
}
construct_roads(x,y);
return 0;
}
Compilation message
parks.cpp: In function 'void dfs(int)':
parks.cpp:21:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:38:3: error: 'build' was not declared in this scope
38 | build({},{},{},{});
| ^~~~~
parks.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<v1.size();i++) {
| ~^~~~~~~~~~
parks.cpp:53:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int j=i+1;j<v1.size() && j-i<=20;j++) {
| ~^~~~~~~~~~
parks.cpp:71:7: error: 'm' was not declared in this scope
71 | if(m[make_pair(x[i]+2,y[i])]) {
| ^
parks.cpp:76:7: error: 'm' was not declared in this scope
76 | if(m[make_pair(x[i],y[i]+2)]) {
| ^
parks.cpp:81:7: error: 'm' was not declared in this scope
81 | if(m[make_pair(x[i]-2,y[i])]) {
| ^
parks.cpp:86:7: error: 'm' was not declared in this scope
86 | if(m[make_pair(x[i],y[i]-2)]) {
| ^
parks.cpp:97:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=0;i<u2.size();i++) {
| ~^~~~~~~~~~
parks.cpp:146:5: error: 'build' was not declared in this scope
146 | build(u2,v2,a2,b2);
| ^~~~~