#include <bits/stdc++.h>
//#include "grader.cpp"
#include "parks.h"
#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#define el "\n"
#define all(x) (x).begin(), (x).end()
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
const int N = 2e6 + 500;
const int steps[4][2] = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};
int pr[N];
int cmp[2 * N], to[2 * N];
vector <int> g[2 * N], gr[2 * N], ord;
bool mk[2 * N];
int f(int v) {return (pr[v] == v ? v : pr[v] = f(pr[v]));}
void link(int v, int u) {
v = f(v); u = f(u);
if (v == u) {
return;
}
pr[u] = v;
}
int nt(int x) {return (x < N ? x + N : x - N);}
void add_impl(int a, int b) {
int na = nt(a), nb = nt(b);
g[na].pb(b); gr[b].pb(na);
g[nb].pb(a); gr[a].pb(nb);
}
void dfs(int v) {
mk[v] = 1;
for (auto u : g[v]) {
if (mk[u]) {
continue;
}
dfs(u);
}
ord.pb(v);
}
int construct_roads(vector <int> x, vector <int> y) {
int n = sz(x);
map <pair <int, int>, int> mp, used;
vector <pair <int, int> > r;
bool sd = 1;
for (int i = 0; i < n; i++) {
mp[{x[i], y[i]}] = i;
pr[i] = i;
sd &= (x[i] >= 2 && x[i] <= 4);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
int cx = x[i] + steps[j][0];
int cy = y[i] + steps[j][1];
if (mp.count({cx, cy}) && !used[{i, mp[{cx, cy}]}] && (sd || f(i) != f(mp[{cx, cy}]))) {
int to = mp[{cx, cy}];
link(i, to);
r.pb({i, to});
used[{to, i}] = 1;
used[{i, to}] = 1;
}
}
}
sort(all(r));
r.resize(unique(all(r)) - r.begin());
for (int i = 0; i < n; i++) {
if (f(i) != f(0)) {
return 0;
}
}
vector <int> u, v, A, B;
map <pair <int, int>, vector <int> > e;
for (int i = 0; i < sz(r); i++) {
int a = r[i].F, b = r[i].S;
u.pb(a); v.pb(b);
if (abs(x[a] - x[b]) == 0) {
int lx = x[a];
int ly = min(y[a], y[b]);
e[{lx - 1, ly + 1}].pb(i);
e[{lx + 1, ly + 1}].pb(nt(i));
} else {
int lx = min(x[a], x[b]);
int ly = y[a];
e[{lx + 1, ly + 1}].pb(i);
e[{lx + 1, ly - 1}].pb(nt(i));
}
}
for (auto [p, v] : e) {
int x = p.F, y = p.S;
if (sz(v) == 1) {
continue;
}
assert(sz(v) == 2);
int a = v[0], b = v[1];
add_impl(a, b);
}
for (int i = 0; i < 2 * N; i++) {
cmp[i] = -1;
if (mk[i]) {
continue;
}
dfs(i);
}
reverse(all(ord));
int C = 0;
for (auto v : ord) {
function <void(int)> dfsr = [&](int v) {
if (cmp[v] != -1) {
return;
}
cmp[v] = C;
for (auto u : gr[v]) {
dfsr(u);
}
};
dfsr(v);
C++;
}
for (int i = 0; i < N; i++) {
if (cmp[i] == cmp[nt(i)]) {
return 0;
}
to[i] = (cmp[i] > cmp[nt(i)]);
}
for (int i = 0; i < sz(r); i++) {
int a = r[i].F, b = r[i].S;
if (abs(x[a] - x[b]) == 0) {
int lx = x[a];
int ly = min(y[a], y[b]);
if (to[i]) {
A.pb(lx + 1);
} else {
A.pb(lx - 1);
}
B.pb(ly + 1);
} else {
int lx = min(x[a], x[b]);
int ly = y[a];
if (to[i]) {
B.pb(ly - 1);
} else {
B.pb(ly + 1);
}
A.pb(lx + 1);
}
}
build(u, v, A, B);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:111:13: warning: unused variable 'x' [-Wunused-variable]
111 | int x = p.F, y = p.S;
| ^
parks.cpp:111:22: warning: unused variable 'y' [-Wunused-variable]
111 | int x = p.F, y = p.S;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
17 |
Runtime error |
271 ms |
381420 KB |
Execution killed with signal 6 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
17 |
Runtime error |
271 ms |
381420 KB |
Execution killed with signal 6 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
17 |
Correct |
157 ms |
231348 KB |
Output is correct |
18 |
Correct |
159 ms |
231388 KB |
Output is correct |
19 |
Correct |
99 ms |
188148 KB |
Output is correct |
20 |
Correct |
1865 ms |
339216 KB |
Output is correct |
21 |
Correct |
2053 ms |
335100 KB |
Output is correct |
22 |
Correct |
2058 ms |
335412 KB |
Output is correct |
23 |
Correct |
1587 ms |
314380 KB |
Output is correct |
24 |
Correct |
610 ms |
204672 KB |
Output is correct |
25 |
Correct |
1450 ms |
231124 KB |
Output is correct |
26 |
Correct |
1344 ms |
231208 KB |
Output is correct |
27 |
Correct |
1941 ms |
329224 KB |
Output is correct |
28 |
Correct |
2000 ms |
329244 KB |
Output is correct |
29 |
Correct |
2172 ms |
329288 KB |
Output is correct |
30 |
Correct |
2067 ms |
329280 KB |
Output is correct |
31 |
Correct |
174 ms |
231392 KB |
Output is correct |
32 |
Correct |
218 ms |
238500 KB |
Output is correct |
33 |
Correct |
244 ms |
196416 KB |
Output is correct |
34 |
Correct |
1749 ms |
338236 KB |
Output is correct |
35 |
Correct |
110 ms |
190316 KB |
Output is correct |
36 |
Correct |
305 ms |
198900 KB |
Output is correct |
37 |
Correct |
561 ms |
209752 KB |
Output is correct |
38 |
Correct |
747 ms |
271720 KB |
Output is correct |
39 |
Correct |
1026 ms |
286984 KB |
Output is correct |
40 |
Correct |
1337 ms |
302504 KB |
Output is correct |
41 |
Correct |
1674 ms |
317448 KB |
Output is correct |
42 |
Correct |
2037 ms |
332756 KB |
Output is correct |
43 |
Correct |
156 ms |
231356 KB |
Output is correct |
44 |
Correct |
159 ms |
231368 KB |
Output is correct |
45 |
Correct |
161 ms |
231440 KB |
Output is correct |
46 |
Correct |
101 ms |
188176 KB |
Output is correct |
47 |
Correct |
95 ms |
188108 KB |
Output is correct |
48 |
Correct |
173 ms |
231292 KB |
Output is correct |
49 |
Correct |
175 ms |
231312 KB |
Output is correct |
50 |
Correct |
164 ms |
231320 KB |
Output is correct |
51 |
Correct |
189 ms |
231412 KB |
Output is correct |
52 |
Correct |
89 ms |
188196 KB |
Output is correct |
53 |
Correct |
160 ms |
231308 KB |
Output is correct |
54 |
Correct |
98 ms |
188572 KB |
Output is correct |
55 |
Correct |
103 ms |
188864 KB |
Output is correct |
56 |
Correct |
844 ms |
281492 KB |
Output is correct |
57 |
Correct |
1322 ms |
303928 KB |
Output is correct |
58 |
Correct |
1290 ms |
303336 KB |
Output is correct |
59 |
Correct |
114 ms |
188180 KB |
Output is correct |
60 |
Correct |
159 ms |
231336 KB |
Output is correct |
61 |
Correct |
94 ms |
188108 KB |
Output is correct |
62 |
Correct |
1661 ms |
329348 KB |
Output is correct |
63 |
Correct |
1596 ms |
329288 KB |
Output is correct |
64 |
Correct |
1597 ms |
328784 KB |
Output is correct |
65 |
Correct |
108 ms |
188956 KB |
Output is correct |
66 |
Correct |
110 ms |
189900 KB |
Output is correct |
67 |
Correct |
913 ms |
281372 KB |
Output is correct |
68 |
Correct |
1380 ms |
306100 KB |
Output is correct |
69 |
Correct |
1984 ms |
331240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
17 |
Correct |
1641 ms |
329544 KB |
Output is correct |
18 |
Correct |
1692 ms |
329404 KB |
Output is correct |
19 |
Correct |
1874 ms |
337944 KB |
Output is correct |
20 |
Correct |
2090 ms |
338236 KB |
Output is correct |
21 |
Correct |
1664 ms |
320088 KB |
Output is correct |
22 |
Correct |
172 ms |
231312 KB |
Output is correct |
23 |
Correct |
341 ms |
247496 KB |
Output is correct |
24 |
Correct |
175 ms |
192968 KB |
Output is correct |
25 |
Correct |
407 ms |
204896 KB |
Output is correct |
26 |
Correct |
719 ms |
216780 KB |
Output is correct |
27 |
Correct |
948 ms |
284468 KB |
Output is correct |
28 |
Correct |
1304 ms |
297604 KB |
Output is correct |
29 |
Correct |
1399 ms |
310784 KB |
Output is correct |
30 |
Correct |
1484 ms |
323504 KB |
Output is correct |
31 |
Correct |
1715 ms |
337480 KB |
Output is correct |
32 |
Correct |
1678 ms |
335772 KB |
Output is correct |
33 |
Correct |
1359 ms |
329344 KB |
Output is correct |
34 |
Correct |
97 ms |
189252 KB |
Output is correct |
35 |
Correct |
104 ms |
190244 KB |
Output is correct |
36 |
Correct |
815 ms |
282408 KB |
Output is correct |
37 |
Correct |
1209 ms |
308052 KB |
Output is correct |
38 |
Correct |
1702 ms |
334308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
231396 KB |
Output is correct |
2 |
Correct |
155 ms |
231392 KB |
Output is correct |
3 |
Correct |
99 ms |
188156 KB |
Output is correct |
4 |
Correct |
165 ms |
231396 KB |
Output is correct |
5 |
Correct |
163 ms |
231376 KB |
Output is correct |
6 |
Correct |
94 ms |
188128 KB |
Output is correct |
7 |
Correct |
91 ms |
188204 KB |
Output is correct |
8 |
Correct |
88 ms |
188112 KB |
Output is correct |
9 |
Correct |
758 ms |
280288 KB |
Output is correct |
10 |
Correct |
183 ms |
236228 KB |
Output is correct |
11 |
Correct |
332 ms |
257508 KB |
Output is correct |
12 |
Correct |
212 ms |
238748 KB |
Output is correct |
13 |
Correct |
183 ms |
197656 KB |
Output is correct |
14 |
Correct |
95 ms |
188364 KB |
Output is correct |
15 |
Correct |
104 ms |
188588 KB |
Output is correct |
16 |
Correct |
750 ms |
280276 KB |
Output is correct |
17 |
Runtime error |
271 ms |
381420 KB |
Execution killed with signal 6 |
18 |
Halted |
0 ms |
0 KB |
- |