#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:{0}){
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}]});
}
}
}
}
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(x[i] != 2 || x[i] != 6)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}]});
}
}
}
}
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}]});
}
}
}
}
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:75:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
75 | if(u.size() != n-1)
| ~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1283 ms |
82080 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
596 KB |
Output is correct |
28 |
Correct |
364 ms |
33072 KB |
Output is correct |
29 |
Correct |
605 ms |
49360 KB |
Output is correct |
30 |
Correct |
909 ms |
65672 KB |
Output is correct |
31 |
Correct |
1253 ms |
82060 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 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 |
596 KB |
Output is correct |
45 |
Correct |
516 ms |
45504 KB |
Output is correct |
46 |
Correct |
860 ms |
65556 KB |
Output is correct |
47 |
Correct |
785 ms |
65716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1283 ms |
82080 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
596 KB |
Output is correct |
28 |
Correct |
364 ms |
33072 KB |
Output is correct |
29 |
Correct |
605 ms |
49360 KB |
Output is correct |
30 |
Correct |
909 ms |
65672 KB |
Output is correct |
31 |
Correct |
1253 ms |
82060 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 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 |
596 KB |
Output is correct |
45 |
Correct |
516 ms |
45504 KB |
Output is correct |
46 |
Correct |
860 ms |
65556 KB |
Output is correct |
47 |
Correct |
785 ms |
65716 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Runtime error |
976 ms |
123772 KB |
Execution killed with signal 6 |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 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 |
1253 ms |
82940 KB |
Output is correct |
21 |
Correct |
1162 ms |
83032 KB |
Output is correct |
22 |
Correct |
1359 ms |
82760 KB |
Output is correct |
23 |
Correct |
997 ms |
88648 KB |
Output is correct |
24 |
Correct |
408 ms |
16740 KB |
Output is correct |
25 |
Correct |
872 ms |
21156 KB |
Output is correct |
26 |
Correct |
586 ms |
21344 KB |
Output is correct |
27 |
Correct |
1062 ms |
103808 KB |
Output is correct |
28 |
Correct |
1077 ms |
103944 KB |
Output is correct |
29 |
Correct |
1472 ms |
103844 KB |
Output is correct |
30 |
Correct |
1426 ms |
103872 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
59 ms |
6736 KB |
Output is correct |
33 |
Correct |
152 ms |
8520 KB |
Output is correct |
34 |
Correct |
1094 ms |
83020 KB |
Output is correct |
35 |
Correct |
19 ms |
1364 KB |
Output is correct |
36 |
Correct |
124 ms |
5432 KB |
Output is correct |
37 |
Correct |
340 ms |
10544 KB |
Output is correct |
38 |
Correct |
421 ms |
35328 KB |
Output is correct |
39 |
Correct |
650 ms |
47944 KB |
Output is correct |
40 |
Correct |
881 ms |
60992 KB |
Output is correct |
41 |
Correct |
1120 ms |
74100 KB |
Output is correct |
42 |
Correct |
1421 ms |
87212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
3 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
596 KB |
Output is correct |
56 |
Correct |
452 ms |
45368 KB |
Output is correct |
57 |
Correct |
757 ms |
65600 KB |
Output is correct |
58 |
Correct |
753 ms |
65812 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
1033 ms |
104124 KB |
Output is correct |
63 |
Correct |
987 ms |
104076 KB |
Output is correct |
64 |
Correct |
1031 ms |
103476 KB |
Output is correct |
65 |
Correct |
6 ms |
596 KB |
Output is correct |
66 |
Correct |
12 ms |
1024 KB |
Output is correct |
67 |
Correct |
501 ms |
44728 KB |
Output is correct |
68 |
Correct |
831 ms |
67016 KB |
Output is correct |
69 |
Correct |
1210 ms |
89364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 KB |
Output is correct |
17 |
Correct |
1141 ms |
104412 KB |
Output is correct |
18 |
Correct |
1089 ms |
104520 KB |
Output is correct |
19 |
Correct |
1298 ms |
82756 KB |
Output is correct |
20 |
Correct |
1224 ms |
87492 KB |
Output is correct |
21 |
Correct |
1018 ms |
84584 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
133 ms |
14348 KB |
Output is correct |
24 |
Correct |
49 ms |
2292 KB |
Output is correct |
25 |
Correct |
181 ms |
7716 KB |
Output is correct |
26 |
Correct |
427 ms |
12356 KB |
Output is correct |
27 |
Correct |
511 ms |
44672 KB |
Output is correct |
28 |
Correct |
805 ms |
55768 KB |
Output is correct |
29 |
Correct |
897 ms |
66980 KB |
Output is correct |
30 |
Correct |
1069 ms |
77912 KB |
Output is correct |
31 |
Correct |
1361 ms |
89148 KB |
Output is correct |
32 |
Correct |
1178 ms |
90848 KB |
Output is correct |
33 |
Correct |
1035 ms |
104032 KB |
Output is correct |
34 |
Correct |
7 ms |
724 KB |
Output is correct |
35 |
Correct |
15 ms |
1256 KB |
Output is correct |
36 |
Correct |
515 ms |
44512 KB |
Output is correct |
37 |
Correct |
880 ms |
66944 KB |
Output is correct |
38 |
Correct |
1179 ms |
89056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
436 ms |
52132 KB |
Output is correct |
10 |
Correct |
30 ms |
5612 KB |
Output is correct |
11 |
Correct |
238 ms |
28156 KB |
Output is correct |
12 |
Correct |
46 ms |
8140 KB |
Output is correct |
13 |
Correct |
56 ms |
5036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
500 ms |
52168 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1283 ms |
82080 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
596 KB |
Output is correct |
28 |
Correct |
364 ms |
33072 KB |
Output is correct |
29 |
Correct |
605 ms |
49360 KB |
Output is correct |
30 |
Correct |
909 ms |
65672 KB |
Output is correct |
31 |
Correct |
1253 ms |
82060 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 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 |
596 KB |
Output is correct |
45 |
Correct |
516 ms |
45504 KB |
Output is correct |
46 |
Correct |
860 ms |
65556 KB |
Output is correct |
47 |
Correct |
785 ms |
65716 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Runtime error |
976 ms |
123772 KB |
Execution killed with signal 6 |
56 |
Halted |
0 ms |
0 KB |
- |