#include "swap.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
namespace
{
int N, M, K, t, dp[300000], dsu[100000], rk[100000], node[100000], pre[300000][20];
int chain[100000][2];
pii io[300000];
vector<pair<int, pii>> edge;
vector<int> E[300000];
int find(int k)
{
return dsu[k] == k ? k : dsu[k] = find(dsu[k]);
}
void merge(int x, int y, int u, int v, int w)
{
x = find(x), y = find(y);
if (x == y)
{
chain[x][0] = chain[x][1] = -1;
dp[++K] = w;
E[K].emplace_back(node[x]);
node[x] = K;
return;
}
if (rk[x] < rk[y])
{
merge(y, x, u, v, w);
return;
}
dsu[y] = x;
rk[x] += rk[y];
bool flag = 0;
//fprintf(stderr, "(%d, %d) (%d, %d) (%d, %d) -> ", chain[x][0], chain[x][1], u, v, chain[y][0], chain[y][1]);
for (int i = 0; (!flag) && i < 2; i++)
for (int j = 0; (!flag) && j < 2; j++)
if ((chain[x][i] == u && chain[y][j] == v) || (chain[x][i] == v && chain[y][j] == u))
{
flag = 1;
chain[x][i] = chain[y][j ^ 1];
}
if (!flag)
chain[x][0] = chain[x][1] = -1;
if (chain[x][0] != -1)
w = 2e9;
//fprintf(stderr, "(%d, %d)\n", chain[x][0], chain[x][1]);
dp[++K] = w;
E[K].emplace_back(node[x]);
E[K].emplace_back(node[y]);
node[x] = K;
}
void dfs(int u)
{
io[u].F = ++t;
for (auto v : E[u])
{
pre[v][0] = u;
dp[v] = min(dp[v], dp[u]);
dfs(v);
}
io[u].S = t;
}
bool isChild(int r, int c)
{
return io[r].F <= io[c].F && io[c].S <= io[r].S;
}
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
::N = N, ::M = M, ::K = N - 1;
for (int i = 0; i < N; i++)
node[i] = dsu[i] = chain[i][0] = chain[i][1] = i, rk[i] = 1;
for (int i = 0; i < M; i++)
edge.emplace_back(make_pair(W[i], pii(U[i], V[i])));
sort(edge.begin(), edge.end());
for (auto [w, p] : edge)
{
auto [u, v] = p;
merge(u, v, u, v, w);
}
pre[K][0] = K;
dfs(K);
for (int p = 0; p < 19; p++)
for (int i = 0; i <= K; i++)
pre[i][p + 1] = pre[pre[i][p]][p];
}
int getMinimumFuelCapacity(int X, int Y)
{
int lca = X, ans = 2e9;
for (int p = 19; p >= 0; p--)
if (!isChild(pre[lca][p], Y))
lca = pre[lca][p];
if (!isChild(lca, Y))
lca = pre[lca][0];
ans = dp[lca];
return (ans > 1.5e9 ? -1 : ans);
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:88:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | for (auto [w, p] : edge)
| ^
swap.cpp:90:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | auto [u, v] = p;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
4 ms |
7624 KB |
Output is correct |
7 |
Correct |
4 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
89 ms |
29908 KB |
Output is correct |
10 |
Correct |
109 ms |
35076 KB |
Output is correct |
11 |
Correct |
108 ms |
34512 KB |
Output is correct |
12 |
Correct |
114 ms |
36088 KB |
Output is correct |
13 |
Correct |
108 ms |
39104 KB |
Output is correct |
14 |
Correct |
99 ms |
30148 KB |
Output is correct |
15 |
Correct |
188 ms |
39184 KB |
Output is correct |
16 |
Correct |
190 ms |
38496 KB |
Output is correct |
17 |
Correct |
197 ms |
40124 KB |
Output is correct |
18 |
Correct |
261 ms |
43196 KB |
Output is correct |
19 |
Correct |
86 ms |
17036 KB |
Output is correct |
20 |
Correct |
204 ms |
40392 KB |
Output is correct |
21 |
Correct |
231 ms |
39616 KB |
Output is correct |
22 |
Correct |
222 ms |
41512 KB |
Output is correct |
23 |
Correct |
245 ms |
44732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
229 ms |
41472 KB |
Output is correct |
4 |
Correct |
245 ms |
46764 KB |
Output is correct |
5 |
Correct |
261 ms |
46072 KB |
Output is correct |
6 |
Correct |
277 ms |
46624 KB |
Output is correct |
7 |
Correct |
276 ms |
46652 KB |
Output is correct |
8 |
Correct |
270 ms |
45144 KB |
Output is correct |
9 |
Correct |
270 ms |
46292 KB |
Output is correct |
10 |
Correct |
264 ms |
44836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
4 ms |
7624 KB |
Output is correct |
7 |
Correct |
4 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7680 KB |
Output is correct |
11 |
Correct |
5 ms |
7628 KB |
Output is correct |
12 |
Correct |
7 ms |
7628 KB |
Output is correct |
13 |
Correct |
5 ms |
7508 KB |
Output is correct |
14 |
Correct |
6 ms |
7508 KB |
Output is correct |
15 |
Correct |
7 ms |
7636 KB |
Output is correct |
16 |
Correct |
7 ms |
7624 KB |
Output is correct |
17 |
Correct |
5 ms |
7636 KB |
Output is correct |
18 |
Correct |
5 ms |
7636 KB |
Output is correct |
19 |
Correct |
5 ms |
7640 KB |
Output is correct |
20 |
Correct |
4 ms |
7636 KB |
Output is correct |
21 |
Correct |
6 ms |
7624 KB |
Output is correct |
22 |
Correct |
5 ms |
7764 KB |
Output is correct |
23 |
Correct |
5 ms |
7508 KB |
Output is correct |
24 |
Correct |
5 ms |
7764 KB |
Output is correct |
25 |
Correct |
5 ms |
7756 KB |
Output is correct |
26 |
Correct |
5 ms |
7892 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
5 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7396 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7636 KB |
Output is correct |
7 |
Correct |
4 ms |
7624 KB |
Output is correct |
8 |
Correct |
4 ms |
7624 KB |
Output is correct |
9 |
Correct |
5 ms |
7636 KB |
Output is correct |
10 |
Correct |
89 ms |
29908 KB |
Output is correct |
11 |
Correct |
109 ms |
35076 KB |
Output is correct |
12 |
Correct |
108 ms |
34512 KB |
Output is correct |
13 |
Correct |
114 ms |
36088 KB |
Output is correct |
14 |
Correct |
108 ms |
39104 KB |
Output is correct |
15 |
Correct |
5 ms |
7680 KB |
Output is correct |
16 |
Correct |
5 ms |
7628 KB |
Output is correct |
17 |
Correct |
7 ms |
7628 KB |
Output is correct |
18 |
Correct |
5 ms |
7508 KB |
Output is correct |
19 |
Correct |
6 ms |
7508 KB |
Output is correct |
20 |
Correct |
7 ms |
7636 KB |
Output is correct |
21 |
Correct |
7 ms |
7624 KB |
Output is correct |
22 |
Correct |
5 ms |
7636 KB |
Output is correct |
23 |
Correct |
5 ms |
7636 KB |
Output is correct |
24 |
Correct |
5 ms |
7640 KB |
Output is correct |
25 |
Correct |
4 ms |
7636 KB |
Output is correct |
26 |
Correct |
6 ms |
7624 KB |
Output is correct |
27 |
Correct |
5 ms |
7764 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7764 KB |
Output is correct |
30 |
Correct |
5 ms |
7756 KB |
Output is correct |
31 |
Correct |
5 ms |
7892 KB |
Output is correct |
32 |
Correct |
4 ms |
7636 KB |
Output is correct |
33 |
Correct |
5 ms |
7892 KB |
Output is correct |
34 |
Correct |
16 ms |
11228 KB |
Output is correct |
35 |
Correct |
116 ms |
35776 KB |
Output is correct |
36 |
Correct |
127 ms |
35800 KB |
Output is correct |
37 |
Correct |
114 ms |
35760 KB |
Output is correct |
38 |
Correct |
106 ms |
35368 KB |
Output is correct |
39 |
Correct |
111 ms |
35300 KB |
Output is correct |
40 |
Correct |
113 ms |
32956 KB |
Output is correct |
41 |
Correct |
111 ms |
36080 KB |
Output is correct |
42 |
Correct |
116 ms |
35816 KB |
Output is correct |
43 |
Correct |
106 ms |
39932 KB |
Output is correct |
44 |
Correct |
115 ms |
36112 KB |
Output is correct |
45 |
Correct |
138 ms |
47896 KB |
Output is correct |
46 |
Correct |
115 ms |
35772 KB |
Output is correct |
47 |
Correct |
120 ms |
35900 KB |
Output is correct |
48 |
Correct |
115 ms |
39872 KB |
Output is correct |
49 |
Correct |
102 ms |
44776 KB |
Output is correct |
50 |
Correct |
78 ms |
36536 KB |
Output is correct |
51 |
Correct |
122 ms |
42420 KB |
Output is correct |
52 |
Correct |
158 ms |
50812 KB |
Output is correct |
53 |
Correct |
174 ms |
53584 KB |
Output is correct |
54 |
Correct |
192 ms |
59824 KB |
Output is correct |
55 |
Correct |
123 ms |
39532 KB |
Output is correct |
56 |
Correct |
158 ms |
55356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7636 KB |
Output is correct |
6 |
Correct |
4 ms |
7624 KB |
Output is correct |
7 |
Correct |
4 ms |
7624 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
89 ms |
29908 KB |
Output is correct |
10 |
Correct |
109 ms |
35076 KB |
Output is correct |
11 |
Correct |
108 ms |
34512 KB |
Output is correct |
12 |
Correct |
114 ms |
36088 KB |
Output is correct |
13 |
Correct |
108 ms |
39104 KB |
Output is correct |
14 |
Correct |
99 ms |
30148 KB |
Output is correct |
15 |
Correct |
188 ms |
39184 KB |
Output is correct |
16 |
Correct |
190 ms |
38496 KB |
Output is correct |
17 |
Correct |
197 ms |
40124 KB |
Output is correct |
18 |
Correct |
261 ms |
43196 KB |
Output is correct |
19 |
Correct |
229 ms |
41472 KB |
Output is correct |
20 |
Correct |
245 ms |
46764 KB |
Output is correct |
21 |
Correct |
261 ms |
46072 KB |
Output is correct |
22 |
Correct |
277 ms |
46624 KB |
Output is correct |
23 |
Correct |
276 ms |
46652 KB |
Output is correct |
24 |
Correct |
270 ms |
45144 KB |
Output is correct |
25 |
Correct |
270 ms |
46292 KB |
Output is correct |
26 |
Correct |
264 ms |
44836 KB |
Output is correct |
27 |
Correct |
5 ms |
7680 KB |
Output is correct |
28 |
Correct |
5 ms |
7628 KB |
Output is correct |
29 |
Correct |
7 ms |
7628 KB |
Output is correct |
30 |
Correct |
5 ms |
7508 KB |
Output is correct |
31 |
Correct |
6 ms |
7508 KB |
Output is correct |
32 |
Correct |
7 ms |
7636 KB |
Output is correct |
33 |
Correct |
7 ms |
7624 KB |
Output is correct |
34 |
Correct |
5 ms |
7636 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
16 ms |
11228 KB |
Output is correct |
37 |
Correct |
116 ms |
35776 KB |
Output is correct |
38 |
Correct |
127 ms |
35800 KB |
Output is correct |
39 |
Correct |
114 ms |
35760 KB |
Output is correct |
40 |
Correct |
106 ms |
35368 KB |
Output is correct |
41 |
Correct |
111 ms |
35300 KB |
Output is correct |
42 |
Correct |
113 ms |
32956 KB |
Output is correct |
43 |
Correct |
111 ms |
36080 KB |
Output is correct |
44 |
Correct |
116 ms |
35816 KB |
Output is correct |
45 |
Correct |
106 ms |
39932 KB |
Output is correct |
46 |
Correct |
115 ms |
36112 KB |
Output is correct |
47 |
Correct |
21 ms |
11360 KB |
Output is correct |
48 |
Correct |
197 ms |
40612 KB |
Output is correct |
49 |
Correct |
201 ms |
40680 KB |
Output is correct |
50 |
Correct |
192 ms |
40596 KB |
Output is correct |
51 |
Correct |
219 ms |
40612 KB |
Output is correct |
52 |
Correct |
181 ms |
38808 KB |
Output is correct |
53 |
Correct |
178 ms |
32248 KB |
Output is correct |
54 |
Correct |
195 ms |
41488 KB |
Output is correct |
55 |
Correct |
196 ms |
40572 KB |
Output is correct |
56 |
Correct |
266 ms |
44052 KB |
Output is correct |
57 |
Correct |
214 ms |
41648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7396 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7636 KB |
Output is correct |
7 |
Correct |
4 ms |
7624 KB |
Output is correct |
8 |
Correct |
4 ms |
7624 KB |
Output is correct |
9 |
Correct |
5 ms |
7636 KB |
Output is correct |
10 |
Correct |
89 ms |
29908 KB |
Output is correct |
11 |
Correct |
109 ms |
35076 KB |
Output is correct |
12 |
Correct |
108 ms |
34512 KB |
Output is correct |
13 |
Correct |
114 ms |
36088 KB |
Output is correct |
14 |
Correct |
108 ms |
39104 KB |
Output is correct |
15 |
Correct |
99 ms |
30148 KB |
Output is correct |
16 |
Correct |
188 ms |
39184 KB |
Output is correct |
17 |
Correct |
190 ms |
38496 KB |
Output is correct |
18 |
Correct |
197 ms |
40124 KB |
Output is correct |
19 |
Correct |
261 ms |
43196 KB |
Output is correct |
20 |
Correct |
229 ms |
41472 KB |
Output is correct |
21 |
Correct |
245 ms |
46764 KB |
Output is correct |
22 |
Correct |
261 ms |
46072 KB |
Output is correct |
23 |
Correct |
277 ms |
46624 KB |
Output is correct |
24 |
Correct |
276 ms |
46652 KB |
Output is correct |
25 |
Correct |
270 ms |
45144 KB |
Output is correct |
26 |
Correct |
270 ms |
46292 KB |
Output is correct |
27 |
Correct |
264 ms |
44836 KB |
Output is correct |
28 |
Correct |
5 ms |
7680 KB |
Output is correct |
29 |
Correct |
5 ms |
7628 KB |
Output is correct |
30 |
Correct |
7 ms |
7628 KB |
Output is correct |
31 |
Correct |
5 ms |
7508 KB |
Output is correct |
32 |
Correct |
6 ms |
7508 KB |
Output is correct |
33 |
Correct |
7 ms |
7636 KB |
Output is correct |
34 |
Correct |
7 ms |
7624 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
5 ms |
7636 KB |
Output is correct |
37 |
Correct |
16 ms |
11228 KB |
Output is correct |
38 |
Correct |
116 ms |
35776 KB |
Output is correct |
39 |
Correct |
127 ms |
35800 KB |
Output is correct |
40 |
Correct |
114 ms |
35760 KB |
Output is correct |
41 |
Correct |
106 ms |
35368 KB |
Output is correct |
42 |
Correct |
111 ms |
35300 KB |
Output is correct |
43 |
Correct |
113 ms |
32956 KB |
Output is correct |
44 |
Correct |
111 ms |
36080 KB |
Output is correct |
45 |
Correct |
116 ms |
35816 KB |
Output is correct |
46 |
Correct |
106 ms |
39932 KB |
Output is correct |
47 |
Correct |
115 ms |
36112 KB |
Output is correct |
48 |
Correct |
21 ms |
11360 KB |
Output is correct |
49 |
Correct |
197 ms |
40612 KB |
Output is correct |
50 |
Correct |
201 ms |
40680 KB |
Output is correct |
51 |
Correct |
192 ms |
40596 KB |
Output is correct |
52 |
Correct |
219 ms |
40612 KB |
Output is correct |
53 |
Correct |
181 ms |
38808 KB |
Output is correct |
54 |
Correct |
178 ms |
32248 KB |
Output is correct |
55 |
Correct |
195 ms |
41488 KB |
Output is correct |
56 |
Correct |
196 ms |
40572 KB |
Output is correct |
57 |
Correct |
266 ms |
44052 KB |
Output is correct |
58 |
Correct |
214 ms |
41648 KB |
Output is correct |
59 |
Correct |
86 ms |
17036 KB |
Output is correct |
60 |
Correct |
204 ms |
40392 KB |
Output is correct |
61 |
Correct |
231 ms |
39616 KB |
Output is correct |
62 |
Correct |
222 ms |
41512 KB |
Output is correct |
63 |
Correct |
245 ms |
44732 KB |
Output is correct |
64 |
Correct |
5 ms |
7640 KB |
Output is correct |
65 |
Correct |
4 ms |
7636 KB |
Output is correct |
66 |
Correct |
6 ms |
7624 KB |
Output is correct |
67 |
Correct |
5 ms |
7764 KB |
Output is correct |
68 |
Correct |
5 ms |
7508 KB |
Output is correct |
69 |
Correct |
5 ms |
7764 KB |
Output is correct |
70 |
Correct |
5 ms |
7756 KB |
Output is correct |
71 |
Correct |
5 ms |
7892 KB |
Output is correct |
72 |
Correct |
4 ms |
7636 KB |
Output is correct |
73 |
Correct |
5 ms |
7892 KB |
Output is correct |
74 |
Correct |
138 ms |
47896 KB |
Output is correct |
75 |
Correct |
115 ms |
35772 KB |
Output is correct |
76 |
Correct |
120 ms |
35900 KB |
Output is correct |
77 |
Correct |
115 ms |
39872 KB |
Output is correct |
78 |
Correct |
102 ms |
44776 KB |
Output is correct |
79 |
Correct |
78 ms |
36536 KB |
Output is correct |
80 |
Correct |
122 ms |
42420 KB |
Output is correct |
81 |
Correct |
158 ms |
50812 KB |
Output is correct |
82 |
Correct |
174 ms |
53584 KB |
Output is correct |
83 |
Correct |
192 ms |
59824 KB |
Output is correct |
84 |
Correct |
123 ms |
39532 KB |
Output is correct |
85 |
Correct |
158 ms |
55356 KB |
Output is correct |
86 |
Correct |
79 ms |
22452 KB |
Output is correct |
87 |
Correct |
220 ms |
40576 KB |
Output is correct |
88 |
Correct |
208 ms |
40744 KB |
Output is correct |
89 |
Correct |
256 ms |
43288 KB |
Output is correct |
90 |
Correct |
192 ms |
50844 KB |
Output is correct |
91 |
Correct |
204 ms |
47916 KB |
Output is correct |
92 |
Correct |
286 ms |
46076 KB |
Output is correct |
93 |
Correct |
271 ms |
54932 KB |
Output is correct |
94 |
Correct |
318 ms |
58100 KB |
Output is correct |
95 |
Correct |
288 ms |
63704 KB |
Output is correct |
96 |
Correct |
275 ms |
44184 KB |
Output is correct |
97 |
Correct |
308 ms |
51336 KB |
Output is correct |