#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define maxn 200005
#define pb push_back
typedef pair<int,int> ii;
int dx[2]={2,0},dy[2]={0,2};
bool vis[maxn],bad[maxn],weird[maxn];
vector<ii> squares;
map<ii,int> fid,sid,eid,use;
vector<int> AL[maxn],par[maxn];
int p[maxn],rnk[maxn];
int fp(int i){return (p[i]==i)?i:p[i]=fp(p[i]);}
void join(int x,int y){
x=fp(x),y=fp(y);
if(x==y)return;
if(rnk[x]<rnk[y])p[x]=y;
else p[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
}
inline bool fin(int x,int y){
return fid.find(ii(x,y))!=fid.end();
}
inline bool sin(int x,int y){
return sid.find(ii(x,y))!=sid.end();
}
inline bool ein(int x,int y){
return eid.find(ii(x,y))!=eid.end();
}
void dfs(int u,int p){
//printf("dfs: %d %d\n",u,p);
if(p!=-1){
auto[x,y]=squares[u];
ii f1,f2;
if(p==par[u][0]){
if((x+y)%4==0){//vertical
f1={x-1,y-1},f2={x-1,y+1};
}
else{//horizontal
f1={x-1,y-1},f2={x+1,y-1};
}
}
else{
if((x+y)%4==0){//vertical
f1={x+1,y-1},f2={x+1,y+1};
}
else{//horizontal
f1={x-1,y+1},f2={x+1,y+1};
}
}
int i1=fid[f1],i2=fid[f2];
if(ein(i1,i2))bad[eid[{i1,i2}]]=true;
else bad[eid[{i2,i1}]]=true;
}
for(int v:AL[u]){
if(vis[v])continue;
vis[v]=true;
dfs(v,u);
}
}
int construct_roads(vector<int> x,vector<int> y){
int n=x.size();
for(int i=0;i<n;++i){
fid[{x[i],y[i]}]=i;
}
int scnt=0,ecnt=0;
for(int i=0;i<n;++i){
if(fin(x[i]+2,y[i])&&fin(x[i],y[i]+2)&&fin(x[i]+2,y[i]+2)){
sid[{x[i]+1,y[i]+1}]=scnt++;
squares.push_back({x[i]+1,y[i]+1});
}
for(int j=0;j<2;++j){
int nx=x[i]+dx[j],ny=y[i]+dy[j];
if(fin(nx,ny))eid[{i,fid[{nx,ny}]}]=ecnt++;
}
}
for(auto it=sid.begin();it!=sid.end();++it){
auto[x,y]=it->fi;
assert(it->se!=scnt);
if((x+y)%4==0){//vertical
for(int dx:{-2,2}){
if(sin(x+dx,y)){
AL[sid[{x+dx,y}]].push_back(it->se);
par[it->se].push_back(sid[{x+dx,y}]);
}
else{
AL[scnt].push_back(it->se);
par[it->se].push_back(scnt);
}
}
}
else{//horizontal
for(int dy:{-2,2}){
if(sin(x,y+dy)){
AL[sid[{x,y+dy}]].push_back(it->se);
par[it->se].push_back(sid[{x,y+dy}]);
}
else{
AL[scnt].push_back(it->se);
par[it->se].push_back(scnt);
}
}
}
}
vis[scnt]=true;
dfs(scnt,-1);
/*
for(auto it=sid.begin();it!=sid.end();++it){
auto[x,y]=it->fi;
if((x+y)%4==0){//vertical
int cnt=0;
for(int dx:{-1,1}){
ii f1={x+dx,y-1},f2={x+dx,y+1};
int i1=fid[f1],i2=fid[f2];
if(ein(i1,i2)&&!bad[eid[{i1,i2}]])++cnt;
else if(ein(i2,i1)&&!bad[eid[{i2,i1}]])++cnt;
}
if(cnt==2)assert(false);
}
else{//horizontal
int cnt=0;
for(int dy:{-1,1}){
ii f1={x-1,y+dy},f2={x+1,y+dy};
int i1=fid[f1],i2=fid[f2];
if(ein(i1,i2)&&!bad[eid[{i1,i2}]])++cnt;
else if(ein(i2,i1)&&!bad[eid[{i2,i1}]])++cnt;
}
if(cnt==2)assert(false);
}
}
*/
for(int i=0;i<n;++i)p[i]=i;
vector<int> u,v,a,b;
for(auto it=eid.begin();it!=eid.end();++it){
if(bad[it->se])continue;
auto[i,j]=it->fi;
if(fp(i)==fp(j))continue;
join(i,j);
int x1=x[i],x2=x[j],y1=y[i],y2=y[j];
int bx,by;
if(x1==x2){
if((x1-1+(y1+y2)/2)%4==0)bx=x1-1,by=(y1+y2)/2;
else bx=x1+1,by=(y1+y2)/2;
}
else{
if(((x1+x2)/2+y1-1)%4!=0)bx=(x1+x2)/2,by=y1-1;
else bx=(x1+x2)/2,by=y1+1;
}
u.pb(i),v.pb(j),a.pb(bx),b.pb(by);
int idk=use[{bx,by}];
use[{bx,by}]=true;
}
for(int i=0;i<n;++i){
if(fp(i)!=fp(0))return 0;
}
build(u,v,a,b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:161:7: warning: unused variable 'idk' [-Wunused-variable]
161 | int idk=use[{bx,by}];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9700 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9620 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
6 ms |
9684 KB |
Output is correct |
23 |
Incorrect |
805 ms |
70288 KB |
Solution announced impossible, but it is possible. |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9700 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9620 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
6 ms |
9684 KB |
Output is correct |
23 |
Incorrect |
805 ms |
70288 KB |
Solution announced impossible, but it is possible. |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9700 KB |
Output is correct |
19 |
Correct |
5 ms |
9696 KB |
Output is correct |
20 |
Correct |
621 ms |
61792 KB |
Output is correct |
21 |
Correct |
616 ms |
61928 KB |
Output is correct |
22 |
Correct |
599 ms |
61856 KB |
Output is correct |
23 |
Correct |
542 ms |
54112 KB |
Output is correct |
24 |
Correct |
291 ms |
26600 KB |
Output is correct |
25 |
Correct |
640 ms |
55420 KB |
Output is correct |
26 |
Correct |
527 ms |
55428 KB |
Output is correct |
27 |
Correct |
589 ms |
61884 KB |
Output is correct |
28 |
Correct |
606 ms |
61876 KB |
Output is correct |
29 |
Correct |
668 ms |
61804 KB |
Output is correct |
30 |
Correct |
652 ms |
61832 KB |
Output is correct |
31 |
Correct |
5 ms |
9696 KB |
Output is correct |
32 |
Correct |
33 ms |
13832 KB |
Output is correct |
33 |
Correct |
99 ms |
18420 KB |
Output is correct |
34 |
Correct |
584 ms |
61924 KB |
Output is correct |
35 |
Correct |
21 ms |
12116 KB |
Output is correct |
36 |
Correct |
104 ms |
21460 KB |
Output is correct |
37 |
Correct |
254 ms |
32804 KB |
Output is correct |
38 |
Correct |
209 ms |
30784 KB |
Output is correct |
39 |
Correct |
355 ms |
38532 KB |
Output is correct |
40 |
Correct |
446 ms |
46356 KB |
Output is correct |
41 |
Correct |
530 ms |
54140 KB |
Output is correct |
42 |
Correct |
704 ms |
61876 KB |
Output is correct |
43 |
Correct |
6 ms |
9684 KB |
Output is correct |
44 |
Correct |
5 ms |
9684 KB |
Output is correct |
45 |
Correct |
5 ms |
9684 KB |
Output is correct |
46 |
Correct |
5 ms |
9620 KB |
Output is correct |
47 |
Correct |
5 ms |
9704 KB |
Output is correct |
48 |
Correct |
5 ms |
9704 KB |
Output is correct |
49 |
Correct |
6 ms |
9684 KB |
Output is correct |
50 |
Correct |
5 ms |
9684 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
5 ms |
9684 KB |
Output is correct |
53 |
Correct |
5 ms |
9684 KB |
Output is correct |
54 |
Correct |
8 ms |
10068 KB |
Output is correct |
55 |
Correct |
9 ms |
10356 KB |
Output is correct |
56 |
Correct |
232 ms |
35976 KB |
Output is correct |
57 |
Correct |
423 ms |
47636 KB |
Output is correct |
58 |
Correct |
432 ms |
47596 KB |
Output is correct |
59 |
Correct |
5 ms |
9652 KB |
Output is correct |
60 |
Correct |
5 ms |
9684 KB |
Output is correct |
61 |
Correct |
5 ms |
9704 KB |
Output is correct |
62 |
Correct |
561 ms |
61776 KB |
Output is correct |
63 |
Correct |
540 ms |
62000 KB |
Output is correct |
64 |
Correct |
514 ms |
61612 KB |
Output is correct |
65 |
Correct |
11 ms |
10580 KB |
Output is correct |
66 |
Correct |
16 ms |
11476 KB |
Output is correct |
67 |
Correct |
245 ms |
35984 KB |
Output is correct |
68 |
Correct |
409 ms |
48952 KB |
Output is correct |
69 |
Correct |
639 ms |
61792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
17 |
Correct |
638 ms |
61776 KB |
Output is correct |
18 |
Correct |
571 ms |
61768 KB |
Output is correct |
19 |
Correct |
626 ms |
61860 KB |
Output is correct |
20 |
Correct |
733 ms |
64928 KB |
Output is correct |
21 |
Correct |
591 ms |
56628 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
75 ms |
18648 KB |
Output is correct |
24 |
Correct |
42 ms |
14712 KB |
Output is correct |
25 |
Correct |
209 ms |
26860 KB |
Output is correct |
26 |
Correct |
327 ms |
38908 KB |
Output is correct |
27 |
Correct |
323 ms |
37348 KB |
Output is correct |
28 |
Correct |
394 ms |
44256 KB |
Output is correct |
29 |
Correct |
521 ms |
50884 KB |
Output is correct |
30 |
Correct |
640 ms |
57736 KB |
Output is correct |
31 |
Correct |
738 ms |
64580 KB |
Output is correct |
32 |
Correct |
666 ms |
64468 KB |
Output is correct |
33 |
Correct |
562 ms |
62012 KB |
Output is correct |
34 |
Correct |
13 ms |
10836 KB |
Output is correct |
35 |
Correct |
20 ms |
11824 KB |
Output is correct |
36 |
Correct |
263 ms |
36764 KB |
Output is correct |
37 |
Correct |
426 ms |
50000 KB |
Output is correct |
38 |
Incorrect |
595 ms |
57064 KB |
Solution announced impossible, but it is possible. |
39 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9680 KB |
Output is correct |
7 |
Correct |
5 ms |
9696 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
267 ms |
35992 KB |
Output is correct |
10 |
Correct |
20 ms |
12492 KB |
Output is correct |
11 |
Correct |
94 ms |
23832 KB |
Output is correct |
12 |
Correct |
27 ms |
13908 KB |
Output is correct |
13 |
Correct |
72 ms |
19928 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
10 ms |
10068 KB |
Output is correct |
16 |
Correct |
211 ms |
35972 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9700 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9620 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
6 ms |
9684 KB |
Output is correct |
23 |
Incorrect |
805 ms |
70288 KB |
Solution announced impossible, but it is possible. |
24 |
Halted |
0 ms |
0 KB |
- |