#include "parks.h"
#include <iostream>
#include <map>
#include <array>
#include <queue>
#define ll long long
using namespace std;
map <array<ll, 2>, ll> mp;
map <array<ll, 2>, ll> visited;
map <array<ll, 2>, ll> pt;
map <array<ll, 3>, ll> edge;
queue <array<ll, 2>> Q;
vector <int> U, V, A, B;
ll dj[4][2] = {-1, -1, -1, 1, 1, 1, 1, -1}, dk[4][2] = {-2, 0, 0, 2, 2, 0, 0, -2};
void dfs(ll u, ll v) {
++visited[{u, v}];
for (int i=0; i<4; ++i) {
ll nx = u + dk[i][0], ny = v + dk[i][1];
if (pt.count({nx, ny}) && !visited.count({nx, ny})) dfs(nx, ny);
}
}
void solve(ll u, ll v) {
for (int i=0; i<4; ++i) {
if (edge.count({u, v, i})) {
if (visited.count({u, v})) {
while (true) {
}
}
++visited[{u, v}];
U.push_back(pt[{u + dj[i][0], v + dj[i][1]}]);
V.push_back(pt[{u + dj[(i+1)%4][0], v + dj[(i+1)%4][1]}]);
A.push_back(u);
B.push_back(v);
edge.erase({u, v, i});
edge.erase({u + dk[i][0], v + dk[i][1], i ^ 2});
solve(u + dk[i][0], v + dk[i][1]);
break;
}
}
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
ll n = x.size();
for (int i=0; i<n; ++i) {
pt[{x[i], y[i]}] = i;
for (int j=0; j<4; ++j) {
ll nx = x[i] + dj[j][0], ny = y[i] + dj[j][1];
++mp[{nx, ny}];
}
}
dfs(x[0], y[0]);
if (visited.size() != pt.size()) return 0;
for (auto it = mp.begin(); it != mp.end();) {
auto nx = next(it);
auto [u, v] = it->first;
ll tot = 0;
for (int i=0; i<4; ++i) {
if (pt.count({u + dj[i][0], v + dj[i][1]}) && pt.count({u + dj[(i+1)%4][0], v + dj[(i+1)%4][1]})) {
++tot;
++edge[{u, v, i}];
}
}
mp[{u, v}] = tot;
if (tot == 0) mp.erase(it);
else if (tot == 1) Q.push({u, v});
it = nx;
}
while (!Q.empty()) {
auto [u, v] = Q.front();
Q.pop();
if (!mp.count({u, v})) continue;
for (int i=0; i<4; ++i) {
if (edge.count({u, v, i})) {
U.push_back(pt[{u + dj[i][0], v + dj[i][1]}]);
V.push_back(pt[{u + dj[(i+1)%4][0], v + dj[(i+1)%4][1]}]);
edge.erase({u, v, i});
edge.erase({u + dk[i][0], v + dk[i][1], i ^ 2});
--mp[{u + dk[i][0], v + dk[i][1]}];
if (mp[{u + dk[i][0], v + dk[i][1]}] == 1) Q.push({u + dk[i][0], v + dk[i][1]});
else if (mp[{u + dk[i][0], v + dk[i][1]}] == 0) mp.erase({u + dk[i][0], v + dk[i][1]});
}
}
mp.erase({u, v});
A.push_back(u);
B.push_back(v);
}
for (auto [y, z] : mp) {
if (z == 4) return 0;
else if (z != 2) return 0;
}
visited.clear();
for (auto [y, z] : mp) {
auto [u, v] = y;
solve(u, v);
}
build(U, V, A, B);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 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 |
1557 ms |
131400 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1220 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
512 ms |
54156 KB |
Output is correct |
29 |
Correct |
867 ms |
81616 KB |
Output is correct |
30 |
Correct |
1165 ms |
111424 KB |
Output is correct |
31 |
Correct |
1435 ms |
128848 KB |
Output is correct |
32 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
1116 KB |
Output is correct |
45 |
Correct |
519 ms |
54820 KB |
Output is correct |
46 |
Correct |
850 ms |
81068 KB |
Output is correct |
47 |
Correct |
906 ms |
79684 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 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 |
1557 ms |
131400 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1220 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
512 ms |
54156 KB |
Output is correct |
29 |
Correct |
867 ms |
81616 KB |
Output is correct |
30 |
Correct |
1165 ms |
111424 KB |
Output is correct |
31 |
Correct |
1435 ms |
128848 KB |
Output is correct |
32 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
1116 KB |
Output is correct |
45 |
Correct |
519 ms |
54820 KB |
Output is correct |
46 |
Correct |
850 ms |
81068 KB |
Output is correct |
47 |
Correct |
906 ms |
79684 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 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 |
436 KB |
Output is correct |
55 |
Incorrect |
1123 ms |
123776 KB |
Solution announced impossible, but it is possible. |
56 |
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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1117 ms |
120916 KB |
Output is correct |
21 |
Correct |
1165 ms |
115228 KB |
Output is correct |
22 |
Correct |
1129 ms |
111792 KB |
Output is correct |
23 |
Correct |
949 ms |
99352 KB |
Output is correct |
24 |
Correct |
420 ms |
67788 KB |
Output is correct |
25 |
Correct |
412 ms |
42832 KB |
Output is correct |
26 |
Correct |
384 ms |
42916 KB |
Output is correct |
27 |
Correct |
1067 ms |
99304 KB |
Output is correct |
28 |
Correct |
979 ms |
99260 KB |
Output is correct |
29 |
Correct |
1109 ms |
98128 KB |
Output is correct |
30 |
Correct |
1351 ms |
98712 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
60 ms |
8016 KB |
Output is correct |
33 |
Correct |
166 ms |
28084 KB |
Output is correct |
34 |
Correct |
1069 ms |
116260 KB |
Output is correct |
35 |
Correct |
19 ms |
2648 KB |
Output is correct |
36 |
Correct |
75 ms |
10848 KB |
Output is correct |
37 |
Correct |
158 ms |
20824 KB |
Output is correct |
38 |
Correct |
432 ms |
36368 KB |
Output is correct |
39 |
Correct |
669 ms |
49636 KB |
Output is correct |
40 |
Correct |
880 ms |
62272 KB |
Output is correct |
41 |
Correct |
1157 ms |
76640 KB |
Output is correct |
42 |
Correct |
1410 ms |
90700 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
4 ms |
856 KB |
Output is correct |
55 |
Correct |
3 ms |
912 KB |
Output is correct |
56 |
Correct |
554 ms |
54864 KB |
Output is correct |
57 |
Correct |
891 ms |
81012 KB |
Output is correct |
58 |
Correct |
870 ms |
79540 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
1133 ms |
118220 KB |
Output is correct |
63 |
Correct |
1208 ms |
118592 KB |
Output is correct |
64 |
Correct |
1121 ms |
120276 KB |
Output is correct |
65 |
Correct |
8 ms |
1372 KB |
Output is correct |
66 |
Correct |
13 ms |
2136 KB |
Output is correct |
67 |
Correct |
534 ms |
50476 KB |
Output is correct |
68 |
Correct |
946 ms |
79392 KB |
Output is correct |
69 |
Correct |
1369 ms |
104212 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 KB |
Output is correct |
17 |
Correct |
1143 ms |
127020 KB |
Output is correct |
18 |
Correct |
1029 ms |
123804 KB |
Output is correct |
19 |
Correct |
1175 ms |
118736 KB |
Output is correct |
20 |
Correct |
1160 ms |
117576 KB |
Output is correct |
21 |
Correct |
1020 ms |
99888 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
148 ms |
15896 KB |
Output is correct |
24 |
Correct |
38 ms |
5296 KB |
Output is correct |
25 |
Correct |
99 ms |
14924 KB |
Output is correct |
26 |
Correct |
197 ms |
24200 KB |
Output is correct |
27 |
Correct |
645 ms |
56492 KB |
Output is correct |
28 |
Correct |
866 ms |
69720 KB |
Output is correct |
29 |
Correct |
1133 ms |
85156 KB |
Output is correct |
30 |
Correct |
1366 ms |
98220 KB |
Output is correct |
31 |
Correct |
1630 ms |
112688 KB |
Output is correct |
32 |
Correct |
1330 ms |
121520 KB |
Output is correct |
33 |
Correct |
1134 ms |
126220 KB |
Output is correct |
34 |
Correct |
6 ms |
1624 KB |
Output is correct |
35 |
Correct |
11 ms |
2664 KB |
Output is correct |
36 |
Correct |
553 ms |
54464 KB |
Output is correct |
37 |
Correct |
978 ms |
84244 KB |
Output is correct |
38 |
Correct |
1312 ms |
110664 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
440 KB |
Output is correct |
9 |
Correct |
443 ms |
61384 KB |
Output is correct |
10 |
Correct |
37 ms |
6480 KB |
Output is correct |
11 |
Correct |
287 ms |
33768 KB |
Output is correct |
12 |
Correct |
84 ms |
9600 KB |
Output is correct |
13 |
Correct |
78 ms |
15896 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
523 ms |
55700 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 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 |
1557 ms |
131400 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
5 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1220 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
512 ms |
54156 KB |
Output is correct |
29 |
Correct |
867 ms |
81616 KB |
Output is correct |
30 |
Correct |
1165 ms |
111424 KB |
Output is correct |
31 |
Correct |
1435 ms |
128848 KB |
Output is correct |
32 |
Correct |
0 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
1116 KB |
Output is correct |
45 |
Correct |
519 ms |
54820 KB |
Output is correct |
46 |
Correct |
850 ms |
81068 KB |
Output is correct |
47 |
Correct |
906 ms |
79684 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 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 |
436 KB |
Output is correct |
55 |
Incorrect |
1123 ms |
123776 KB |
Solution announced impossible, but it is possible. |
56 |
Halted |
0 ms |
0 KB |
- |