#include "parks.h"
#include <bits/stdc++.h>
#define N 200005
using namespace std;
int par[N];
int find(int a){
if(a == par[a])return a;
return par[a] = find(par[a]);
}
bool merge(int a,int b){
a = find(a);
b = find(b);
if(a == b)
return 0;
par[a] = b;
return 1;
}
int construct_roads(vector<int> x, vector<int> y) {
if (x.size() == 1) {
build({}, {}, {}, {});
return 1;
}
int n = x.size();
map<pair<int,int>,int> mp;
for(int i = 0;i<n;i++){
par[i] = i;
mp[{x[i],y[i]}] = i;
}
vector<int> u, v, a, b;
vector<pair<int,int>> ord;
for(int i = 0;i<n;i++){
for(auto dx:{-2,0,2}){
for(auto dy:{-2,0,2}){
if(abs(dx) + abs(dy) != 2)continue;
if(!mp.count({x[i]+dx,y[i]+dy}))continue;
if(merge(i,mp[{x[i]+dx,y[i]+dy}])){
ord.push_back({i,mp[{x[i]+dx,y[i]+dy}]});
//u.push_back(i);
//v.push_back(mp[{x[i]+dx,y[i]+dy}]);
}
}
}
}
sort(ord.begin(),ord.end(),[&](pair<int,int> c,pair<int,int> d){
if(max(x[c.first],x[c.second]) != max(x[d.first],x[d.second])){
return max(x[c.first],x[c.second]) < max(x[d.first],x[d.second]);
}
return max(y[c.first],y[c.second]) < max(y[d.first],y[d.second]);
});
for(auto c:ord){
u.push_back(c.first);
v.push_back(c.second);
}
if(u.size() != n-1)
return 0;
a.assign(n-1,-1);
b.assign(n-1,-1);
map<pair<int,int>,int> used;
map<int,int> cnt;
map<pair<int,int>,vector<int>> mpp;
set<pair<int,int>> s;
for(int i = 0;i<n-1;i++){
for(auto dx:{-1,1}){
for(auto dy:{-1,1}){
if(abs(x[u[i]]+dx - x[v[i]]) != 1 || abs(y[u[i]]+dy - y[v[i]]) != 1)continue;
cnt[i]++;
mpp[{x[u[i]]+dx,y[u[i]]+dy}].push_back(i);
}
}
s.insert({cnt[i],i});
}
while(s.size()){
auto tp = *s.begin();
if(tp.first == 0){
assert(0);
}
s.erase(tp);
int i = tp.second;
bool ok = 0;
for(auto dx:{-1,1}){
for(auto dy:{-1,1}){
if(ok)continue;
if(used.count({x[u[i]]+dx,y[u[i]]+dy}))continue;
if(abs(x[u[i]]+dx - x[v[i]]) != 1 || abs(y[u[i]]+dy - y[v[i]]) != 1)continue;
used[{x[u[i]]+dx,y[u[i]]+dy}] = 1;
a[i] = x[u[i]] + dx;
b[i] = y[u[i]] + dy;
for(auto z:mpp[{x[u[i]]+dx,y[u[i]]+dy}]){
if(a[z] != -1)continue;
s.erase({cnt[z],z});
cnt[z]--;
s.insert({cnt[z],z});
}
ok = 1;
}
}
}
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:54:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | if(u.size() != n-1)
| ~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
312 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 |
312 KB |
Output is correct |
23 |
Correct |
1261 ms |
87896 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
580 KB |
Output is correct |
27 |
Correct |
5 ms |
724 KB |
Output is correct |
28 |
Correct |
364 ms |
35288 KB |
Output is correct |
29 |
Correct |
584 ms |
52892 KB |
Output is correct |
30 |
Correct |
870 ms |
70428 KB |
Output is correct |
31 |
Correct |
1103 ms |
87848 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 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 |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
612 KB |
Output is correct |
45 |
Correct |
455 ms |
46384 KB |
Output is correct |
46 |
Correct |
720 ms |
66928 KB |
Output is correct |
47 |
Correct |
714 ms |
66812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
312 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 |
312 KB |
Output is correct |
23 |
Correct |
1261 ms |
87896 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
580 KB |
Output is correct |
27 |
Correct |
5 ms |
724 KB |
Output is correct |
28 |
Correct |
364 ms |
35288 KB |
Output is correct |
29 |
Correct |
584 ms |
52892 KB |
Output is correct |
30 |
Correct |
870 ms |
70428 KB |
Output is correct |
31 |
Correct |
1103 ms |
87848 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 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 |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
612 KB |
Output is correct |
45 |
Correct |
455 ms |
46384 KB |
Output is correct |
46 |
Correct |
720 ms |
66928 KB |
Output is correct |
47 |
Correct |
714 ms |
66812 KB |
Output is correct |
48 |
Correct |
0 ms |
312 KB |
Output is correct |
49 |
Correct |
1 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 |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Runtime error |
822 ms |
137416 KB |
Execution killed with signal 6 |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1076 ms |
83840 KB |
Output is correct |
21 |
Correct |
1020 ms |
83724 KB |
Output is correct |
22 |
Correct |
1143 ms |
84900 KB |
Output is correct |
23 |
Correct |
871 ms |
90500 KB |
Output is correct |
24 |
Correct |
381 ms |
18452 KB |
Output is correct |
25 |
Correct |
661 ms |
22744 KB |
Output is correct |
26 |
Correct |
536 ms |
22768 KB |
Output is correct |
27 |
Correct |
1041 ms |
105488 KB |
Output is correct |
28 |
Correct |
999 ms |
105532 KB |
Output is correct |
29 |
Correct |
1176 ms |
105348 KB |
Output is correct |
30 |
Correct |
1133 ms |
105468 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
56 ms |
6948 KB |
Output is correct |
33 |
Correct |
121 ms |
9788 KB |
Output is correct |
34 |
Correct |
937 ms |
85132 KB |
Output is correct |
35 |
Correct |
19 ms |
1484 KB |
Output is correct |
36 |
Correct |
93 ms |
6128 KB |
Output is correct |
37 |
Correct |
219 ms |
11744 KB |
Output is correct |
38 |
Correct |
369 ms |
36424 KB |
Output is correct |
39 |
Correct |
559 ms |
49352 KB |
Output is correct |
40 |
Correct |
734 ms |
62792 KB |
Output is correct |
41 |
Correct |
917 ms |
76404 KB |
Output is correct |
42 |
Correct |
1146 ms |
89460 KB |
Output is correct |
43 |
Correct |
1 ms |
308 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
308 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
312 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
2 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
644 KB |
Output is correct |
56 |
Correct |
449 ms |
46232 KB |
Output is correct |
57 |
Correct |
735 ms |
66764 KB |
Output is correct |
58 |
Correct |
708 ms |
66756 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
312 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1069 ms |
105600 KB |
Output is correct |
63 |
Correct |
1116 ms |
105560 KB |
Output is correct |
64 |
Correct |
1080 ms |
105132 KB |
Output is correct |
65 |
Correct |
5 ms |
724 KB |
Output is correct |
66 |
Correct |
12 ms |
1152 KB |
Output is correct |
67 |
Correct |
511 ms |
45496 KB |
Output is correct |
68 |
Correct |
880 ms |
68196 KB |
Output is correct |
69 |
Correct |
1262 ms |
90940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
17 |
Correct |
1173 ms |
105224 KB |
Output is correct |
18 |
Correct |
1098 ms |
105376 KB |
Output is correct |
19 |
Correct |
1101 ms |
83692 KB |
Output is correct |
20 |
Correct |
1089 ms |
86412 KB |
Output is correct |
21 |
Correct |
906 ms |
85696 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
145 ms |
14616 KB |
Output is correct |
24 |
Correct |
41 ms |
2592 KB |
Output is correct |
25 |
Correct |
149 ms |
8636 KB |
Output is correct |
26 |
Correct |
297 ms |
13936 KB |
Output is correct |
27 |
Correct |
457 ms |
45420 KB |
Output is correct |
28 |
Correct |
616 ms |
57016 KB |
Output is correct |
29 |
Correct |
777 ms |
68392 KB |
Output is correct |
30 |
Correct |
987 ms |
78988 KB |
Output is correct |
31 |
Correct |
1152 ms |
90264 KB |
Output is correct |
32 |
Correct |
1087 ms |
93492 KB |
Output is correct |
33 |
Correct |
1014 ms |
105760 KB |
Output is correct |
34 |
Correct |
7 ms |
852 KB |
Output is correct |
35 |
Correct |
15 ms |
1364 KB |
Output is correct |
36 |
Correct |
480 ms |
45612 KB |
Output is correct |
37 |
Correct |
793 ms |
68736 KB |
Output is correct |
38 |
Correct |
1208 ms |
91476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
556 ms |
52908 KB |
Output is correct |
10 |
Correct |
34 ms |
5652 KB |
Output is correct |
11 |
Correct |
233 ms |
28484 KB |
Output is correct |
12 |
Correct |
45 ms |
8268 KB |
Output is correct |
13 |
Correct |
52 ms |
5268 KB |
Output is correct |
14 |
Correct |
3 ms |
320 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
502 ms |
52880 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
312 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 |
312 KB |
Output is correct |
23 |
Correct |
1261 ms |
87896 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
580 KB |
Output is correct |
27 |
Correct |
5 ms |
724 KB |
Output is correct |
28 |
Correct |
364 ms |
35288 KB |
Output is correct |
29 |
Correct |
584 ms |
52892 KB |
Output is correct |
30 |
Correct |
870 ms |
70428 KB |
Output is correct |
31 |
Correct |
1103 ms |
87848 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 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 |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
612 KB |
Output is correct |
45 |
Correct |
455 ms |
46384 KB |
Output is correct |
46 |
Correct |
720 ms |
66928 KB |
Output is correct |
47 |
Correct |
714 ms |
66812 KB |
Output is correct |
48 |
Correct |
0 ms |
312 KB |
Output is correct |
49 |
Correct |
1 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 |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Runtime error |
822 ms |
137416 KB |
Execution killed with signal 6 |
56 |
Halted |
0 ms |
0 KB |
- |