#include "parks.h"
#include <map>
#include <utility>
#include <queue>
using namespace std;
map < pair < int , int > , int > all,con;
map < pair < int , int > , pair < int , int > > road;
vector < int > u,v,a,b;
queue < pair < int , int > > BFS;
int Father[200005];
int Find(int here)
{
if(Father[here]==here) return here;
Father[here]=Find(Father[here]);
return Father[here];
}
int construct_roads(vector < int > x, vector < int > y)
{
int N=x.size(),i,t,aa,bb,ok=1,xx,yy,how=0;
for(i=0;i<N;i++)
{
all[make_pair(x[i],y[i])]=i;
Father[i]=i;
}
for(i=0;i<N;i++)
{
if(all.find(make_pair(x[i],y[i]+2))!=all.end())
{
how++;
t=all[make_pair(x[i],y[i]+2)];
road[make_pair(x[i],y[i]+1)]=make_pair(i,t);
con[make_pair(x[i]+1,y[i]+1)]++;
con[make_pair(x[i]-1,y[i]+1)]++;
aa=Find(i);
bb=Find(t);
if(aa!=bb) Father[aa]=bb;
}
if(all.find(make_pair(x[i]+2,y[i]))!=all.end())
{
how++;
t=all[make_pair(x[i]+2,y[i])];
road[make_pair(x[i]+1,y[i])]=make_pair(i,t);
con[make_pair(x[i]+1,y[i]+1)]++;
con[make_pair(x[i]+1,y[i]-1)]++;
aa=Find(i);
bb=Find(t);
if(aa!=bb) Father[aa]=bb;
}
}
t=Find(0);
for(i=0;i<N;i++) if(t!=Find(i)) ok=0;
if(ok==0) return ok;
for(auto i:con) if(i.second==1) BFS.push(i.first);
while(!BFS.empty())
{
xx=BFS.front().first;
yy=BFS.front().second;
BFS.pop();
if(con.find(make_pair(xx,yy))==con.end()) continue;
if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy;
if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy;
if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1;
if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1;
u.push_back(road[make_pair(aa,bb)].first);
v.push_back(road[make_pair(aa,bb)].second);
a.push_back(xx);
b.push_back(yy);
road.erase(make_pair(aa,bb));
if(aa%2==0)
{
con[make_pair(aa-1,bb)]--;
con[make_pair(aa+1,bb)]--;
if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
}
else
{
con[make_pair(aa,bb-1)]--;
con[make_pair(aa,bb+1)]--;
if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
}
}
/*while(!road.empty())
{
BFS.push(con.begin()->first);
while(!BFS.empty())
{
xx=BFS.front().first;
yy=BFS.front().second;
BFS.pop();
if(con[make_pair(xx,yy)]==0||con[make_pair(xx,yy)]<=0) continue;
if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy;
if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy;
if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1;
if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1;
con.erase(make_pair(xx,yy));
u.push_back(road[make_pair(aa,bb)].first);
v.push_back(road[make_pair(aa,bb)].second);
a.push_back(xx);
b.push_back(yy);
road.erase(make_pair(aa,bb));
if(aa%2==0)
{
con[make_pair(aa-1,bb)]--;
con[make_pair(aa+1,bb)]--;
if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
}
else
{
con[make_pair(aa,bb-1)]--;
con[make_pair(aa,bb+1)]--;
if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
}
}
}*/
if(ok) build(u,v,a,b);
return ok;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:86:32: warning: 'bb' may be used uninitialized in this function [-Wmaybe-uninitialized]
86 | con[make_pair(aa,bb+1)]--;
| ~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Pair u[1]=0 @(2, 2) and v[1]=0 @(2, 2) does not form a valid edge (distance != 2) |
3 |
Halted |
0 ms |
0 KB |
- |