#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) {
if (visited.count({u, v})) return;
++visited[{u, v}];
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]}]);
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});
--mp[{u, v}];
--mp[{u + dk[i][0], v + dk[i][1]}];
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}];
}
}
tot = min(tot, 3LL);
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]});
break;
}
}
mp.erase({u, v});
A.push_back(u);
B.push_back(v);
}
visited.clear();
for (auto [y, z] : mp) {
auto [u, v] = y;
solve(u, v);
}
for (auto [y, z] : mp) {
if (z > 0) return 0;
}
build(U, V, A, B);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Wrong answer detected in grader |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Wrong answer detected in grader |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 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 |
604 KB |
Output is correct |
20 |
Correct |
1059 ms |
120896 KB |
Output is correct |
21 |
Correct |
1052 ms |
114896 KB |
Output is correct |
22 |
Correct |
1102 ms |
111860 KB |
Output is correct |
23 |
Correct |
881 ms |
99352 KB |
Output is correct |
24 |
Correct |
431 ms |
67736 KB |
Output is correct |
25 |
Correct |
423 ms |
42676 KB |
Output is correct |
26 |
Correct |
347 ms |
42652 KB |
Output is correct |
27 |
Correct |
1026 ms |
99348 KB |
Output is correct |
28 |
Correct |
998 ms |
99112 KB |
Output is correct |
29 |
Correct |
1096 ms |
98320 KB |
Output is correct |
30 |
Correct |
1161 ms |
98680 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
54 ms |
7908 KB |
Output is correct |
33 |
Correct |
157 ms |
28240 KB |
Output is correct |
34 |
Correct |
1035 ms |
116284 KB |
Output is correct |
35 |
Correct |
19 ms |
2648 KB |
Output is correct |
36 |
Correct |
67 ms |
10848 KB |
Output is correct |
37 |
Correct |
149 ms |
20816 KB |
Output is correct |
38 |
Correct |
369 ms |
36412 KB |
Output is correct |
39 |
Correct |
575 ms |
49500 KB |
Output is correct |
40 |
Correct |
787 ms |
62232 KB |
Output is correct |
41 |
Correct |
1048 ms |
76500 KB |
Output is correct |
42 |
Correct |
1212 ms |
90740 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
2 ms |
860 KB |
Output is correct |
55 |
Correct |
3 ms |
1116 KB |
Output is correct |
56 |
Correct |
445 ms |
54912 KB |
Output is correct |
57 |
Correct |
773 ms |
81072 KB |
Output is correct |
58 |
Correct |
874 ms |
79636 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
1183 ms |
118080 KB |
Output is correct |
63 |
Correct |
1165 ms |
118620 KB |
Output is correct |
64 |
Correct |
1108 ms |
120444 KB |
Output is correct |
65 |
Correct |
5 ms |
1372 KB |
Output is correct |
66 |
Correct |
9 ms |
2140 KB |
Output is correct |
67 |
Correct |
527 ms |
50308 KB |
Output is correct |
68 |
Correct |
860 ms |
79356 KB |
Output is correct |
69 |
Correct |
1156 ms |
104368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 KB |
Output is correct |
17 |
Correct |
968 ms |
126980 KB |
Output is correct |
18 |
Correct |
983 ms |
123924 KB |
Output is correct |
19 |
Correct |
1168 ms |
118848 KB |
Output is correct |
20 |
Correct |
1271 ms |
117572 KB |
Output is correct |
21 |
Correct |
1100 ms |
100024 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
137 ms |
15736 KB |
Output is correct |
24 |
Correct |
31 ms |
5348 KB |
Output is correct |
25 |
Correct |
102 ms |
14928 KB |
Output is correct |
26 |
Correct |
173 ms |
24152 KB |
Output is correct |
27 |
Correct |
568 ms |
56384 KB |
Output is correct |
28 |
Correct |
749 ms |
69772 KB |
Output is correct |
29 |
Correct |
956 ms |
84892 KB |
Output is correct |
30 |
Correct |
1296 ms |
98208 KB |
Output is correct |
31 |
Correct |
1399 ms |
112824 KB |
Output is correct |
32 |
Correct |
1157 ms |
121520 KB |
Output is correct |
33 |
Correct |
1076 ms |
126220 KB |
Output is correct |
34 |
Correct |
6 ms |
1628 KB |
Output is correct |
35 |
Correct |
11 ms |
2708 KB |
Output is correct |
36 |
Correct |
543 ms |
54528 KB |
Output is correct |
37 |
Correct |
786 ms |
84136 KB |
Output is correct |
38 |
Correct |
1144 ms |
110768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 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 |
512 ms |
61432 KB |
Output is correct |
10 |
Correct |
38 ms |
6592 KB |
Output is correct |
11 |
Correct |
273 ms |
33812 KB |
Output is correct |
12 |
Correct |
67 ms |
9700 KB |
Output is correct |
13 |
Correct |
73 ms |
15916 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
493 ms |
55488 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Wrong answer detected in grader |
18 |
Halted |
0 ms |
0 KB |
- |