#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;
map<pair<int,int>,int> used;
for(int i = 0;i<n-1;i++){
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.push_back(x[u[i]] + dx);
b.push_back(y[u[i]] + dy);
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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
715 ms |
41296 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
580 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
724 KB |
Output is correct |
28 |
Correct |
221 ms |
17032 KB |
Output is correct |
29 |
Correct |
363 ms |
25200 KB |
Output is correct |
30 |
Correct |
528 ms |
33528 KB |
Output is correct |
31 |
Correct |
696 ms |
41780 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
251 ms |
21304 KB |
Output is correct |
46 |
Correct |
466 ms |
30448 KB |
Output is correct |
47 |
Correct |
504 ms |
30560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
715 ms |
41296 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
580 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
724 KB |
Output is correct |
28 |
Correct |
221 ms |
17032 KB |
Output is correct |
29 |
Correct |
363 ms |
25200 KB |
Output is correct |
30 |
Correct |
528 ms |
33528 KB |
Output is correct |
31 |
Correct |
696 ms |
41780 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
251 ms |
21304 KB |
Output is correct |
46 |
Correct |
466 ms |
30448 KB |
Output is correct |
47 |
Correct |
504 ms |
30560 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
0 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 |
312 KB |
Output is correct |
54 |
Correct |
1 ms |
312 KB |
Output is correct |
55 |
Incorrect |
684 ms |
36824 KB |
Wrong answer detected in grader |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Incorrect |
591 ms |
35032 KB |
Wrong answer detected in grader |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
17 |
Correct |
624 ms |
41864 KB |
Output is correct |
18 |
Correct |
678 ms |
41856 KB |
Output is correct |
19 |
Incorrect |
611 ms |
36344 KB |
Wrong answer detected in grader |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
258 ms |
20776 KB |
Output is correct |
10 |
Correct |
17 ms |
2500 KB |
Output is correct |
11 |
Correct |
113 ms |
11200 KB |
Output is correct |
12 |
Correct |
23 ms |
3572 KB |
Output is correct |
13 |
Correct |
48 ms |
4960 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
250 ms |
20772 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
715 ms |
41296 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
580 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
724 KB |
Output is correct |
28 |
Correct |
221 ms |
17032 KB |
Output is correct |
29 |
Correct |
363 ms |
25200 KB |
Output is correct |
30 |
Correct |
528 ms |
33528 KB |
Output is correct |
31 |
Correct |
696 ms |
41780 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
251 ms |
21304 KB |
Output is correct |
46 |
Correct |
466 ms |
30448 KB |
Output is correct |
47 |
Correct |
504 ms |
30560 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
0 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 |
312 KB |
Output is correct |
54 |
Correct |
1 ms |
312 KB |
Output is correct |
55 |
Incorrect |
684 ms |
36824 KB |
Wrong answer detected in grader |
56 |
Halted |
0 ms |
0 KB |
- |