#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "parks.h"
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
//
//static void check(bool cond, string message) {
// if (!cond) {
// printf("%s\n", message.c_str());
// fclose(stdout);
// exit(0);
// }
//}
//
//static int n;
//static bool build_called;
//static int m;
//static vector<int> _u, _v, _a, _b;
//
//void build(vector<int> u, vector<int> v, vector<int> a, vector<int> b) {
// check(!build_called, "build is called more than once");
// build_called = true;
// m = u.size();
// check(int(v.size()) == m, "u.size() != v.size()");
// check(int(a.size()) == m, "u.size() != a.size()");
// check(int(b.size()) == m, "u.size() != b.size()");
// _u = u;
// _v = v;
// _a = a;
// _b = b;
//}
const int N = 2e5 + 10;
int par[N], sz[N];
int Find(int u) {
return (u == par[u] ? u : par[u] = Find(par[u]));
}
int Union(int u, int v) {
if ((u = Find(u)) == (v = Find(v))) return 0;
if (sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
return 1;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n = x.size();
map <pair <int, int>, int> id;
for (int i = 0; i < n; ++i) id[{x[i], y[i]}] = i, par[i] = i, sz[i] = 1;
vector <tuple <int, int, int, int> > ans;
int cnt = 0;
auto addX = [&](int x, int y, int i, int j) {
if (Find(i) == Find(j)) return;
if (x + y & 2) ans.push_back({i, j, x - 1, y - 1});
else ans.push_back({i, j, x + 1, y - 1});
cnt += Union(i, j);
};
auto addY = [&](int x, int y, int i, int j) {
if (Find(i) == Find(j)) return;
if (x + y & 2) ans.push_back({i, j, x - 1, y + 1});
else ans.push_back({i, j, x - 1, y - 1});
cnt += Union(i, j);
};
for (int i = 0; i < n; ++i) {
if (id.find({x[i], y[i] - 2}) != id.end()) addX(x[i], y[i], i, id[{x[i], y[i] - 2}]);
if (id.find({x[i] - 2, y[i]}) != id.end()) addY(x[i], y[i], i, id[{x[i] - 2, y[i]}]);
}
assert(cnt <= n - 1);
if (cnt != n - 1) return 0;
vector <int> u, v, a, b;
for (auto [_u, _v, _a, _b]: ans)
u.push_back(_u), v.push_back(_v), a.push_back(_a), b.push_back(_b);
build(u, v, a, b);
return 1;
}
//int main() {
// assert(scanf("%d", &n) == 1);
// vector<int> x(n), y(n);
// for (int i = 0; i < n; i++) {
// assert(scanf("%d%d", &x[i], &y[i]) == 2);
// }
// fclose(stdin);
//
// build_called = false;
// const int possible = construct_roads(x, y);
//
// check(possible == 0 || possible == 1, "Invalid return value of construct_roads()");
// if (possible == 1) {
// check(build_called, "construct_roads() returned 1 without calling build()");
// } else {
// check(!build_called, "construct_roads() called build() but returned 0");
// }
//
// printf("%d\n", possible);
// if (possible == 1) {
// printf("%d\n", m);
// for (int j = 0; j < m; j++) {
// printf("%d %d %d %d\n", _u[j], _v[j], _a[j], _b[j]);
// }
// }
// fclose(stdout);
// return 0;
//}
Compilation message
parks.cpp: In lambda function:
parks.cpp:64:15: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
64 | if (x + y & 2) ans.push_back({i, j, x - 1, y - 1});
| ~~^~~
parks.cpp: In lambda function:
parks.cpp:70:15: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
70 | if (x + y & 2) ans.push_back({i, j, x - 1, y + 1});
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
474 ms |
33704 KB |
Output is correct |
21 |
Correct |
403 ms |
33328 KB |
Output is correct |
22 |
Correct |
434 ms |
33276 KB |
Output is correct |
23 |
Correct |
316 ms |
28448 KB |
Output is correct |
24 |
Correct |
296 ms |
19228 KB |
Output is correct |
25 |
Correct |
398 ms |
23472 KB |
Output is correct |
26 |
Correct |
332 ms |
23360 KB |
Output is correct |
27 |
Correct |
390 ms |
32456 KB |
Output is correct |
28 |
Correct |
418 ms |
32312 KB |
Output is correct |
29 |
Correct |
503 ms |
32536 KB |
Output is correct |
30 |
Correct |
451 ms |
32316 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
18 ms |
2668 KB |
Output is correct |
33 |
Correct |
91 ms |
10060 KB |
Output is correct |
34 |
Correct |
417 ms |
33724 KB |
Output is correct |
35 |
Correct |
9 ms |
1620 KB |
Output is correct |
36 |
Correct |
55 ms |
6336 KB |
Output is correct |
37 |
Correct |
137 ms |
12236 KB |
Output is correct |
38 |
Correct |
127 ms |
14040 KB |
Output is correct |
39 |
Correct |
211 ms |
18608 KB |
Output is correct |
40 |
Correct |
314 ms |
24560 KB |
Output is correct |
41 |
Correct |
389 ms |
29240 KB |
Output is correct |
42 |
Correct |
478 ms |
33756 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 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 |
304 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 |
212 KB |
Output is correct |
54 |
Correct |
2 ms |
468 KB |
Output is correct |
55 |
Correct |
3 ms |
596 KB |
Output is correct |
56 |
Correct |
169 ms |
16336 KB |
Output is correct |
57 |
Correct |
252 ms |
24380 KB |
Output is correct |
58 |
Correct |
289 ms |
24412 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
312 KB |
Output is correct |
62 |
Correct |
389 ms |
32416 KB |
Output is correct |
63 |
Correct |
397 ms |
32544 KB |
Output is correct |
64 |
Correct |
368 ms |
32180 KB |
Output is correct |
65 |
Correct |
4 ms |
724 KB |
Output is correct |
66 |
Correct |
7 ms |
1236 KB |
Output is correct |
67 |
Correct |
160 ms |
16276 KB |
Output is correct |
68 |
Correct |
309 ms |
25200 KB |
Output is correct |
69 |
Correct |
454 ms |
32472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
17 |
Correct |
429 ms |
32832 KB |
Output is correct |
18 |
Correct |
382 ms |
33016 KB |
Output is correct |
19 |
Correct |
432 ms |
33252 KB |
Output is correct |
20 |
Correct |
482 ms |
31744 KB |
Output is correct |
21 |
Correct |
375 ms |
28772 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
43 ms |
5508 KB |
Output is correct |
24 |
Correct |
20 ms |
2872 KB |
Output is correct |
25 |
Correct |
89 ms |
9364 KB |
Output is correct |
26 |
Correct |
214 ms |
14240 KB |
Output is correct |
27 |
Correct |
176 ms |
16996 KB |
Output is correct |
28 |
Correct |
275 ms |
20964 KB |
Output is correct |
29 |
Correct |
312 ms |
26496 KB |
Output is correct |
30 |
Correct |
377 ms |
29636 KB |
Output is correct |
31 |
Correct |
443 ms |
33808 KB |
Output is correct |
32 |
Correct |
437 ms |
32376 KB |
Output is correct |
33 |
Correct |
371 ms |
32448 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
1492 KB |
Output is correct |
36 |
Correct |
152 ms |
16316 KB |
Output is correct |
37 |
Correct |
327 ms |
25120 KB |
Output is correct |
38 |
Correct |
432 ms |
32580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
144 ms |
15560 KB |
Output is correct |
10 |
Correct |
9 ms |
1960 KB |
Output is correct |
11 |
Correct |
51 ms |
8896 KB |
Output is correct |
12 |
Correct |
18 ms |
2640 KB |
Output is correct |
13 |
Correct |
26 ms |
5580 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
146 ms |
16316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Incorrect |
1 ms |
212 KB |
Tree @(3, 5) appears more than once: for edges on positions 1 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |