#include <bits/stdc++.h>
#include "parks.h"
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct dsu {
vector<int> p, size;
dsu(int n) : p(n), size(n, 1) {
iota(all(p), 0);
}
int get(int v) {
return v == p[v] ? v : p[v] = get(p[v]);
}
bool merge(int a, int b) {
a = get(a), b = get(b);
if (a == b) return 0;
if (size[a] < size[b]) swap(a, b);
size[a] += size[b], p[b] = a;
return 1;
}
};
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n = sz(x);
map<ar<int, 4>, int> mp;
map<ar<int, 2>, int> idx;
for (int i = 0; i < n; i++) idx[{x[i], y[i]}] = i;
vector<int> a(n-1), b(n-1), c(n-1), d(n-1);
dsu D(n);
int now = 0;
map<ar<int, 2>, int> adj;
for (int i = 0; i < n; i++) {
if (idx.count({x[i] + 2, y[i]})) {
mp[{x[i], y[i], x[i] + 2, y[i]}] += 2;
adj[{x[i] + 1, y[i] + 1}]++;
adj[{x[i] + 1, y[i] - 1}]++;
}
if (idx.count({x[i], y[i] + 2})) {
mp[{x[i], y[i], x[i], y[i] + 2}] += 2;
adj[{x[i] + 1, y[i] + 1}]++;
adj[{x[i] - 1, y[i] + 1}]++;
}
}
set<pair<int, ar<int, 2>>> q;
for (auto [x, y] : adj) q.insert({y, x});
set<ar<int, 2>> used;
while (q.size()) {
auto [val, p] = *q.begin();
q.erase(q.begin());
if (val != adj[p]) {
if (adj[p]) {
q.insert({adj[p], p});
continue;
}
}
ar<int, 4> seg2{p[0] - 1, p[1] - 1, p[0] - 1, p[1] + 1};
ar<int, 4> seg3{p[0] + 1, p[1] - 1, p[0] + 1, p[1] + 1};
ar<int, 4> seg4{p[0] - 1, p[1] - 1, p[0] + 1, p[1] - 1};
ar<int, 4> seg5{p[0] - 1, p[1] + 1, p[0] + 1, p[1] + 1};
bool F = 0;
for (int tp = 0; tp <= 4; tp++) {
for (auto& seg : {seg2, seg3, seg4, seg5}) if (mp.count(seg) && mp[seg] == tp) {
int i1 = idx[{seg[0], seg[1]}], i2 = idx[{seg[2], seg[3]}];
if (D.get(i1) == D.get(i2)) continue;
ar<int, 2> pt, pt2;
if (seg[0] != seg[2]) {
pt = {seg[0] + 1, seg[1] - 1};
pt2 = {seg[0] + 1, seg[1] + 1};
}
else {
pt = {seg[0] - 1, seg[1] + 1};
pt2 = {seg[0] + 1, seg[1] + 1};
}
for (auto p : {pt, pt2}) {
if (q.count({adj[p], p})) q.erase({adj[p], p});
adj[p]--;
if (adj[p] > 0) q.insert({adj[p], p});
}
F = 1;
D.merge(i1, i2);
a[now] = i1, b[now] = i2;
c[now] = p[0], d[now] = p[1];
now++;
break;
}
if (F) break;
}
if (F) for (auto& seg : {seg2, seg3, seg4, seg5}) if (mp.count(seg)) mp[seg]--;
}
if (D.size[D.get(0)] != n) return 0;
build(a, b, c, d);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 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 |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1794 ms |
78804 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
696 KB |
Output is correct |
26 |
Correct |
13 ms |
1368 KB |
Output is correct |
27 |
Correct |
17 ms |
1880 KB |
Output is correct |
28 |
Correct |
553 ms |
31688 KB |
Output is correct |
29 |
Correct |
970 ms |
47508 KB |
Output is correct |
30 |
Correct |
1296 ms |
63060 KB |
Output is correct |
31 |
Correct |
1703 ms |
78796 KB |
Output is correct |
32 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
388 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 |
6 ms |
1116 KB |
Output is correct |
44 |
Correct |
9 ms |
1372 KB |
Output is correct |
45 |
Correct |
544 ms |
34740 KB |
Output is correct |
46 |
Correct |
827 ms |
50340 KB |
Output is correct |
47 |
Correct |
815 ms |
50004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 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 |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1794 ms |
78804 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
696 KB |
Output is correct |
26 |
Correct |
13 ms |
1368 KB |
Output is correct |
27 |
Correct |
17 ms |
1880 KB |
Output is correct |
28 |
Correct |
553 ms |
31688 KB |
Output is correct |
29 |
Correct |
970 ms |
47508 KB |
Output is correct |
30 |
Correct |
1296 ms |
63060 KB |
Output is correct |
31 |
Correct |
1703 ms |
78796 KB |
Output is correct |
32 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
388 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 |
6 ms |
1116 KB |
Output is correct |
44 |
Correct |
9 ms |
1372 KB |
Output is correct |
45 |
Correct |
544 ms |
34740 KB |
Output is correct |
46 |
Correct |
827 ms |
50340 KB |
Output is correct |
47 |
Correct |
815 ms |
50004 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
436 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Incorrect |
0 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 4 and 5 |
54 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 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 |
432 KB |
Output is correct |
20 |
Correct |
930 ms |
60648 KB |
Output is correct |
21 |
Correct |
1048 ms |
60600 KB |
Output is correct |
22 |
Correct |
1018 ms |
60580 KB |
Output is correct |
23 |
Correct |
871 ms |
72628 KB |
Output is correct |
24 |
Correct |
185 ms |
22352 KB |
Output is correct |
25 |
Correct |
1110 ms |
84936 KB |
Output is correct |
26 |
Correct |
1028 ms |
84820 KB |
Output is correct |
27 |
Correct |
1082 ms |
84816 KB |
Output is correct |
28 |
Correct |
1047 ms |
83916 KB |
Output is correct |
29 |
Correct |
1179 ms |
84924 KB |
Output is correct |
30 |
Correct |
1161 ms |
84844 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Incorrect |
53 ms |
5452 KB |
Tree @(185697, 20395) appears more than once: for edges on positions 13092 and 13093 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 KB |
Output is correct |
17 |
Correct |
1083 ms |
85316 KB |
Output is correct |
18 |
Correct |
1117 ms |
85564 KB |
Output is correct |
19 |
Incorrect |
1140 ms |
60672 KB |
Tree @(3, 50001) appears more than once: for edges on positions 0 and 1 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
523 ms |
42532 KB |
Output is correct |
10 |
Correct |
31 ms |
4440 KB |
Output is correct |
11 |
Correct |
258 ms |
22976 KB |
Output is correct |
12 |
Correct |
55 ms |
6732 KB |
Output is correct |
13 |
Correct |
213 ms |
19004 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
466 ms |
42576 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 |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1794 ms |
78804 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
696 KB |
Output is correct |
26 |
Correct |
13 ms |
1368 KB |
Output is correct |
27 |
Correct |
17 ms |
1880 KB |
Output is correct |
28 |
Correct |
553 ms |
31688 KB |
Output is correct |
29 |
Correct |
970 ms |
47508 KB |
Output is correct |
30 |
Correct |
1296 ms |
63060 KB |
Output is correct |
31 |
Correct |
1703 ms |
78796 KB |
Output is correct |
32 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
388 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 |
6 ms |
1116 KB |
Output is correct |
44 |
Correct |
9 ms |
1372 KB |
Output is correct |
45 |
Correct |
544 ms |
34740 KB |
Output is correct |
46 |
Correct |
827 ms |
50340 KB |
Output is correct |
47 |
Correct |
815 ms |
50004 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
436 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Incorrect |
0 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 4 and 5 |
54 |
Halted |
0 ms |
0 KB |
- |