#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vi;
const int MAX_N=2e5+10;
vector<vi> ox(MAX_N),oy(MAX_N);
int pa[MAX_N],tam[MAX_N];
void init(int n){
for(int i=0;i<n;i++){
pa[i]=i; tam[i]=1;
}
}
int findset(int i){
if(pa[i]==i) return i;
return pa[i]=findset(pa[i]);
}
bool issameset(int i,int j){
return findset(i)==findset(j);
}
void unionset(int i,int j){
if(!issameset(i,j)){
int u=findset(i),v=findset(j);
pa[u]=v;
tam[v]+=tam[u];
}
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n=x.size();
for(int i=0;i<n;i++){
ox[x[i]].push_back(ii(y[i],i));
oy[y[i]].push_back(ii(x[i],i));
}
for(int i=0;i<MAX_N;i++){
sort(ox[i].begin(),ox[i].end());
sort(oy[i].begin(),oy[i].end());
}
vector<int> ru,rv,ra,rb;
set<ii> ba;
init(n);
for(int i=2;i<MAX_N;i+=2){
int t=oy[i].size();
for(int j=0;j<t-1;j++){
int xi=oy[i][j].first,xj=oy[i][j+1].first;
int u=oy[i][j].second,v=oy[i][j+1].second;
if(xj-xi==2 && !issameset(u,v)){
ru.push_back(oy[i][j].second);
rv.push_back(oy[i][j+1].second);
unionset(u,v);
int xk=oy[i][j+2].first;
auto it=lower_bound(oy[i-2].begin(),oy[i-2].end(),ii(xj,-1));
if(j+2<t && oy[i][j+2].first-xj==2 && it!=oy[i-2].end() && (*it).first==xj){
ra.push_back(xi+1); rb.push_back(i+1);
ba.insert(ii(xi+1,i+1));
it=lower_bound(oy[i+2].begin(),oy[i+2].end(),ii(xi,-1));
int id=it-oy[i+2].begin();
if(it!=oy[i+2].end() && (*it).first==xi && id-1>=0 && oy[i+2][id].first-oy[i+2][id-1].first==2){
int uu=oy[i+2][id].second,vv=oy[i+2][id-1].second;
unionset(uu,vv);
ru.push_back(uu); rv.push_back(vv);
ra.push_back(xi-1); rb.push_back(i+3);
}
}
else{
ra.push_back(xi+1); rb.push_back(i-1);
ba.insert(ii(xi+1,i-1));
}
}
}
}
for(int i=2;i<MAX_N;i+=2){
int t=ox[i].size();
for(int j=0;j<t-1;j++){
int yi=ox[i][j].first,yj=ox[i][j+1].first;
int u=ox[i][j].second,v=ox[i][j+1].second;
if(yj-yi==2){
ru.push_back(u); rv.push_back(v);
unionset(u,v);
if(ba.find(ii(i-1,yi+1))!=ba.end()){
ra.push_back(i+1); rb.push_back(yi+1);
ba.insert(ii(i+1,yi+1));
}
else{
ra.push_back(i-1); rb.push_back(yi+1);
ba.insert(ii(i-1,yi+1));
}
}
}
}
if(tam[findset(0)]!=n) return 0;
build(ru,rv,ra,rb);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:50:6: warning: unused variable 'xk' [-Wunused-variable]
50 | int xk=oy[i][j+2].first;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
17 |
Correct |
10 ms |
9676 KB |
Output is correct |
18 |
Correct |
8 ms |
9676 KB |
Output is correct |
19 |
Correct |
11 ms |
9676 KB |
Output is correct |
20 |
Correct |
9 ms |
9676 KB |
Output is correct |
21 |
Correct |
9 ms |
9676 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
338 ms |
48472 KB |
Output is correct |
24 |
Correct |
8 ms |
9676 KB |
Output is correct |
25 |
Correct |
10 ms |
9972 KB |
Output is correct |
26 |
Correct |
10 ms |
10176 KB |
Output is correct |
27 |
Correct |
12 ms |
10188 KB |
Output is correct |
28 |
Correct |
126 ms |
25284 KB |
Output is correct |
29 |
Correct |
197 ms |
32936 KB |
Output is correct |
30 |
Correct |
272 ms |
40840 KB |
Output is correct |
31 |
Correct |
359 ms |
48644 KB |
Output is correct |
32 |
Correct |
8 ms |
9676 KB |
Output is correct |
33 |
Correct |
9 ms |
9676 KB |
Output is correct |
34 |
Correct |
9 ms |
9676 KB |
Output is correct |
35 |
Correct |
8 ms |
9676 KB |
Output is correct |
36 |
Correct |
10 ms |
9676 KB |
Output is correct |
37 |
Correct |
10 ms |
9704 KB |
Output is correct |
38 |
Correct |
9 ms |
9676 KB |
Output is correct |
39 |
Correct |
8 ms |
9676 KB |
Output is correct |
40 |
Correct |
8 ms |
9604 KB |
Output is correct |
41 |
Correct |
8 ms |
9676 KB |
Output is correct |
42 |
Correct |
9 ms |
9676 KB |
Output is correct |
43 |
Correct |
10 ms |
9932 KB |
Output is correct |
44 |
Correct |
12 ms |
10060 KB |
Output is correct |
45 |
Correct |
136 ms |
24596 KB |
Output is correct |
46 |
Correct |
199 ms |
31928 KB |
Output is correct |
47 |
Correct |
194 ms |
31916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
17 |
Correct |
10 ms |
9676 KB |
Output is correct |
18 |
Correct |
8 ms |
9676 KB |
Output is correct |
19 |
Correct |
11 ms |
9676 KB |
Output is correct |
20 |
Correct |
9 ms |
9676 KB |
Output is correct |
21 |
Correct |
9 ms |
9676 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
338 ms |
48472 KB |
Output is correct |
24 |
Correct |
8 ms |
9676 KB |
Output is correct |
25 |
Correct |
10 ms |
9972 KB |
Output is correct |
26 |
Correct |
10 ms |
10176 KB |
Output is correct |
27 |
Correct |
12 ms |
10188 KB |
Output is correct |
28 |
Correct |
126 ms |
25284 KB |
Output is correct |
29 |
Correct |
197 ms |
32936 KB |
Output is correct |
30 |
Correct |
272 ms |
40840 KB |
Output is correct |
31 |
Correct |
359 ms |
48644 KB |
Output is correct |
32 |
Correct |
8 ms |
9676 KB |
Output is correct |
33 |
Correct |
9 ms |
9676 KB |
Output is correct |
34 |
Correct |
9 ms |
9676 KB |
Output is correct |
35 |
Correct |
8 ms |
9676 KB |
Output is correct |
36 |
Correct |
10 ms |
9676 KB |
Output is correct |
37 |
Correct |
10 ms |
9704 KB |
Output is correct |
38 |
Correct |
9 ms |
9676 KB |
Output is correct |
39 |
Correct |
8 ms |
9676 KB |
Output is correct |
40 |
Correct |
8 ms |
9604 KB |
Output is correct |
41 |
Correct |
8 ms |
9676 KB |
Output is correct |
42 |
Correct |
9 ms |
9676 KB |
Output is correct |
43 |
Correct |
10 ms |
9932 KB |
Output is correct |
44 |
Correct |
12 ms |
10060 KB |
Output is correct |
45 |
Correct |
136 ms |
24596 KB |
Output is correct |
46 |
Correct |
199 ms |
31928 KB |
Output is correct |
47 |
Correct |
194 ms |
31916 KB |
Output is correct |
48 |
Correct |
9 ms |
9700 KB |
Output is correct |
49 |
Correct |
9 ms |
9676 KB |
Output is correct |
50 |
Correct |
9 ms |
9676 KB |
Output is correct |
51 |
Incorrect |
11 ms |
9676 KB |
Tree @(5, 5) appears more than once: for edges on positions 3 and 6 |
52 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
17 |
Correct |
9 ms |
9708 KB |
Output is correct |
18 |
Correct |
10 ms |
9676 KB |
Output is correct |
19 |
Correct |
10 ms |
9676 KB |
Output is correct |
20 |
Correct |
349 ms |
39408 KB |
Output is correct |
21 |
Correct |
310 ms |
39108 KB |
Output is correct |
22 |
Correct |
288 ms |
38424 KB |
Output is correct |
23 |
Correct |
216 ms |
34816 KB |
Output is correct |
24 |
Correct |
85 ms |
18296 KB |
Output is correct |
25 |
Correct |
140 ms |
30488 KB |
Output is correct |
26 |
Correct |
189 ms |
30424 KB |
Output is correct |
27 |
Correct |
220 ms |
36760 KB |
Output is correct |
28 |
Correct |
223 ms |
36844 KB |
Output is correct |
29 |
Correct |
210 ms |
36760 KB |
Output is correct |
30 |
Correct |
210 ms |
36808 KB |
Output is correct |
31 |
Correct |
9 ms |
9676 KB |
Output is correct |
32 |
Incorrect |
24 ms |
12040 KB |
Tree @(185703, 20399) appears more than once: for edges on positions 1266 and 7203 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
17 |
Correct |
269 ms |
38484 KB |
Output is correct |
18 |
Correct |
262 ms |
38500 KB |
Output is correct |
19 |
Incorrect |
295 ms |
38808 KB |
Tree @(49999, 7) appears more than once: for edges on positions 1 and 149992 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
9 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9676 KB |
Output is correct |
4 |
Correct |
9 ms |
9704 KB |
Output is correct |
5 |
Correct |
9 ms |
9676 KB |
Output is correct |
6 |
Correct |
9 ms |
9700 KB |
Output is correct |
7 |
Correct |
10 ms |
9600 KB |
Output is correct |
8 |
Correct |
9 ms |
9704 KB |
Output is correct |
9 |
Correct |
139 ms |
27880 KB |
Output is correct |
10 |
Correct |
17 ms |
11596 KB |
Output is correct |
11 |
Correct |
65 ms |
19476 KB |
Output is correct |
12 |
Correct |
23 ms |
12492 KB |
Output is correct |
13 |
Correct |
37 ms |
15712 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
12 ms |
9932 KB |
Output is correct |
16 |
Correct |
178 ms |
27256 KB |
Output is correct |
17 |
Correct |
10 ms |
9676 KB |
Output is correct |
18 |
Correct |
8 ms |
9676 KB |
Output is correct |
19 |
Correct |
11 ms |
9676 KB |
Output is correct |
20 |
Correct |
9 ms |
9676 KB |
Output is correct |
21 |
Correct |
9 ms |
9676 KB |
Output is correct |
22 |
Correct |
8 ms |
9676 KB |
Output is correct |
23 |
Correct |
338 ms |
48472 KB |
Output is correct |
24 |
Correct |
8 ms |
9676 KB |
Output is correct |
25 |
Correct |
10 ms |
9972 KB |
Output is correct |
26 |
Correct |
10 ms |
10176 KB |
Output is correct |
27 |
Correct |
12 ms |
10188 KB |
Output is correct |
28 |
Correct |
126 ms |
25284 KB |
Output is correct |
29 |
Correct |
197 ms |
32936 KB |
Output is correct |
30 |
Correct |
272 ms |
40840 KB |
Output is correct |
31 |
Correct |
359 ms |
48644 KB |
Output is correct |
32 |
Correct |
8 ms |
9676 KB |
Output is correct |
33 |
Correct |
9 ms |
9676 KB |
Output is correct |
34 |
Correct |
9 ms |
9676 KB |
Output is correct |
35 |
Correct |
8 ms |
9676 KB |
Output is correct |
36 |
Correct |
10 ms |
9676 KB |
Output is correct |
37 |
Correct |
10 ms |
9704 KB |
Output is correct |
38 |
Correct |
9 ms |
9676 KB |
Output is correct |
39 |
Correct |
8 ms |
9676 KB |
Output is correct |
40 |
Correct |
8 ms |
9604 KB |
Output is correct |
41 |
Correct |
8 ms |
9676 KB |
Output is correct |
42 |
Correct |
9 ms |
9676 KB |
Output is correct |
43 |
Correct |
10 ms |
9932 KB |
Output is correct |
44 |
Correct |
12 ms |
10060 KB |
Output is correct |
45 |
Correct |
136 ms |
24596 KB |
Output is correct |
46 |
Correct |
199 ms |
31928 KB |
Output is correct |
47 |
Correct |
194 ms |
31916 KB |
Output is correct |
48 |
Correct |
9 ms |
9700 KB |
Output is correct |
49 |
Correct |
9 ms |
9676 KB |
Output is correct |
50 |
Correct |
9 ms |
9676 KB |
Output is correct |
51 |
Incorrect |
11 ms |
9676 KB |
Tree @(5, 5) appears more than once: for edges on positions 3 and 6 |
52 |
Halted |
0 ms |
0 KB |
- |