#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
const int mxn=2e5+5;
int n,x[mxn],y[mxn],p[mxn],sz[mxn];
int ax[4]={1,1,-1,-1};
int ay[4]={-1,1,1,-1};
vector<pii>vt;
map<pii,int>mp;
int fnd(int u){
if(p[u]==u) return u;
return p[u]=fnd(p[u]);
}
void Union(int u,int v){
u=fnd(u);
v=fnd(v);
if(u!=v){
if(sz[u]>sz[v]) swap(u,v);
sz[u]+=sz[v];
p[v]=u;
}
}
int construct_roads(vector<int> xx, vector<int> yy) {
n=xx.size();
if(n==1){
build({},{},{},{});
return 1;
}
for(int i=0;i<n;i++){
vt.pb({xx[i],yy[i]});
mp[{xx[i],yy[i]}]=i+1;
x[i+1]=xx[i];
y[i+1]=yy[i];
}
//for(int i=1;i<=n;i++){
//if(!mp[{x[i],y[i]+2}]&&!mp[{x[i],y[i]-2}]&&!mp[{x[i]+2,y[i]}]&&!mp[{x[i]-2,y[i]}]) return 0;
//}
sort(all(vt));
//for(int i=1;i<n;i++){
//if(abs(vt[i].F-vt[i-1].F)>2||abs(vt[i].S-vt[i-1].S)>2) return 0;
//}
set<pii>st;
for(int i=1;i<=n;i++){
st.insert({x[i]-1,y[i]-1});
st.insert({x[i]+1,y[i]-1});
st.insert({x[i]-1,y[i]+1});
st.insert({x[i]+1,y[i]+1});
}
for(int i=1;i<=n;i++){
p[i]=i;
sz[i]=1;
}
vector<int>idxl,idxr,posl,posr;
int cnt=n-1;
int say=15;
while(cnt&&st.size()){
auto cur=st.begin();
while(cur!=st.end()){
auto it=cur++;
int l=it->F;
int r=it->S;
set<int>dif;
vector<int>idx;
//cout<<"OK"<<endl;
for(int i=0;i<4;i++){
int u=l+ax[i],v=r+ay[i];
int node=mp[{u,v}];
if(node>0&&!dif.count(fnd(node))){
dif.insert(fnd(node));
idx.pb(node);
}
}
//cout<<dif.size()<<endl;
if(dif.size()==1){
st.erase(it);
continue;
}
//if(dif.size()>2) continue;
int q=-1,q2=1;
for(int i=0;i<idx.size();i++){
for(int j=i+1;j<idx.size();j++){
if(abs(x[idx[i]]-x[idx[j]])+abs(y[idx[i]]-y[idx[j]])==2){
q=i,
q2=j;
break;
}
}
}
if(q==-1){
st.erase(it);
continue;
}
idxl.pb(idx[q]-1);
idxr.pb(idx[q2]-1);
//cout<<idx[0]<<' '<<idx[1]<<endl;
posl.pb(l);
posr.pb(r);
Union(idx[q],idx[q2]);
cnt--;
st.erase(it);
}
//cnt--;
}
if(cnt) return 0;
build(idxl,idxr,posl,posr);
return 1;
}
/*
cd "c:\Users\User\Desktop\parks (4)\cpp\" ; if ($?) { g++ parks.cpp grader.cpp -o parks } ; if ($?) { .\a.exe }
5
4 4
4 6
6 4
4 2
2 4
*/
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i=0;i<idx.size();i++){
| ~^~~~~~~~~~~
parks.cpp:92:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j=i+1;j<idx.size();j++){
| ~^~~~~~~~~~~
parks.cpp:66:9: warning: unused variable 'say' [-Wunused-variable]
66 | int say=15;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
663 ms |
55388 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
980 KB |
Output is correct |
27 |
Correct |
6 ms |
1348 KB |
Output is correct |
28 |
Correct |
191 ms |
22672 KB |
Output is correct |
29 |
Correct |
308 ms |
33280 KB |
Output is correct |
30 |
Correct |
439 ms |
44868 KB |
Output is correct |
31 |
Correct |
594 ms |
55040 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
312 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
4 ms |
852 KB |
Output is correct |
44 |
Correct |
6 ms |
1236 KB |
Output is correct |
45 |
Correct |
308 ms |
31984 KB |
Output is correct |
46 |
Correct |
463 ms |
46772 KB |
Output is correct |
47 |
Correct |
463 ms |
46872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
663 ms |
55388 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
980 KB |
Output is correct |
27 |
Correct |
6 ms |
1348 KB |
Output is correct |
28 |
Correct |
191 ms |
22672 KB |
Output is correct |
29 |
Correct |
308 ms |
33280 KB |
Output is correct |
30 |
Correct |
439 ms |
44868 KB |
Output is correct |
31 |
Correct |
594 ms |
55040 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
312 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
4 ms |
852 KB |
Output is correct |
44 |
Correct |
6 ms |
1236 KB |
Output is correct |
45 |
Correct |
308 ms |
31984 KB |
Output is correct |
46 |
Correct |
463 ms |
46772 KB |
Output is correct |
47 |
Correct |
463 ms |
46872 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
0 ms |
312 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
316 KB |
Output is correct |
53 |
Correct |
1 ms |
280 KB |
Output is correct |
54 |
Incorrect |
0 ms |
212 KB |
Solution announced impossible, but it is possible. |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Incorrect |
608 ms |
50716 KB |
Solution announced impossible, but it is possible. |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
17 |
Correct |
671 ms |
62204 KB |
Output is correct |
18 |
Correct |
667 ms |
61820 KB |
Output is correct |
19 |
Incorrect |
596 ms |
52088 KB |
Solution announced impossible, but it is possible. |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
273 ms |
34320 KB |
Output is correct |
10 |
Correct |
19 ms |
3516 KB |
Output is correct |
11 |
Correct |
118 ms |
17040 KB |
Output is correct |
12 |
Correct |
29 ms |
4996 KB |
Output is correct |
13 |
Correct |
83 ms |
13252 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
964 KB |
Output is correct |
16 |
Correct |
288 ms |
34476 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
663 ms |
55388 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
980 KB |
Output is correct |
27 |
Correct |
6 ms |
1348 KB |
Output is correct |
28 |
Correct |
191 ms |
22672 KB |
Output is correct |
29 |
Correct |
308 ms |
33280 KB |
Output is correct |
30 |
Correct |
439 ms |
44868 KB |
Output is correct |
31 |
Correct |
594 ms |
55040 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
312 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
4 ms |
852 KB |
Output is correct |
44 |
Correct |
6 ms |
1236 KB |
Output is correct |
45 |
Correct |
308 ms |
31984 KB |
Output is correct |
46 |
Correct |
463 ms |
46772 KB |
Output is correct |
47 |
Correct |
463 ms |
46872 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
0 ms |
312 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
316 KB |
Output is correct |
53 |
Correct |
1 ms |
280 KB |
Output is correct |
54 |
Incorrect |
0 ms |
212 KB |
Solution announced impossible, but it is possible. |
55 |
Halted |
0 ms |
0 KB |
- |