#include <bits/stdc++.h>
#include "parks.h"
#define mp make_pair
#define X first
#define Y second
#define all(a) a.begin(),a.end()
using namespace std;
using pii = pair <int, int>;
const int maxn = 5e5 + 10;
int par [maxn];
inline int get (int x) {
if (par[x] == x) return x;
return par[x] = get (par[x]);
}
inline void unite (int x, int y) {
x = get (x), y = get (y);
par[x] = y;
}
vector <int> graph [maxn][2];
bool used [maxn][2];
vector <int> u, v, c, d;
vector <pii> xx [maxn], yy [maxn];
map <pii, int> mas;
int timer = 0;
int K [maxn], M [maxn], result [maxn];
vector <int> way = {};
inline void dfs (int v, bool B) {
way.push_back (v);
used[v][B] = true;
for (int i : graph[v][B]) if (!used[i][!B]) dfs (i, !B);
}
int construct_roads (vector <int> a, vector <int> b) {
int n = (int) a.size ();
for (int i = 0; i < n; ++i) par[i] = i;
vector <int> _b = a;
sort (all (_b));
int L = _b[0], R = _b.back ();
for (int i = 0; i < n; ++i) xx[a[i]].push_back (mp (b[i], i)), yy[b[i]].push_back (mp (a[i], i));
for (int i = L; i <= R; i += 2) {
sort (all (xx[i]));
int N = (int) xx[i].size ();
for (int j = 0; j < N - 1; ++j) if (xx[i][j].X + 2 == xx[i][j + 1].X) {
int U = xx[i][j].Y, V = xx[i][j + 1].Y;
//cout << "! " << U << ' ' << V << '\n';
if (get (U) == get (V)) continue;
u.push_back (U);
v.push_back (V);
unite (U, V);
}
}
_b = b;
sort (all (_b));
L = _b[0], R = _b.back ();
for (int i = L; i <= R; i += 2) {
sort (all (yy[i]));
int N = (int) yy[i].size ();
for (int j = 0; j < N - 1; ++j) if (yy[i][j].X + 2 == yy[i][j + 1].X) {
int U = yy[i][j].Y, V = yy[i][j + 1].Y;
if (get (U) == get (V)) continue;
u.push_back (U);
v.push_back (V);
unite (U, V);
}
}
if ((int) u.size () != n - 1) return 0;
for (int i = 0; i < n - 1; ++i) {
int m1, m2;
if (a[u[i]] == a[v[i]]) {
m1 = a[u[i]], m2 = b[u[i]] + b[v[i]] >> 1;
++m1;
if (!mas[mp (m1, m2)]) ++timer, mas[mp (m1, m2)] = timer, K[timer] = m1, M[timer] = m2;
graph[i][0].push_back (mas[mp (m1, m2)]);
graph[mas[mp (m1, m2)]][1].push_back (i);
m1 -= 2;
if (!mas[mp (m1, m2)]) ++timer, mas[mp (m1, m2)] = timer, K[timer] = m1, M[timer] = m2;
graph[i][0].push_back (mas[mp (m1, m2)]);
graph[mas[mp (m1, m2)]][1].push_back (i);
} else {
m1 = a[u[i]] + a[v[i]] >> 1, m2 = b[u[i]];
++m2;
if (!mas[mp (m1, m2)]) ++timer, mas[mp (m1, m2)] = timer, K[timer] = m1, M[timer] = m2;
graph[i][0].push_back (mas[mp (m1, m2)]);
graph[mas[mp (m1, m2)]][1].push_back (i);
m2 -= 2;
if (!mas[mp (m1, m2)]) ++timer, mas[mp (m1, m2)] = timer, K[timer] = m1, M[timer] = m2;
graph[i][0].push_back (mas[mp (m1, m2)]);
graph[mas[mp (m1, m2)]][1].push_back (i);
}
}
for (int i = 0; i < timer; ++i) if (graph[i][1].size () == 1 && !used[i][1]) {
way.clear ();
dfs (i, 1);
int N = (int) way.size ();
for (int j = 0; j + 1 < (int) way.size (); j += 2) result[way[j + 1]] = way[j];
}
for (int i = 0; i < timer; ++i) if (!used[i][1]) {
way.clear ();
dfs (i, 1);
int N = (int) way.size ();
for (int j = 0; j + 1 < (int) way.size (); j += 2) result[way[j + 1]] = way[j];
}
for (int i = 0; i < n - 1; ++i) c.push_back (K[result[i]]), d.push_back (M[result[i]]);
build (u, v, c, d);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:77:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | m1 = a[u[i]], m2 = b[u[i]] + b[v[i]] >> 1;
parks.cpp:87:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | m1 = a[u[i]] + a[v[i]] >> 1, m2 = b[u[i]];
parks.cpp:101:7: warning: unused variable 'N' [-Wunused-variable]
101 | int N = (int) way.size ();
| ^
parks.cpp:107:7: warning: unused variable 'N' [-Wunused-variable]
107 | int N = (int) way.size ();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
17 |
Incorrect |
25 ms |
47224 KB |
Tree (a[2], b[2]) = (5, 3) is not adjacent to edge between u[2]=3 @(2, 2) and v[2]=2 @(4, 2) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
17 |
Incorrect |
25 ms |
47224 KB |
Tree (a[2], b[2]) = (5, 3) is not adjacent to edge between u[2]=3 @(2, 2) and v[2]=2 @(4, 2) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
17 |
Correct |
19 ms |
47276 KB |
Output is correct |
18 |
Correct |
19 ms |
47272 KB |
Output is correct |
19 |
Correct |
19 ms |
47188 KB |
Output is correct |
20 |
Correct |
396 ms |
111468 KB |
Output is correct |
21 |
Correct |
387 ms |
100660 KB |
Output is correct |
22 |
Correct |
444 ms |
100748 KB |
Output is correct |
23 |
Correct |
329 ms |
105120 KB |
Output is correct |
24 |
Correct |
85 ms |
55884 KB |
Output is correct |
25 |
Correct |
84 ms |
57796 KB |
Output is correct |
26 |
Correct |
88 ms |
57784 KB |
Output is correct |
27 |
Correct |
338 ms |
113608 KB |
Output is correct |
28 |
Correct |
320 ms |
113576 KB |
Output is correct |
29 |
Correct |
400 ms |
113564 KB |
Output is correct |
30 |
Correct |
479 ms |
113660 KB |
Output is correct |
31 |
Correct |
23 ms |
47316 KB |
Output is correct |
32 |
Correct |
57 ms |
51332 KB |
Output is correct |
33 |
Correct |
78 ms |
55884 KB |
Output is correct |
34 |
Correct |
381 ms |
111500 KB |
Output is correct |
35 |
Correct |
26 ms |
47956 KB |
Output is correct |
36 |
Correct |
41 ms |
50416 KB |
Output is correct |
37 |
Correct |
60 ms |
53288 KB |
Output is correct |
38 |
Correct |
178 ms |
67700 KB |
Output is correct |
39 |
Correct |
253 ms |
75460 KB |
Output is correct |
40 |
Correct |
334 ms |
82892 KB |
Output is correct |
41 |
Correct |
381 ms |
90336 KB |
Output is correct |
42 |
Correct |
455 ms |
97752 KB |
Output is correct |
43 |
Correct |
23 ms |
47188 KB |
Output is correct |
44 |
Correct |
28 ms |
47188 KB |
Output is correct |
45 |
Correct |
28 ms |
47188 KB |
Output is correct |
46 |
Correct |
23 ms |
47220 KB |
Output is correct |
47 |
Correct |
29 ms |
47204 KB |
Output is correct |
48 |
Correct |
23 ms |
47188 KB |
Output is correct |
49 |
Correct |
27 ms |
47244 KB |
Output is correct |
50 |
Correct |
27 ms |
47204 KB |
Output is correct |
51 |
Correct |
27 ms |
47300 KB |
Output is correct |
52 |
Correct |
27 ms |
47180 KB |
Output is correct |
53 |
Correct |
22 ms |
47188 KB |
Output is correct |
54 |
Correct |
25 ms |
47436 KB |
Output is correct |
55 |
Correct |
23 ms |
47464 KB |
Output is correct |
56 |
Correct |
194 ms |
75364 KB |
Output is correct |
57 |
Correct |
295 ms |
87780 KB |
Output is correct |
58 |
Correct |
286 ms |
87708 KB |
Output is correct |
59 |
Correct |
29 ms |
47188 KB |
Output is correct |
60 |
Correct |
22 ms |
47188 KB |
Output is correct |
61 |
Correct |
23 ms |
47292 KB |
Output is correct |
62 |
Correct |
455 ms |
114784 KB |
Output is correct |
63 |
Correct |
440 ms |
116076 KB |
Output is correct |
64 |
Correct |
410 ms |
115048 KB |
Output is correct |
65 |
Correct |
23 ms |
47548 KB |
Output is correct |
66 |
Correct |
25 ms |
47776 KB |
Output is correct |
67 |
Correct |
213 ms |
74340 KB |
Output is correct |
68 |
Correct |
316 ms |
87652 KB |
Output is correct |
69 |
Correct |
427 ms |
101104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
17 |
Correct |
387 ms |
115372 KB |
Output is correct |
18 |
Correct |
380 ms |
115408 KB |
Output is correct |
19 |
Correct |
402 ms |
110072 KB |
Output is correct |
20 |
Correct |
386 ms |
98392 KB |
Output is correct |
21 |
Correct |
349 ms |
102804 KB |
Output is correct |
22 |
Correct |
23 ms |
47188 KB |
Output is correct |
23 |
Correct |
83 ms |
56356 KB |
Output is correct |
24 |
Correct |
30 ms |
48484 KB |
Output is correct |
25 |
Correct |
55 ms |
51672 KB |
Output is correct |
26 |
Correct |
72 ms |
54392 KB |
Output is correct |
27 |
Correct |
220 ms |
74004 KB |
Output is correct |
28 |
Correct |
276 ms |
80652 KB |
Output is correct |
29 |
Correct |
327 ms |
86896 KB |
Output is correct |
30 |
Correct |
395 ms |
93216 KB |
Output is correct |
31 |
Correct |
456 ms |
99660 KB |
Output is correct |
32 |
Correct |
404 ms |
107548 KB |
Output is correct |
33 |
Correct |
421 ms |
116076 KB |
Output is correct |
34 |
Correct |
24 ms |
47572 KB |
Output is correct |
35 |
Correct |
26 ms |
47904 KB |
Output is correct |
36 |
Correct |
214 ms |
75296 KB |
Output is correct |
37 |
Correct |
304 ms |
89196 KB |
Output is correct |
38 |
Correct |
405 ms |
103144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
47196 KB |
Output is correct |
2 |
Correct |
21 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47192 KB |
Output is correct |
4 |
Correct |
20 ms |
47188 KB |
Output is correct |
5 |
Correct |
19 ms |
47260 KB |
Output is correct |
6 |
Correct |
20 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47284 KB |
Output is correct |
8 |
Correct |
21 ms |
47288 KB |
Output is correct |
9 |
Correct |
236 ms |
83972 KB |
Output is correct |
10 |
Correct |
33 ms |
51012 KB |
Output is correct |
11 |
Correct |
108 ms |
66792 KB |
Output is correct |
12 |
Correct |
43 ms |
52896 KB |
Output is correct |
13 |
Correct |
41 ms |
50712 KB |
Output is correct |
14 |
Correct |
20 ms |
47360 KB |
Output is correct |
15 |
Correct |
22 ms |
47324 KB |
Output is correct |
16 |
Correct |
289 ms |
83868 KB |
Output is correct |
17 |
Incorrect |
25 ms |
47224 KB |
Tree (a[2], b[2]) = (5, 3) is not adjacent to edge between u[2]=3 @(2, 2) and v[2]=2 @(4, 2) |
18 |
Halted |
0 ms |
0 KB |
- |