#include "parks.h"
#include<bits/stdc++.h>
using namespace std;
struct DSU
{
vector<int> e;
DSU(int n)
{
e.assign(n , -1);
}
int get(int u )
{
return (e[u] < 0 ? u : e[u] = get(e[u]));
}
bool same(int u , int v)
{
return get(u) == get(v);
}
bool unite(int u , int v)
{
u = get(u ) , v =get(v);
if(u == v)
return 0;
if(e[u] > e[v])
swap(u , v);
e[u]+=e[v];
e[v] = u;
return 1;
}
};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n= (int)x.size();
vector<int> u , v , a , b;
map<pair<int, int> , int> idx;
set<pair<int ,int>> taken;
for(int i = 0 ; i <n ; i++)
{
idx[{x[i] , y[i]}] = i;
}
vector<pair<int ,int>> vv;
for(int i = 0 ; i < n ; i++)
vv.push_back({x[i] , y[i]});
sort(vv.begin() , vv.end());
u.clear();
v.clear();
a.clear();
b.clear();
taken.clear();
for(int i = 0 ; i < n ;i++)
{
x[i] = vv[i].first , y[i] = vv[i].second;
}
DSU dsu(n);
for(int i = 0 ; i < n ;i++)
{
if(idx.count({x[i] + 2 , y[i]}) && dsu.unite(idx[{x[i] , y[i]}] , idx[{x[i] + 2 , y[i]}]))
{
if(!taken.count({x[i] + 1 , y[i] - 1}))
{
u.push_back(idx[{x[i] , y[i]}]);
v.push_back(idx[{x[i] + 2 , y[i]}]);
a.push_back(x[i] + 1);
b.push_back(y[i] - 1);
taken.insert({x[i] + 1 , y[i] - 1});
}
else if(!taken.count({x[i] + 1 , y[i] + 1}))
{
u.push_back(idx[{x[i] , y[i]}]);
v.push_back(idx[{x[i] + 2 , y[i]}]);
a.push_back(x[i] + 1);
b.push_back(y[i] + 1);
taken.insert({x[i] + 1 , y[i] + 1});
}
}
if(idx.count({x[i] , y[i] + 2}) && dsu.unite(idx[{x[i] , y[i]}] , idx[{x[i] , y[i] + 2}]))
{
if(!taken.count({x[i] - 1 , y[i] + 1}))
{
u.push_back(idx[{x[i] , y[i]}]);
v.push_back(idx[{x[i] , y[i] + 2}]);
b.push_back(y[i] + 1);
a.push_back(x[i] - 1);
taken.insert({x[i] - 1 , y[i] +1});
}
else if(!taken.count({x[i] + 1 , y[i] + 1}))
{
u.push_back(idx[{x[i] , y[i]}]);
v.push_back(idx[{x[i] , y[i] + 2}]);
b.push_back(y[i] + 1);
a.push_back(x[i] + 1);
taken.insert({x[i] + 1 , y[i] +1});
}
}
}
set<int> s;
for(int i = 0 ;i < n ;i++)
{
s.insert(dsu.get(i));
}
if((int)s.size() == 1)
{
build(u , v , a , b);
return 1;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
281 ms |
38476 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
576 KB |
Output is correct |
26 |
Correct |
2 ms |
816 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
89 ms |
15552 KB |
Output is correct |
29 |
Correct |
134 ms |
23100 KB |
Output is correct |
30 |
Correct |
196 ms |
31128 KB |
Output is correct |
31 |
Correct |
242 ms |
38536 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
102 ms |
19224 KB |
Output is correct |
46 |
Correct |
164 ms |
28648 KB |
Output is correct |
47 |
Correct |
157 ms |
28660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
281 ms |
38476 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
576 KB |
Output is correct |
26 |
Correct |
2 ms |
816 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
89 ms |
15552 KB |
Output is correct |
29 |
Correct |
134 ms |
23100 KB |
Output is correct |
30 |
Correct |
196 ms |
31128 KB |
Output is correct |
31 |
Correct |
242 ms |
38536 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
102 ms |
19224 KB |
Output is correct |
46 |
Correct |
164 ms |
28648 KB |
Output is correct |
47 |
Correct |
157 ms |
28660 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
344 KB |
Output is correct |
55 |
Correct |
251 ms |
38528 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
1628 KB |
Output is correct |
59 |
Correct |
8 ms |
1880 KB |
Output is correct |
60 |
Correct |
112 ms |
19312 KB |
Output is correct |
61 |
Correct |
157 ms |
27156 KB |
Output is correct |
62 |
Correct |
227 ms |
32156 KB |
Output is correct |
63 |
Correct |
250 ms |
38664 KB |
Output is correct |
64 |
Correct |
0 ms |
344 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
250 ms |
38536 KB |
Output is correct |
68 |
Correct |
230 ms |
38480 KB |
Output is correct |
69 |
Correct |
233 ms |
38280 KB |
Output is correct |
70 |
Correct |
4 ms |
860 KB |
Output is correct |
71 |
Correct |
6 ms |
1628 KB |
Output is correct |
72 |
Correct |
103 ms |
19308 KB |
Output is correct |
73 |
Correct |
177 ms |
29324 KB |
Output is correct |
74 |
Correct |
272 ms |
38636 KB |
Output is correct |
75 |
Correct |
246 ms |
38532 KB |
Output is correct |
76 |
Correct |
235 ms |
38536 KB |
Output is correct |
77 |
Correct |
4 ms |
1116 KB |
Output is correct |
78 |
Correct |
7 ms |
1628 KB |
Output is correct |
79 |
Correct |
104 ms |
19276 KB |
Output is correct |
80 |
Correct |
166 ms |
29468 KB |
Output is correct |
81 |
Correct |
242 ms |
38540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
245 ms |
39348 KB |
Output is correct |
21 |
Correct |
245 ms |
39300 KB |
Output is correct |
22 |
Correct |
252 ms |
39300 KB |
Output is correct |
23 |
Correct |
231 ms |
33184 KB |
Output is correct |
24 |
Correct |
147 ms |
27592 KB |
Output is correct |
25 |
Correct |
210 ms |
31152 KB |
Output is correct |
26 |
Correct |
204 ms |
31176 KB |
Output is correct |
27 |
Correct |
256 ms |
38556 KB |
Output is correct |
28 |
Correct |
244 ms |
38536 KB |
Output is correct |
29 |
Correct |
262 ms |
38528 KB |
Output is correct |
30 |
Correct |
328 ms |
38372 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Incorrect |
13 ms |
3164 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
17 |
Correct |
252 ms |
38928 KB |
Output is correct |
18 |
Correct |
277 ms |
39044 KB |
Output is correct |
19 |
Correct |
295 ms |
39184 KB |
Output is correct |
20 |
Correct |
267 ms |
37548 KB |
Output is correct |
21 |
Correct |
212 ms |
33788 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Incorrect |
37 ms |
6456 KB |
Given structure is not connected: There is no path between vertices 0 and 2 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19192 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
59 ms |
10368 KB |
Output is correct |
12 |
Correct |
12 ms |
3420 KB |
Output is correct |
13 |
Correct |
35 ms |
7048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
111 ms |
19364 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
281 ms |
38476 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
576 KB |
Output is correct |
26 |
Correct |
2 ms |
816 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
89 ms |
15552 KB |
Output is correct |
29 |
Correct |
134 ms |
23100 KB |
Output is correct |
30 |
Correct |
196 ms |
31128 KB |
Output is correct |
31 |
Correct |
242 ms |
38536 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
102 ms |
19224 KB |
Output is correct |
46 |
Correct |
164 ms |
28648 KB |
Output is correct |
47 |
Correct |
157 ms |
28660 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
344 KB |
Output is correct |
55 |
Correct |
251 ms |
38528 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
5 ms |
1628 KB |
Output is correct |
59 |
Correct |
8 ms |
1880 KB |
Output is correct |
60 |
Correct |
112 ms |
19312 KB |
Output is correct |
61 |
Correct |
157 ms |
27156 KB |
Output is correct |
62 |
Correct |
227 ms |
32156 KB |
Output is correct |
63 |
Correct |
250 ms |
38664 KB |
Output is correct |
64 |
Correct |
0 ms |
344 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
250 ms |
38536 KB |
Output is correct |
68 |
Correct |
230 ms |
38480 KB |
Output is correct |
69 |
Correct |
233 ms |
38280 KB |
Output is correct |
70 |
Correct |
4 ms |
860 KB |
Output is correct |
71 |
Correct |
6 ms |
1628 KB |
Output is correct |
72 |
Correct |
103 ms |
19308 KB |
Output is correct |
73 |
Correct |
177 ms |
29324 KB |
Output is correct |
74 |
Correct |
272 ms |
38636 KB |
Output is correct |
75 |
Correct |
246 ms |
38532 KB |
Output is correct |
76 |
Correct |
235 ms |
38536 KB |
Output is correct |
77 |
Correct |
4 ms |
1116 KB |
Output is correct |
78 |
Correct |
7 ms |
1628 KB |
Output is correct |
79 |
Correct |
104 ms |
19276 KB |
Output is correct |
80 |
Correct |
166 ms |
29468 KB |
Output is correct |
81 |
Correct |
242 ms |
38540 KB |
Output is correct |
82 |
Correct |
1 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
344 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
245 ms |
39348 KB |
Output is correct |
86 |
Correct |
245 ms |
39300 KB |
Output is correct |
87 |
Correct |
252 ms |
39300 KB |
Output is correct |
88 |
Correct |
231 ms |
33184 KB |
Output is correct |
89 |
Correct |
147 ms |
27592 KB |
Output is correct |
90 |
Correct |
210 ms |
31152 KB |
Output is correct |
91 |
Correct |
204 ms |
31176 KB |
Output is correct |
92 |
Correct |
256 ms |
38556 KB |
Output is correct |
93 |
Correct |
244 ms |
38536 KB |
Output is correct |
94 |
Correct |
262 ms |
38528 KB |
Output is correct |
95 |
Correct |
328 ms |
38372 KB |
Output is correct |
96 |
Correct |
0 ms |
344 KB |
Output is correct |
97 |
Incorrect |
13 ms |
3164 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
98 |
Halted |
0 ms |
0 KB |
- |