/* Ignut
started: 16.08.2024
now: 16.08.2024
████████████████████████████████████████████████████████████████████
████████████████████████████████ ████████████████████████████████
██████████████████████████████ ██████████████████████████████
██████ ██████████████████ ██████████████████ ██████
██████ ██████████████ ██████████████ ██████
██████ ██ ████████████ ████████████ ██ ██████
██████ ████ ██████████ ██████████ ████ ██████
██████ ████ ██████████ ██████████ ████ ██████
██████ ████ ██████████ ██████████ ██████ ██████
██████ ██████ ██████████ ██████████ ██████ ██████
██████ ██████ ████████ ████████ ██████ ██████
██████ ██████ ██████ ██████ ██████ ██████
██████ ████ ████ ████ ████ ██████
██████ ██████████ ████ ██████████ ██████
██████ ██ ██████ ████████ ██████ ██ ██████
██████ ██████ ████████ ██████ ██████
██████ ██ ██ ██████
██████████████████████ ████ ████ ██████████████████████
████████████████████████ ██ ██ ████████████████████████
██████████████████████████ ██████████████████████████
██████████████████████████████ ██████████████████████████████
████████████████████████████████████████████████████████████████████
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void build(vector<int> u, vector<int> v, vector<int> a, vector<int> b);
struct DSU {
int cntComp;
vector<int> p, sz;
void Init(int n) {
cntComp = n;
p.assign(n, 0);
iota(p.begin(), p.end(), 0);
sz.assign(n, 1);
}
int Get(int v) {
return p[v] == v ? v : p[v] = Get(p[v]);
}
void Unite(int u, int v) {
u = Get(u), v = Get(v);
if (u == v) return;
cntComp --;
if (sz[u] > sz[v]) swap(u, v);
p[u] = v;
sz[v] += sz[u];
}
};
int construct_roads(vector<int> x, vector<int> y) {
int n = x.size();
map<pair<int, int>, int> mp;
for (int i = 0; i < n; i ++) mp[{x[i], y[i]}] = i + 1;
DSU dsu; dsu.Init(n);
vector<int> u, v, a, b;
map<tuple<int, int, int, int>, int> have;
auto MakeEdge = [&u, &v, &dsu, &x, &y, &have](int i, int j) -> void {
u.push_back(i);
v.push_back(j);
dsu.Unite(i, j);
have[{x[i], y[i], x[j], y[j]}] ++;
have[{x[j], y[j], x[i], y[j]}] ++;
};
for (int i = 0; i < n; i ++) {
if (mp.count({x[i] + 2, y[i]})) {
int j = mp[{x[i] + 2, y[i]}] - 1;
if (dsu.Get(i) != dsu.Get(j))
MakeEdge(i, j);
}
if (mp.count({x[i], y[i] + 2})) {
int j = mp[{x[i], y[i] + 2}] - 1;
if (dsu.Get(i) != dsu.Get(j))
MakeEdge(i, j);
}
}
if (dsu.cntComp > 1) return 0;
a.assign(n - 1, 0), b.assign(n - 1, 0);
map<tuple<int, int, int, int>, pair<int, int>> res;
for (int i = 0; i < u.size(); i ++) {
if (res.count({x[u[i]], y[u[i]], x[v[i]], y[v[i]]})) {
auto [xx, yy] = res[{x[u[i]], y[u[i]], x[v[i]], y[v[i]]}];
a[i] = xx, b[i] = yy;
continue;
}
if (res.count({x[v[i]], y[v[i]], x[u[i]], y[u[i]]})) {
auto [xx, yy] = res[{x[v[i]], y[v[i]], x[u[i]], y[u[i]]}];
a[i] = xx, b[i] = yy;
continue;
}
int x1 = x[u[i]], y1 = y[u[i]], x2 = x[v[i]], y2 = y[v[i]];
int xf, yf;
if (x1 == x2) xf = x1 - 1;
else xf = (x1 + x2) / 2;
if (y1 == y2) yf = y1 - 1;
else yf = (y1 + y2) / 2;
a[i] = xf, b[i] = yf;
auto Try = [&x1, &y1, &x2, &y2, &xf, &yf, &have, &res](int xx1, int yy1, int xx2, int yy2, int dxf, int dyf) -> bool {
if (res.count({xx1, yy1, xx2, yy2})) return false;
if (res.count({xx2, yy2, xx1, yy1})) return false;
if (have.count({xx1, yy1, xx2, yy2})) {
x1 = xx1, y1 = yy1, x2 = xx2, y2 = yy2;
xf += dxf;
yf += dyf;
return true;
}
return false;
};
while (true) {
res[{x1, y1, x2, y2}] = {xf, yf};
if (x1 == x2) {
if (xf < x1) {
if (Try(x1 - 2, y2, x1, y2, 0, +2)) continue;
if (Try(x1 - 2, y1, x1 - 2, y2, -2, 0)) continue;
if (Try(x1 - 2, y1, x1, y1, 0, -2)) continue;
}
else {
if (Try(x1, y2, x1 + 2, y2, 0, +2)) continue;
if (Try(x1 + 2, y1, x1 + 1, y2, +2, 0)) continue;
if (Try(x1, y1, x1 + 2, y1, 0, -2)) continue;
}
}
else {
if (yf > y1) {
if (Try(x1, y1, x1, y1 + 2, -2, 0)) continue;
if (Try(x1, y1 + 2, x2, y1 + 2, 0, +2)) continue;
if (Try(x2, y2, x2, y2 + 2, +2, 0)) continue;
}
else {
if (Try(x1, y1 - 2, x1, y1, -2, 0)) continue;
if (Try(x1, y1 - 2, x2, y1 - 2, 0, -2)) continue;
if (Try(x2, y1 - 2, x2, y2, +2, 0)) continue;
}
}
break;
}
}
build(u, v, a, b);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < u.size(); i ++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
17 |
Incorrect |
0 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 1 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
17 |
Incorrect |
0 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 1 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
631 ms |
66996 KB |
Output is correct |
21 |
Correct |
759 ms |
66816 KB |
Output is correct |
22 |
Correct |
672 ms |
67036 KB |
Output is correct |
23 |
Correct |
723 ms |
56632 KB |
Output is correct |
24 |
Correct |
167 ms |
19280 KB |
Output is correct |
25 |
Correct |
528 ms |
45624 KB |
Output is correct |
26 |
Correct |
472 ms |
45760 KB |
Output is correct |
27 |
Correct |
859 ms |
66104 KB |
Output is correct |
28 |
Correct |
878 ms |
66436 KB |
Output is correct |
29 |
Correct |
1091 ms |
66120 KB |
Output is correct |
30 |
Correct |
1096 ms |
66100 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
35 ms |
5460 KB |
Output is correct |
33 |
Correct |
74 ms |
10188 KB |
Output is correct |
34 |
Correct |
713 ms |
67132 KB |
Output is correct |
35 |
Correct |
13 ms |
2580 KB |
Output is correct |
36 |
Correct |
82 ms |
11904 KB |
Output is correct |
37 |
Correct |
208 ms |
23412 KB |
Output is correct |
38 |
Correct |
287 ms |
27140 KB |
Output is correct |
39 |
Correct |
423 ms |
37084 KB |
Output is correct |
40 |
Correct |
568 ms |
47060 KB |
Output is correct |
41 |
Correct |
744 ms |
57148 KB |
Output is correct |
42 |
Correct |
896 ms |
67248 KB |
Output is correct |
43 |
Correct |
0 ms |
348 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 |
0 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 |
436 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
732 KB |
Output is correct |
55 |
Correct |
3 ms |
1116 KB |
Output is correct |
56 |
Correct |
332 ms |
33280 KB |
Output is correct |
57 |
Correct |
596 ms |
48188 KB |
Output is correct |
58 |
Correct |
561 ms |
48136 KB |
Output is correct |
59 |
Correct |
0 ms |
600 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
404 KB |
Output is correct |
62 |
Correct |
675 ms |
66100 KB |
Output is correct |
63 |
Correct |
682 ms |
66260 KB |
Output is correct |
64 |
Correct |
688 ms |
65852 KB |
Output is correct |
65 |
Correct |
5 ms |
1116 KB |
Output is correct |
66 |
Correct |
13 ms |
2104 KB |
Output is correct |
67 |
Correct |
335 ms |
33288 KB |
Output is correct |
68 |
Correct |
618 ms |
49956 KB |
Output is correct |
69 |
Correct |
889 ms |
66308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
17 |
Correct |
819 ms |
65692 KB |
Output is correct |
18 |
Correct |
776 ms |
66696 KB |
Output is correct |
19 |
Correct |
701 ms |
66872 KB |
Output is correct |
20 |
Correct |
883 ms |
64788 KB |
Output is correct |
21 |
Correct |
787 ms |
58004 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
88 ms |
11176 KB |
Output is correct |
24 |
Correct |
30 ms |
4948 KB |
Output is correct |
25 |
Correct |
126 ms |
16452 KB |
Output is correct |
26 |
Correct |
270 ms |
28316 KB |
Output is correct |
27 |
Correct |
375 ms |
33600 KB |
Output is correct |
28 |
Correct |
531 ms |
42240 KB |
Output is correct |
29 |
Correct |
636 ms |
50396 KB |
Output is correct |
30 |
Correct |
814 ms |
58432 KB |
Output is correct |
31 |
Correct |
997 ms |
67132 KB |
Output is correct |
32 |
Correct |
896 ms |
66108 KB |
Output is correct |
33 |
Correct |
751 ms |
66124 KB |
Output is correct |
34 |
Correct |
6 ms |
1368 KB |
Output is correct |
35 |
Correct |
11 ms |
2396 KB |
Output is correct |
36 |
Correct |
360 ms |
33280 KB |
Output is correct |
37 |
Correct |
581 ms |
49724 KB |
Output is correct |
38 |
Correct |
876 ms |
66104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
394 ms |
33184 KB |
Output is correct |
10 |
Correct |
15 ms |
3852 KB |
Output is correct |
11 |
Correct |
96 ms |
18164 KB |
Output is correct |
12 |
Correct |
21 ms |
5464 KB |
Output is correct |
13 |
Correct |
43 ms |
10312 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
275 ms |
33072 KB |
Output is correct |
17 |
Incorrect |
0 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 0 and 1 |
18 |
Halted |
0 ms |
0 KB |
- |