#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() , [](pair<int, int> a , pair<int ,int> b){return a.first + a.second < b.first + b.second;});
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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 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 |
280 ms |
38472 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
856 KB |
Output is correct |
28 |
Correct |
114 ms |
15688 KB |
Output is correct |
29 |
Correct |
157 ms |
23168 KB |
Output is correct |
30 |
Correct |
204 ms |
31032 KB |
Output is correct |
31 |
Correct |
311 ms |
38536 KB |
Output is correct |
32 |
Correct |
1 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 |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
127 ms |
19396 KB |
Output is correct |
46 |
Correct |
173 ms |
28592 KB |
Output is correct |
47 |
Correct |
168 ms |
28644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 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 |
280 ms |
38472 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
856 KB |
Output is correct |
28 |
Correct |
114 ms |
15688 KB |
Output is correct |
29 |
Correct |
157 ms |
23168 KB |
Output is correct |
30 |
Correct |
204 ms |
31032 KB |
Output is correct |
31 |
Correct |
311 ms |
38536 KB |
Output is correct |
32 |
Correct |
1 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 |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
127 ms |
19396 KB |
Output is correct |
46 |
Correct |
173 ms |
28592 KB |
Output is correct |
47 |
Correct |
168 ms |
28644 KB |
Output is correct |
48 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
55 |
Correct |
277 ms |
38376 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
600 KB |
Output is correct |
58 |
Correct |
7 ms |
1628 KB |
Output is correct |
59 |
Correct |
10 ms |
1932 KB |
Output is correct |
60 |
Correct |
139 ms |
19220 KB |
Output is correct |
61 |
Correct |
173 ms |
27012 KB |
Output is correct |
62 |
Correct |
223 ms |
31932 KB |
Output is correct |
63 |
Correct |
299 ms |
38664 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
249 ms |
38520 KB |
Output is correct |
68 |
Correct |
243 ms |
38528 KB |
Output is correct |
69 |
Correct |
237 ms |
38420 KB |
Output is correct |
70 |
Correct |
3 ms |
860 KB |
Output is correct |
71 |
Correct |
7 ms |
1628 KB |
Output is correct |
72 |
Incorrect |
106 ms |
19112 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
73 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 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 |
Incorrect |
327 ms |
35600 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 KB |
Output is correct |
17 |
Correct |
242 ms |
38784 KB |
Output is correct |
18 |
Correct |
258 ms |
39048 KB |
Output is correct |
19 |
Incorrect |
291 ms |
37248 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
105 ms |
19256 KB |
Output is correct |
10 |
Correct |
8 ms |
2396 KB |
Output is correct |
11 |
Correct |
63 ms |
10492 KB |
Output is correct |
12 |
Correct |
14 ms |
3396 KB |
Output is correct |
13 |
Correct |
36 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
121 ms |
19216 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 |
280 ms |
38472 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
856 KB |
Output is correct |
28 |
Correct |
114 ms |
15688 KB |
Output is correct |
29 |
Correct |
157 ms |
23168 KB |
Output is correct |
30 |
Correct |
204 ms |
31032 KB |
Output is correct |
31 |
Correct |
311 ms |
38536 KB |
Output is correct |
32 |
Correct |
1 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 |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
127 ms |
19396 KB |
Output is correct |
46 |
Correct |
173 ms |
28592 KB |
Output is correct |
47 |
Correct |
168 ms |
28644 KB |
Output is correct |
48 |
Correct |
0 ms |
600 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 |
348 KB |
Output is correct |
55 |
Correct |
277 ms |
38376 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
600 KB |
Output is correct |
58 |
Correct |
7 ms |
1628 KB |
Output is correct |
59 |
Correct |
10 ms |
1932 KB |
Output is correct |
60 |
Correct |
139 ms |
19220 KB |
Output is correct |
61 |
Correct |
173 ms |
27012 KB |
Output is correct |
62 |
Correct |
223 ms |
31932 KB |
Output is correct |
63 |
Correct |
299 ms |
38664 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
249 ms |
38520 KB |
Output is correct |
68 |
Correct |
243 ms |
38528 KB |
Output is correct |
69 |
Correct |
237 ms |
38420 KB |
Output is correct |
70 |
Correct |
3 ms |
860 KB |
Output is correct |
71 |
Correct |
7 ms |
1628 KB |
Output is correct |
72 |
Incorrect |
106 ms |
19112 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
73 |
Halted |
0 ms |
0 KB |
- |