#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int p[N];
int sz[N];
set<int> se[N];
set<int> to_cross[N];
set<pair<int, int>> from_cross[N];
int get(int v) {
return p[v] = (p[v] == v ? v : get(p[v]));
}
void unite(int u, int v) {
u = get(u);
v = get(v);
if (sz[u] > sz[v]) swap(u, v);
p[u] = v;
sz[v] += sz[u];
}
int construct_roads(vector<int> vx, vector<int> vy) {
int n = (int) vx.size();
map<pair<int, int>, int> mp;
for (int i = 0; i < n; ++i) {
p[i] = i;
sz[i] = 1;
mp[{vx[i], vy[i]}] = i;
}
for (int i = 0; i < n; ++i) {
int x = vx[i], y = vy[i];
for (int dx : {-2, 0, 2}) {
for (int dy : {-2, 0, 2}) {
if (dx != 0 && dy != 0) continue;
if (mp.count({x + dx, y + dy})) {
int id = mp[{x + dx, y + dy}];
if (get(id) != get(i)) {
unite(i, id);
se[i].insert(id);
se[id].insert(i);
}
}
}
}
}
if (sz[get(1)] != n) {
return 0;
}
set<pair<int, int>> used;
vector<int> u, v, a, b;
for (int i = 0; i < n; ++i) {
bool ok = true;
int x = vx[i], y = vy[i];
for (int dx : {-2, 0, 2}) {
for (int dy : {-2, 0, 2}) {
if (dx != 0 && dy != 0) continue;
ok &= mp.count({x + dx, y + dy});
}
}
if (ok) {
for (int dx : {-2, 2}) {
int id = mp[{x + dx, y}];
u.push_back(i);
v.push_back(id);
int cx = x + dx / 2, cy = y + (dx < 0 ? -1 : 1);
a.push_back(cx);
b.push_back(cy);
used.insert({cx, cy});
se[id].erase(i);
se[i].erase(id);
}
for (int dy : {-2, 2}) {
int id = mp[{x, y + dy}];
u.push_back(i);
v.push_back(id);
int cx = x + (dy < 0 ? 1 : -1), cy = y + dy / 2;
a.push_back(cx);
b.push_back(cy);
used.insert({cx, cy});
se[id].erase(i);
se[i].erase(id);
}
}
}
int cnt = 0;
vector<int> type;
vector<set<int>> g;
map<pair<int, int>, int> crosses;
map<int, pair<int, int>> lines, bck;
for (int i = 0; i < n; ++i) {
int x = vx[i], y = vy[i];
for (int id : se[i]) {
if (id < i) continue;
int xx = vx[id], yy = vy[id];
int line_id = cnt;
type.push_back(0);//type[cnt]
g.push_back({});
lines[cnt++] = {i, id};
for (int d : {-1, 1}) {
int cx, cy;
if (x == xx) {
cx = x + d, cy = (y + yy) / 2;
} else {
cx = (x + xx) / 2, cy = y + d;
}
if (used.count({cx, cy})) {
continue;
}
if (!crosses.count({cx, cy})) {
g.push_back({});
type.push_back(1);
bck[cnt] = {cx, cy};
crosses[{cx, cy}] = cnt++;
}
int cross_id = crosses[{cx, cy}];
g[cross_id].insert(line_id);
g[line_id].insert(cross_id);
}
}
}
queue<int> que;
vector<int> indeg(cnt);
for (int i = 0; i < cnt; ++i) {
indeg[i] = g[i].size();
if (g[i].size() == 1) {
assert(type[i] == 1);
que.push(i);
}
}
set<int> mentioned;
while (!que.empty()) {
int cur = que.front();
que.pop();
if (indeg[cur] == 0) {
continue;
}
if (type[cur] == 0) {
for (int w : g[cur]) {
g[w].erase(cur);
--indeg[w];
if (indeg[w] == 1) {
que.push(w);
}
}
} else {
int line_id = *g[cur].begin();
if (!mentioned.count(line_id)) {
mentioned.insert(line_id);
u.push_back(lines[line_id].first);
v.push_back(lines[line_id].second);
a.push_back(bck[cur].first);
b.push_back(bck[cur].second);
}
for (int w : g[cur]) {
g[w].erase(cur);
--indeg[w];
if (indeg[w] == 1) {
que.push(w);
}
}
}
}
build(u, v, a, b);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
8 ms |
29276 KB |
Output is correct |
19 |
Correct |
10 ms |
29276 KB |
Output is correct |
20 |
Correct |
9 ms |
29276 KB |
Output is correct |
21 |
Correct |
9 ms |
29328 KB |
Output is correct |
22 |
Correct |
9 ms |
29276 KB |
Output is correct |
23 |
Correct |
1492 ms |
179308 KB |
Output is correct |
24 |
Correct |
8 ms |
29272 KB |
Output is correct |
25 |
Correct |
11 ms |
30148 KB |
Output is correct |
26 |
Correct |
12 ms |
29788 KB |
Output is correct |
27 |
Correct |
13 ms |
29788 KB |
Output is correct |
28 |
Correct |
472 ms |
89064 KB |
Output is correct |
29 |
Correct |
809 ms |
124920 KB |
Output is correct |
30 |
Correct |
1152 ms |
150988 KB |
Output is correct |
31 |
Correct |
1516 ms |
179312 KB |
Output is correct |
32 |
Correct |
8 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29256 KB |
Output is correct |
34 |
Correct |
8 ms |
29276 KB |
Output is correct |
35 |
Correct |
8 ms |
29276 KB |
Output is correct |
36 |
Correct |
8 ms |
29276 KB |
Output is correct |
37 |
Correct |
10 ms |
29256 KB |
Output is correct |
38 |
Correct |
9 ms |
29276 KB |
Output is correct |
39 |
Correct |
9 ms |
29144 KB |
Output is correct |
40 |
Correct |
8 ms |
29276 KB |
Output is correct |
41 |
Correct |
9 ms |
29272 KB |
Output is correct |
42 |
Correct |
8 ms |
29276 KB |
Output is correct |
43 |
Correct |
11 ms |
29532 KB |
Output is correct |
44 |
Correct |
11 ms |
29788 KB |
Output is correct |
45 |
Correct |
633 ms |
107436 KB |
Output is correct |
46 |
Correct |
1046 ms |
144860 KB |
Output is correct |
47 |
Correct |
1071 ms |
144836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
8 ms |
29276 KB |
Output is correct |
19 |
Correct |
10 ms |
29276 KB |
Output is correct |
20 |
Correct |
9 ms |
29276 KB |
Output is correct |
21 |
Correct |
9 ms |
29328 KB |
Output is correct |
22 |
Correct |
9 ms |
29276 KB |
Output is correct |
23 |
Correct |
1492 ms |
179308 KB |
Output is correct |
24 |
Correct |
8 ms |
29272 KB |
Output is correct |
25 |
Correct |
11 ms |
30148 KB |
Output is correct |
26 |
Correct |
12 ms |
29788 KB |
Output is correct |
27 |
Correct |
13 ms |
29788 KB |
Output is correct |
28 |
Correct |
472 ms |
89064 KB |
Output is correct |
29 |
Correct |
809 ms |
124920 KB |
Output is correct |
30 |
Correct |
1152 ms |
150988 KB |
Output is correct |
31 |
Correct |
1516 ms |
179312 KB |
Output is correct |
32 |
Correct |
8 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29256 KB |
Output is correct |
34 |
Correct |
8 ms |
29276 KB |
Output is correct |
35 |
Correct |
8 ms |
29276 KB |
Output is correct |
36 |
Correct |
8 ms |
29276 KB |
Output is correct |
37 |
Correct |
10 ms |
29256 KB |
Output is correct |
38 |
Correct |
9 ms |
29276 KB |
Output is correct |
39 |
Correct |
9 ms |
29144 KB |
Output is correct |
40 |
Correct |
8 ms |
29276 KB |
Output is correct |
41 |
Correct |
9 ms |
29272 KB |
Output is correct |
42 |
Correct |
8 ms |
29276 KB |
Output is correct |
43 |
Correct |
11 ms |
29532 KB |
Output is correct |
44 |
Correct |
11 ms |
29788 KB |
Output is correct |
45 |
Correct |
633 ms |
107436 KB |
Output is correct |
46 |
Correct |
1046 ms |
144860 KB |
Output is correct |
47 |
Correct |
1071 ms |
144836 KB |
Output is correct |
48 |
Correct |
8 ms |
29276 KB |
Output is correct |
49 |
Correct |
8 ms |
29272 KB |
Output is correct |
50 |
Correct |
8 ms |
29180 KB |
Output is correct |
51 |
Runtime error |
29 ms |
58960 KB |
Execution killed with signal 6 |
52 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
17 |
Correct |
10 ms |
29276 KB |
Output is correct |
18 |
Correct |
8 ms |
29276 KB |
Output is correct |
19 |
Correct |
9 ms |
29276 KB |
Output is correct |
20 |
Correct |
1574 ms |
173400 KB |
Output is correct |
21 |
Correct |
1568 ms |
173184 KB |
Output is correct |
22 |
Correct |
1604 ms |
173556 KB |
Output is correct |
23 |
Correct |
1159 ms |
182776 KB |
Output is correct |
24 |
Correct |
291 ms |
45744 KB |
Output is correct |
25 |
Correct |
497 ms |
64476 KB |
Output is correct |
26 |
Correct |
433 ms |
64488 KB |
Output is correct |
27 |
Correct |
1481 ms |
210204 KB |
Output is correct |
28 |
Correct |
1502 ms |
210300 KB |
Output is correct |
29 |
Correct |
1528 ms |
210348 KB |
Output is correct |
30 |
Correct |
1514 ms |
210324 KB |
Output is correct |
31 |
Correct |
9 ms |
29528 KB |
Output is correct |
32 |
Correct |
73 ms |
39516 KB |
Output is correct |
33 |
Correct |
118 ms |
37456 KB |
Output is correct |
34 |
Correct |
1600 ms |
173256 KB |
Output is correct |
35 |
Correct |
23 ms |
31068 KB |
Output is correct |
36 |
Correct |
96 ms |
37972 KB |
Output is correct |
37 |
Correct |
214 ms |
46672 KB |
Output is correct |
38 |
Correct |
539 ms |
82664 KB |
Output is correct |
39 |
Correct |
863 ms |
102624 KB |
Output is correct |
40 |
Correct |
1302 ms |
127532 KB |
Output is correct |
41 |
Correct |
1725 ms |
144524 KB |
Output is correct |
42 |
Correct |
2245 ms |
164332 KB |
Output is correct |
43 |
Correct |
11 ms |
29276 KB |
Output is correct |
44 |
Correct |
9 ms |
29276 KB |
Output is correct |
45 |
Correct |
9 ms |
29272 KB |
Output is correct |
46 |
Correct |
10 ms |
29120 KB |
Output is correct |
47 |
Correct |
11 ms |
29276 KB |
Output is correct |
48 |
Correct |
11 ms |
29276 KB |
Output is correct |
49 |
Correct |
8 ms |
29248 KB |
Output is correct |
50 |
Correct |
10 ms |
29324 KB |
Output is correct |
51 |
Correct |
13 ms |
29276 KB |
Output is correct |
52 |
Correct |
9 ms |
29276 KB |
Output is correct |
53 |
Correct |
10 ms |
29276 KB |
Output is correct |
54 |
Correct |
12 ms |
29588 KB |
Output is correct |
55 |
Correct |
14 ms |
30040 KB |
Output is correct |
56 |
Correct |
818 ms |
107536 KB |
Output is correct |
57 |
Correct |
1273 ms |
146544 KB |
Output is correct |
58 |
Correct |
1237 ms |
144844 KB |
Output is correct |
59 |
Correct |
10 ms |
29272 KB |
Output is correct |
60 |
Correct |
10 ms |
29272 KB |
Output is correct |
61 |
Correct |
10 ms |
29100 KB |
Output is correct |
62 |
Correct |
1662 ms |
210508 KB |
Output is correct |
63 |
Correct |
1608 ms |
210316 KB |
Output is correct |
64 |
Correct |
1489 ms |
209648 KB |
Output is correct |
65 |
Correct |
13 ms |
29788 KB |
Output is correct |
66 |
Correct |
20 ms |
30640 KB |
Output is correct |
67 |
Correct |
722 ms |
103268 KB |
Output is correct |
68 |
Correct |
1228 ms |
142540 KB |
Output is correct |
69 |
Correct |
1673 ms |
179660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
17 |
Correct |
1460 ms |
210336 KB |
Output is correct |
18 |
Correct |
1543 ms |
210440 KB |
Output is correct |
19 |
Correct |
1659 ms |
173380 KB |
Output is correct |
20 |
Correct |
1113 ms |
76848 KB |
Output is correct |
21 |
Correct |
1181 ms |
137836 KB |
Output is correct |
22 |
Correct |
9 ms |
29276 KB |
Output is correct |
23 |
Correct |
172 ms |
51368 KB |
Output is correct |
24 |
Correct |
44 ms |
32596 KB |
Output is correct |
25 |
Correct |
153 ms |
41560 KB |
Output is correct |
26 |
Correct |
278 ms |
50260 KB |
Output is correct |
27 |
Correct |
710 ms |
92716 KB |
Output is correct |
28 |
Correct |
926 ms |
107592 KB |
Output is correct |
29 |
Correct |
1160 ms |
129048 KB |
Output is correct |
30 |
Correct |
1409 ms |
141756 KB |
Output is correct |
31 |
Correct |
1648 ms |
158004 KB |
Output is correct |
32 |
Correct |
1568 ms |
188620 KB |
Output is correct |
33 |
Correct |
1519 ms |
210324 KB |
Output is correct |
34 |
Correct |
16 ms |
30044 KB |
Output is correct |
35 |
Correct |
24 ms |
30812 KB |
Output is correct |
36 |
Correct |
690 ms |
103340 KB |
Output is correct |
37 |
Correct |
1166 ms |
142280 KB |
Output is correct |
38 |
Correct |
1560 ms |
179880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
9 ms |
29176 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
9 ms |
29124 KB |
Output is correct |
5 |
Correct |
9 ms |
29132 KB |
Output is correct |
6 |
Correct |
8 ms |
29272 KB |
Output is correct |
7 |
Correct |
9 ms |
29216 KB |
Output is correct |
8 |
Correct |
9 ms |
29276 KB |
Output is correct |
9 |
Correct |
652 ms |
118916 KB |
Output is correct |
10 |
Correct |
43 ms |
38152 KB |
Output is correct |
11 |
Correct |
253 ms |
77796 KB |
Output is correct |
12 |
Correct |
62 ms |
42744 KB |
Output is correct |
13 |
Correct |
47 ms |
36944 KB |
Output is correct |
14 |
Correct |
10 ms |
29272 KB |
Output is correct |
15 |
Correct |
12 ms |
29532 KB |
Output is correct |
16 |
Correct |
688 ms |
118844 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
8 ms |
29276 KB |
Output is correct |
19 |
Correct |
10 ms |
29276 KB |
Output is correct |
20 |
Correct |
9 ms |
29276 KB |
Output is correct |
21 |
Correct |
9 ms |
29328 KB |
Output is correct |
22 |
Correct |
9 ms |
29276 KB |
Output is correct |
23 |
Correct |
1492 ms |
179308 KB |
Output is correct |
24 |
Correct |
8 ms |
29272 KB |
Output is correct |
25 |
Correct |
11 ms |
30148 KB |
Output is correct |
26 |
Correct |
12 ms |
29788 KB |
Output is correct |
27 |
Correct |
13 ms |
29788 KB |
Output is correct |
28 |
Correct |
472 ms |
89064 KB |
Output is correct |
29 |
Correct |
809 ms |
124920 KB |
Output is correct |
30 |
Correct |
1152 ms |
150988 KB |
Output is correct |
31 |
Correct |
1516 ms |
179312 KB |
Output is correct |
32 |
Correct |
8 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29256 KB |
Output is correct |
34 |
Correct |
8 ms |
29276 KB |
Output is correct |
35 |
Correct |
8 ms |
29276 KB |
Output is correct |
36 |
Correct |
8 ms |
29276 KB |
Output is correct |
37 |
Correct |
10 ms |
29256 KB |
Output is correct |
38 |
Correct |
9 ms |
29276 KB |
Output is correct |
39 |
Correct |
9 ms |
29144 KB |
Output is correct |
40 |
Correct |
8 ms |
29276 KB |
Output is correct |
41 |
Correct |
9 ms |
29272 KB |
Output is correct |
42 |
Correct |
8 ms |
29276 KB |
Output is correct |
43 |
Correct |
11 ms |
29532 KB |
Output is correct |
44 |
Correct |
11 ms |
29788 KB |
Output is correct |
45 |
Correct |
633 ms |
107436 KB |
Output is correct |
46 |
Correct |
1046 ms |
144860 KB |
Output is correct |
47 |
Correct |
1071 ms |
144836 KB |
Output is correct |
48 |
Correct |
8 ms |
29276 KB |
Output is correct |
49 |
Correct |
8 ms |
29272 KB |
Output is correct |
50 |
Correct |
8 ms |
29180 KB |
Output is correct |
51 |
Runtime error |
29 ms |
58960 KB |
Execution killed with signal 6 |
52 |
Halted |
0 ms |
0 KB |
- |