#include<bits/stdc++.h>
#include "parks.h"
using namespace std;
using ll = long long;
ll n, m, q, k, x, y, a, b, c;
map< pair< pair<ll, ll>, pair<ll, ll> >, vector< pair< pair<ll, ll>, pair<ll, ll> > > > G;
set< pair< pair<ll, ll>, pair<ll, ll> > > prs;
struct dsu {
ll n;
vector<ll> par;
vector<ll> sz;
dsu(ll N) : n(N) {
par.resize(n);
sz.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
sz[i] = 1;
}
}
ll find_p(ll x) {
if (par[x] == x) {
return x;
}
ll p = find_p(par[x]);
par[x] = p;
return p;
}
void join(ll x, ll y) {
x = find_p(x);
y = find_p(y);
if (x == y) {
return;
}
if (sz[y] > sz[x]) {
swap(x, y);
}
par[y] = x;
sz[x] += sz[y];
}
};
void dfs(pair< pair<ll, ll>, pair<ll, ll> > x, map< pair<ll, ll>, pair<ll, ll> > &mp) {
// cout << x.first.first << ' ';
// cout << x.first.second << '\t';
// cout << x.second.first << ' ';
// cout << x.second.second << '\n';
mp[x.first] = x.second;
for (auto i : G[x]) {
if (!mp.count(i.first)) {
dfs(i, mp);
}
}
}
int construct_roads(vector<int> x, vector<int> y) {
n = x.size();
array< vector<int>, 4 > ans;
map< pair<ll, ll>, ll > pts;
for (int i = 0; i < n; i++) {
pts[{x[i], y[i]}] = i;
}
dsu d(n);
map< pair<ll, ll>, vector< pair<ll, ll> > > vec;
for (int i = 0; i < n; i++) {
if (pts.count({x[i], y[i] - 2}) && d.find_p(i) != d.find_p(pts[{x[i], y[i] - 2}])) {
d.join(i, pts[{x[i], y[i] - 2}]);
vec[{x[i] - 1, y[i] - 1}].push_back({x[i], y[i] - 1});
vec[{x[i] + 1, y[i] - 1}].push_back({x[i], y[i] - 1});
}
if (pts.count({x[i], y[i] + 2}) && d.find_p(i) != d.find_p(pts[{x[i], y[i] + 2}])) {
d.join(i, pts[{x[i], y[i] + 2}]);
vec[{x[i] - 1, y[i] + 1}].push_back({x[i], y[i] + 1});
vec[{x[i] + 1, y[i] + 1}].push_back({x[i], y[i] + 1});
}
}
for (int i = 0; i < n; i++) {
if (pts.count({x[i] - 2, y[i]}) && d.find_p(i) != d.find_p(pts[{x[i] - 2, y[i]}])) {
d.join(i, pts[{x[i] - 2, y[i]}]);
vec[{x[i] - 1, y[i] + 1}].push_back({x[i] - 1, y[i]});
vec[{x[i] - 1, y[i] - 1}].push_back({x[i] - 1, y[i]});
}
if (pts.count({x[i] + 2, y[i]}) && d.find_p(i) != d.find_p(pts[{x[i] + 2, y[i]}])) {
d.join(i, pts[{x[i] + 2, y[i]}]);
vec[{x[i] + 1, y[i] + 1}].push_back({x[i] + 1, y[i]});
vec[{x[i] + 1, y[i] - 1}].push_back({x[i] + 1, y[i]});
}
}
if (d.sz[d.find_p(0)] != n) {
return 0;
}
for (auto &i : vec) {
// cout << i.first.first << ' ' << i.first.second << '\n';
for (auto j : i.second) {
prs.insert({j, i.first});
// cout << '\t' << j.first << ' ' << j.second << '\n';
for (auto k : i.second) {
if (j != k) {
G[{j, i.first}].push_back({k, {k.first + (k.first - i.first.first), k.second + (k.second - i.first.second)}});
// cout << "\t\t" << k.first + (k.first - i.first.first) << ' ' << k.second + (k.second - i.first.second) << '\n';
}
}
}
}
map< pair<ll, ll>, pair<ll, ll> > mp;
for (auto i : prs) {
// cout << i.first.first.first << ' ' << i.first.first.second << '\n';
if (mp.count(i.first)) {
continue;
}
map< pair<ll, ll>, pair<ll, ll> > mp1;
dfs(i, mp1);
for (auto j : mp1) {
if (mp.count(j.first)) {
continue;
}
if (j.first.first % 2 == 0) {
ans[0].push_back(pts[{j.first.first, j.first.second - 1}]);
ans[1].push_back(pts[{j.first.first, j.first.second + 1}]);
}
else {
ans[0].push_back(pts[{j.first.first - 1, j.first.second}]);
ans[1].push_back(pts[{j.first.first + 1, j.first.second}]);
}
ans[2].push_back(j.second.first);
ans[3].push_back(j.second.second);
mp[j.first] = j.second;
}
}
build(ans[0], ans[1], ans[2], ans[3]);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1175 ms |
147608 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1284 KB |
Output is correct |
26 |
Correct |
5 ms |
1112 KB |
Output is correct |
27 |
Correct |
6 ms |
1372 KB |
Output is correct |
28 |
Correct |
383 ms |
59216 KB |
Output is correct |
29 |
Correct |
671 ms |
88756 KB |
Output is correct |
30 |
Correct |
973 ms |
118372 KB |
Output is correct |
31 |
Correct |
1371 ms |
147536 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 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 |
4 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
1112 KB |
Output is correct |
45 |
Correct |
526 ms |
78412 KB |
Output is correct |
46 |
Correct |
765 ms |
113572 KB |
Output is correct |
47 |
Correct |
768 ms |
113576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1175 ms |
147608 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1284 KB |
Output is correct |
26 |
Correct |
5 ms |
1112 KB |
Output is correct |
27 |
Correct |
6 ms |
1372 KB |
Output is correct |
28 |
Correct |
383 ms |
59216 KB |
Output is correct |
29 |
Correct |
671 ms |
88756 KB |
Output is correct |
30 |
Correct |
973 ms |
118372 KB |
Output is correct |
31 |
Correct |
1371 ms |
147536 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 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 |
4 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
1112 KB |
Output is correct |
45 |
Correct |
526 ms |
78412 KB |
Output is correct |
46 |
Correct |
765 ms |
113572 KB |
Output is correct |
47 |
Correct |
768 ms |
113576 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
344 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 |
1255 ms |
151208 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
1628 KB |
Output is correct |
58 |
Correct |
18 ms |
4868 KB |
Output is correct |
59 |
Correct |
17 ms |
2872 KB |
Output is correct |
60 |
Correct |
496 ms |
75816 KB |
Output is correct |
61 |
Correct |
799 ms |
102052 KB |
Output is correct |
62 |
Correct |
988 ms |
124972 KB |
Output is correct |
63 |
Correct |
1324 ms |
151284 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
946 ms |
138076 KB |
Output is correct |
68 |
Correct |
941 ms |
138172 KB |
Output is correct |
69 |
Correct |
963 ms |
137384 KB |
Output is correct |
70 |
Correct |
6 ms |
1372 KB |
Output is correct |
71 |
Correct |
13 ms |
2436 KB |
Output is correct |
72 |
Correct |
537 ms |
78600 KB |
Output is correct |
73 |
Correct |
846 ms |
117632 KB |
Output is correct |
74 |
Correct |
1218 ms |
156748 KB |
Output is correct |
75 |
Correct |
1076 ms |
150696 KB |
Output is correct |
76 |
Correct |
952 ms |
138148 KB |
Output is correct |
77 |
Correct |
9 ms |
1628 KB |
Output is correct |
78 |
Correct |
15 ms |
2860 KB |
Output is correct |
79 |
Correct |
511 ms |
77356 KB |
Output is correct |
80 |
Correct |
843 ms |
115880 KB |
Output is correct |
81 |
Correct |
1277 ms |
154360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1129 ms |
206948 KB |
Output is correct |
21 |
Execution timed out |
3538 ms |
139600 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 KB |
Output is correct |
17 |
Correct |
945 ms |
138148 KB |
Output is correct |
18 |
Correct |
905 ms |
138132 KB |
Output is correct |
19 |
Execution timed out |
3530 ms |
160320 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
405 ms |
69164 KB |
Output is correct |
10 |
Correct |
19 ms |
7516 KB |
Output is correct |
11 |
Correct |
137 ms |
37320 KB |
Output is correct |
12 |
Correct |
30 ms |
10844 KB |
Output is correct |
13 |
Correct |
59 ms |
14160 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
373 ms |
69116 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1175 ms |
147608 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1284 KB |
Output is correct |
26 |
Correct |
5 ms |
1112 KB |
Output is correct |
27 |
Correct |
6 ms |
1372 KB |
Output is correct |
28 |
Correct |
383 ms |
59216 KB |
Output is correct |
29 |
Correct |
671 ms |
88756 KB |
Output is correct |
30 |
Correct |
973 ms |
118372 KB |
Output is correct |
31 |
Correct |
1371 ms |
147536 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 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 |
4 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
1112 KB |
Output is correct |
45 |
Correct |
526 ms |
78412 KB |
Output is correct |
46 |
Correct |
765 ms |
113572 KB |
Output is correct |
47 |
Correct |
768 ms |
113576 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
344 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 |
1255 ms |
151208 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
1628 KB |
Output is correct |
58 |
Correct |
18 ms |
4868 KB |
Output is correct |
59 |
Correct |
17 ms |
2872 KB |
Output is correct |
60 |
Correct |
496 ms |
75816 KB |
Output is correct |
61 |
Correct |
799 ms |
102052 KB |
Output is correct |
62 |
Correct |
988 ms |
124972 KB |
Output is correct |
63 |
Correct |
1324 ms |
151284 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
946 ms |
138076 KB |
Output is correct |
68 |
Correct |
941 ms |
138172 KB |
Output is correct |
69 |
Correct |
963 ms |
137384 KB |
Output is correct |
70 |
Correct |
6 ms |
1372 KB |
Output is correct |
71 |
Correct |
13 ms |
2436 KB |
Output is correct |
72 |
Correct |
537 ms |
78600 KB |
Output is correct |
73 |
Correct |
846 ms |
117632 KB |
Output is correct |
74 |
Correct |
1218 ms |
156748 KB |
Output is correct |
75 |
Correct |
1076 ms |
150696 KB |
Output is correct |
76 |
Correct |
952 ms |
138148 KB |
Output is correct |
77 |
Correct |
9 ms |
1628 KB |
Output is correct |
78 |
Correct |
15 ms |
2860 KB |
Output is correct |
79 |
Correct |
511 ms |
77356 KB |
Output is correct |
80 |
Correct |
843 ms |
115880 KB |
Output is correct |
81 |
Correct |
1277 ms |
154360 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
1129 ms |
206948 KB |
Output is correct |
86 |
Execution timed out |
3538 ms |
139600 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |