#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 400004;
int N, M, R[MAX], D[MAX], H[MAX], S[MAX], P[19][MAX];
bool T[MAX], C[MAX], isLine;
vector<tuple<int, int, int>> E;
vector<int> G[MAX];
int Find(int n) {
if (n == R[n]) return n;
return R[n] = Find(R[n]);
}
void DFS(int cur, int prev) {
H[cur] = H[prev] + 1;
P[0][cur] = prev;
for (int next : G[cur]) DFS(next, cur);
}
int LCA(int a, int b) {
if (H[a] > H[b]) swap(a, b);
for (int i = 18; i >= 0; --i) if ((H[b] - H[a]) & (1 << i)) b = P[i][b];
if (a == b) return a;
for (int i = 18; i >= 0; --i) if (P[i][a] != P[i][b]) a = P[i][a], b = P[i][b];
return P[0][a];
}
void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
N = _N, M = _M;
for (int i = 0; i < M; ++i) {
E.emplace_back(W[i], U[i], V[i]);
D[U[i]]++;
D[V[i]]++;
}
if (N == M + 1 && *max_element(D, D + MAX) <= 2) isLine = 1;
sort(E.begin(), E.end());
iota(R, R + MAX, 0);
memset(D, 0, sizeof(D));
int node = N;
for (auto [w, a, b] : E) {
int ra = Find(a), rb = Find(b);
S[node] = w;
T[node] = (T[ra] || T[rb] || ++D[a] >= 3 || ++D[b] >= 3);
C[node] = (C[ra] || C[rb] || ra == rb);
G[node].push_back(ra);
if (ra != rb) G[node].push_back(rb);
R[ra] = R[rb] = node;
node++;
}
DFS(node - 1, node - 1);
for (int n = 1; n < 19; ++n) for (int i = 0; i < node; ++i) P[n][i] = P[n - 1][P[n - 1][i]];
}
int getMinimumFuelCapacity(int x, int y) {
if (isLine) return -1;
int c = LCA(x, y);
for (int i = 18; i >= 0; --i) {
int p = P[i][c];
if (!T[p] && !C[p]) c = p;
}
if (!T[c] && !C[c]) c = P[0][c];
return S[c];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
46428 KB |
Output is correct |
2 |
Correct |
8 ms |
46428 KB |
Output is correct |
3 |
Correct |
8 ms |
46564 KB |
Output is correct |
4 |
Correct |
9 ms |
46432 KB |
Output is correct |
5 |
Correct |
8 ms |
46684 KB |
Output is correct |
6 |
Correct |
8 ms |
46684 KB |
Output is correct |
7 |
Correct |
9 ms |
46684 KB |
Output is correct |
8 |
Correct |
8 ms |
46684 KB |
Output is correct |
9 |
Correct |
47 ms |
53316 KB |
Output is correct |
10 |
Correct |
57 ms |
54724 KB |
Output is correct |
11 |
Correct |
54 ms |
54648 KB |
Output is correct |
12 |
Correct |
58 ms |
55256 KB |
Output is correct |
13 |
Correct |
52 ms |
56628 KB |
Output is correct |
14 |
Correct |
51 ms |
53752 KB |
Output is correct |
15 |
Correct |
119 ms |
58228 KB |
Output is correct |
16 |
Correct |
100 ms |
58088 KB |
Output is correct |
17 |
Correct |
99 ms |
58664 KB |
Output is correct |
18 |
Correct |
96 ms |
60176 KB |
Output is correct |
19 |
Correct |
80 ms |
52756 KB |
Output is correct |
20 |
Correct |
208 ms |
59364 KB |
Output is correct |
21 |
Correct |
154 ms |
59520 KB |
Output is correct |
22 |
Correct |
169 ms |
60204 KB |
Output is correct |
23 |
Correct |
208 ms |
61744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
46428 KB |
Output is correct |
2 |
Correct |
8 ms |
46428 KB |
Output is correct |
3 |
Correct |
203 ms |
61768 KB |
Output is correct |
4 |
Correct |
213 ms |
63024 KB |
Output is correct |
5 |
Correct |
189 ms |
62808 KB |
Output is correct |
6 |
Correct |
183 ms |
62688 KB |
Output is correct |
7 |
Correct |
213 ms |
62900 KB |
Output is correct |
8 |
Correct |
175 ms |
62344 KB |
Output is correct |
9 |
Correct |
182 ms |
62712 KB |
Output is correct |
10 |
Correct |
192 ms |
62356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
46428 KB |
Output is correct |
2 |
Correct |
8 ms |
46428 KB |
Output is correct |
3 |
Correct |
8 ms |
46564 KB |
Output is correct |
4 |
Correct |
9 ms |
46432 KB |
Output is correct |
5 |
Correct |
8 ms |
46684 KB |
Output is correct |
6 |
Correct |
8 ms |
46684 KB |
Output is correct |
7 |
Correct |
9 ms |
46684 KB |
Output is correct |
8 |
Correct |
8 ms |
46684 KB |
Output is correct |
9 |
Correct |
7 ms |
46428 KB |
Output is correct |
10 |
Correct |
9 ms |
46472 KB |
Output is correct |
11 |
Correct |
8 ms |
46684 KB |
Output is correct |
12 |
Correct |
8 ms |
46684 KB |
Output is correct |
13 |
Correct |
8 ms |
46684 KB |
Output is correct |
14 |
Correct |
8 ms |
46684 KB |
Output is correct |
15 |
Correct |
8 ms |
46684 KB |
Output is correct |
16 |
Correct |
8 ms |
46684 KB |
Output is correct |
17 |
Correct |
9 ms |
46848 KB |
Output is correct |
18 |
Correct |
9 ms |
46684 KB |
Output is correct |
19 |
Correct |
7 ms |
46736 KB |
Output is correct |
20 |
Correct |
7 ms |
46684 KB |
Output is correct |
21 |
Correct |
8 ms |
46644 KB |
Output is correct |
22 |
Correct |
8 ms |
46684 KB |
Output is correct |
23 |
Correct |
7 ms |
46464 KB |
Output is correct |
24 |
Correct |
8 ms |
46684 KB |
Output is correct |
25 |
Correct |
8 ms |
46684 KB |
Output is correct |
26 |
Correct |
8 ms |
46684 KB |
Output is correct |
27 |
Correct |
8 ms |
46472 KB |
Output is correct |
28 |
Correct |
8 ms |
46684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
46428 KB |
Output is correct |
2 |
Correct |
14 ms |
46428 KB |
Output is correct |
3 |
Correct |
8 ms |
46428 KB |
Output is correct |
4 |
Correct |
8 ms |
46564 KB |
Output is correct |
5 |
Correct |
9 ms |
46432 KB |
Output is correct |
6 |
Correct |
8 ms |
46684 KB |
Output is correct |
7 |
Correct |
8 ms |
46684 KB |
Output is correct |
8 |
Correct |
9 ms |
46684 KB |
Output is correct |
9 |
Correct |
8 ms |
46684 KB |
Output is correct |
10 |
Correct |
47 ms |
53316 KB |
Output is correct |
11 |
Correct |
57 ms |
54724 KB |
Output is correct |
12 |
Correct |
54 ms |
54648 KB |
Output is correct |
13 |
Correct |
58 ms |
55256 KB |
Output is correct |
14 |
Correct |
52 ms |
56628 KB |
Output is correct |
15 |
Correct |
9 ms |
46472 KB |
Output is correct |
16 |
Correct |
8 ms |
46684 KB |
Output is correct |
17 |
Correct |
8 ms |
46684 KB |
Output is correct |
18 |
Correct |
8 ms |
46684 KB |
Output is correct |
19 |
Correct |
8 ms |
46684 KB |
Output is correct |
20 |
Correct |
8 ms |
46684 KB |
Output is correct |
21 |
Correct |
8 ms |
46684 KB |
Output is correct |
22 |
Correct |
9 ms |
46848 KB |
Output is correct |
23 |
Correct |
9 ms |
46684 KB |
Output is correct |
24 |
Correct |
7 ms |
46736 KB |
Output is correct |
25 |
Correct |
7 ms |
46684 KB |
Output is correct |
26 |
Correct |
8 ms |
46644 KB |
Output is correct |
27 |
Correct |
8 ms |
46684 KB |
Output is correct |
28 |
Correct |
7 ms |
46464 KB |
Output is correct |
29 |
Correct |
8 ms |
46684 KB |
Output is correct |
30 |
Correct |
8 ms |
46684 KB |
Output is correct |
31 |
Correct |
8 ms |
46684 KB |
Output is correct |
32 |
Correct |
8 ms |
46472 KB |
Output is correct |
33 |
Correct |
8 ms |
46684 KB |
Output is correct |
34 |
Correct |
14 ms |
47644 KB |
Output is correct |
35 |
Correct |
58 ms |
55040 KB |
Output is correct |
36 |
Correct |
56 ms |
54984 KB |
Output is correct |
37 |
Correct |
57 ms |
55032 KB |
Output is correct |
38 |
Correct |
57 ms |
54984 KB |
Output is correct |
39 |
Correct |
59 ms |
54660 KB |
Output is correct |
40 |
Correct |
53 ms |
54084 KB |
Output is correct |
41 |
Correct |
57 ms |
55116 KB |
Output is correct |
42 |
Correct |
58 ms |
54980 KB |
Output is correct |
43 |
Correct |
52 ms |
56776 KB |
Output is correct |
44 |
Correct |
63 ms |
55240 KB |
Output is correct |
45 |
Correct |
84 ms |
62828 KB |
Output is correct |
46 |
Correct |
62 ms |
54984 KB |
Output is correct |
47 |
Correct |
58 ms |
54980 KB |
Output is correct |
48 |
Correct |
67 ms |
56772 KB |
Output is correct |
49 |
Correct |
85 ms |
64408 KB |
Output is correct |
50 |
Correct |
68 ms |
60460 KB |
Output is correct |
51 |
Correct |
75 ms |
59732 KB |
Output is correct |
52 |
Correct |
96 ms |
62988 KB |
Output is correct |
53 |
Correct |
110 ms |
63664 KB |
Output is correct |
54 |
Correct |
112 ms |
66052 KB |
Output is correct |
55 |
Correct |
55 ms |
56832 KB |
Output is correct |
56 |
Correct |
106 ms |
63936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
46428 KB |
Output is correct |
2 |
Correct |
8 ms |
46428 KB |
Output is correct |
3 |
Correct |
8 ms |
46564 KB |
Output is correct |
4 |
Correct |
9 ms |
46432 KB |
Output is correct |
5 |
Correct |
8 ms |
46684 KB |
Output is correct |
6 |
Correct |
8 ms |
46684 KB |
Output is correct |
7 |
Correct |
9 ms |
46684 KB |
Output is correct |
8 |
Correct |
8 ms |
46684 KB |
Output is correct |
9 |
Correct |
47 ms |
53316 KB |
Output is correct |
10 |
Correct |
57 ms |
54724 KB |
Output is correct |
11 |
Correct |
54 ms |
54648 KB |
Output is correct |
12 |
Correct |
58 ms |
55256 KB |
Output is correct |
13 |
Correct |
52 ms |
56628 KB |
Output is correct |
14 |
Correct |
51 ms |
53752 KB |
Output is correct |
15 |
Correct |
119 ms |
58228 KB |
Output is correct |
16 |
Correct |
100 ms |
58088 KB |
Output is correct |
17 |
Correct |
99 ms |
58664 KB |
Output is correct |
18 |
Correct |
96 ms |
60176 KB |
Output is correct |
19 |
Correct |
203 ms |
61768 KB |
Output is correct |
20 |
Correct |
213 ms |
63024 KB |
Output is correct |
21 |
Correct |
189 ms |
62808 KB |
Output is correct |
22 |
Correct |
183 ms |
62688 KB |
Output is correct |
23 |
Correct |
213 ms |
62900 KB |
Output is correct |
24 |
Correct |
175 ms |
62344 KB |
Output is correct |
25 |
Correct |
182 ms |
62712 KB |
Output is correct |
26 |
Correct |
192 ms |
62356 KB |
Output is correct |
27 |
Correct |
9 ms |
46472 KB |
Output is correct |
28 |
Correct |
8 ms |
46684 KB |
Output is correct |
29 |
Correct |
8 ms |
46684 KB |
Output is correct |
30 |
Correct |
8 ms |
46684 KB |
Output is correct |
31 |
Correct |
8 ms |
46684 KB |
Output is correct |
32 |
Correct |
8 ms |
46684 KB |
Output is correct |
33 |
Correct |
8 ms |
46684 KB |
Output is correct |
34 |
Correct |
9 ms |
46848 KB |
Output is correct |
35 |
Correct |
9 ms |
46684 KB |
Output is correct |
36 |
Correct |
14 ms |
47644 KB |
Output is correct |
37 |
Correct |
58 ms |
55040 KB |
Output is correct |
38 |
Correct |
56 ms |
54984 KB |
Output is correct |
39 |
Correct |
57 ms |
55032 KB |
Output is correct |
40 |
Correct |
57 ms |
54984 KB |
Output is correct |
41 |
Correct |
59 ms |
54660 KB |
Output is correct |
42 |
Correct |
53 ms |
54084 KB |
Output is correct |
43 |
Correct |
57 ms |
55116 KB |
Output is correct |
44 |
Correct |
58 ms |
54980 KB |
Output is correct |
45 |
Correct |
52 ms |
56776 KB |
Output is correct |
46 |
Correct |
63 ms |
55240 KB |
Output is correct |
47 |
Correct |
24 ms |
48048 KB |
Output is correct |
48 |
Correct |
177 ms |
59852 KB |
Output is correct |
49 |
Correct |
184 ms |
59944 KB |
Output is correct |
50 |
Correct |
157 ms |
59784 KB |
Output is correct |
51 |
Correct |
168 ms |
59808 KB |
Output is correct |
52 |
Correct |
151 ms |
59488 KB |
Output is correct |
53 |
Correct |
138 ms |
58300 KB |
Output is correct |
54 |
Correct |
160 ms |
60788 KB |
Output is correct |
55 |
Correct |
176 ms |
59788 KB |
Output is correct |
56 |
Correct |
243 ms |
44844 KB |
Output is correct |
57 |
Correct |
171 ms |
44024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
46428 KB |
Output is correct |
2 |
Correct |
14 ms |
46428 KB |
Output is correct |
3 |
Correct |
8 ms |
46428 KB |
Output is correct |
4 |
Correct |
8 ms |
46564 KB |
Output is correct |
5 |
Correct |
9 ms |
46432 KB |
Output is correct |
6 |
Correct |
8 ms |
46684 KB |
Output is correct |
7 |
Correct |
8 ms |
46684 KB |
Output is correct |
8 |
Correct |
9 ms |
46684 KB |
Output is correct |
9 |
Correct |
8 ms |
46684 KB |
Output is correct |
10 |
Correct |
47 ms |
53316 KB |
Output is correct |
11 |
Correct |
57 ms |
54724 KB |
Output is correct |
12 |
Correct |
54 ms |
54648 KB |
Output is correct |
13 |
Correct |
58 ms |
55256 KB |
Output is correct |
14 |
Correct |
52 ms |
56628 KB |
Output is correct |
15 |
Correct |
51 ms |
53752 KB |
Output is correct |
16 |
Correct |
119 ms |
58228 KB |
Output is correct |
17 |
Correct |
100 ms |
58088 KB |
Output is correct |
18 |
Correct |
99 ms |
58664 KB |
Output is correct |
19 |
Correct |
96 ms |
60176 KB |
Output is correct |
20 |
Correct |
203 ms |
61768 KB |
Output is correct |
21 |
Correct |
213 ms |
63024 KB |
Output is correct |
22 |
Correct |
189 ms |
62808 KB |
Output is correct |
23 |
Correct |
183 ms |
62688 KB |
Output is correct |
24 |
Correct |
213 ms |
62900 KB |
Output is correct |
25 |
Correct |
175 ms |
62344 KB |
Output is correct |
26 |
Correct |
182 ms |
62712 KB |
Output is correct |
27 |
Correct |
192 ms |
62356 KB |
Output is correct |
28 |
Correct |
9 ms |
46472 KB |
Output is correct |
29 |
Correct |
8 ms |
46684 KB |
Output is correct |
30 |
Correct |
8 ms |
46684 KB |
Output is correct |
31 |
Correct |
8 ms |
46684 KB |
Output is correct |
32 |
Correct |
8 ms |
46684 KB |
Output is correct |
33 |
Correct |
8 ms |
46684 KB |
Output is correct |
34 |
Correct |
8 ms |
46684 KB |
Output is correct |
35 |
Correct |
9 ms |
46848 KB |
Output is correct |
36 |
Correct |
9 ms |
46684 KB |
Output is correct |
37 |
Correct |
14 ms |
47644 KB |
Output is correct |
38 |
Correct |
58 ms |
55040 KB |
Output is correct |
39 |
Correct |
56 ms |
54984 KB |
Output is correct |
40 |
Correct |
57 ms |
55032 KB |
Output is correct |
41 |
Correct |
57 ms |
54984 KB |
Output is correct |
42 |
Correct |
59 ms |
54660 KB |
Output is correct |
43 |
Correct |
53 ms |
54084 KB |
Output is correct |
44 |
Correct |
57 ms |
55116 KB |
Output is correct |
45 |
Correct |
58 ms |
54980 KB |
Output is correct |
46 |
Correct |
52 ms |
56776 KB |
Output is correct |
47 |
Correct |
63 ms |
55240 KB |
Output is correct |
48 |
Correct |
24 ms |
48048 KB |
Output is correct |
49 |
Correct |
177 ms |
59852 KB |
Output is correct |
50 |
Correct |
184 ms |
59944 KB |
Output is correct |
51 |
Correct |
157 ms |
59784 KB |
Output is correct |
52 |
Correct |
168 ms |
59808 KB |
Output is correct |
53 |
Correct |
151 ms |
59488 KB |
Output is correct |
54 |
Correct |
138 ms |
58300 KB |
Output is correct |
55 |
Correct |
160 ms |
60788 KB |
Output is correct |
56 |
Correct |
176 ms |
59788 KB |
Output is correct |
57 |
Correct |
243 ms |
44844 KB |
Output is correct |
58 |
Correct |
171 ms |
44024 KB |
Output is correct |
59 |
Correct |
80 ms |
52756 KB |
Output is correct |
60 |
Correct |
208 ms |
59364 KB |
Output is correct |
61 |
Correct |
154 ms |
59520 KB |
Output is correct |
62 |
Correct |
169 ms |
60204 KB |
Output is correct |
63 |
Correct |
208 ms |
61744 KB |
Output is correct |
64 |
Correct |
7 ms |
46736 KB |
Output is correct |
65 |
Correct |
7 ms |
46684 KB |
Output is correct |
66 |
Correct |
8 ms |
46644 KB |
Output is correct |
67 |
Correct |
8 ms |
46684 KB |
Output is correct |
68 |
Correct |
7 ms |
46464 KB |
Output is correct |
69 |
Correct |
8 ms |
46684 KB |
Output is correct |
70 |
Correct |
8 ms |
46684 KB |
Output is correct |
71 |
Correct |
8 ms |
46684 KB |
Output is correct |
72 |
Correct |
8 ms |
46472 KB |
Output is correct |
73 |
Correct |
8 ms |
46684 KB |
Output is correct |
74 |
Correct |
84 ms |
62828 KB |
Output is correct |
75 |
Correct |
62 ms |
54984 KB |
Output is correct |
76 |
Correct |
58 ms |
54980 KB |
Output is correct |
77 |
Correct |
67 ms |
56772 KB |
Output is correct |
78 |
Correct |
85 ms |
64408 KB |
Output is correct |
79 |
Correct |
68 ms |
60460 KB |
Output is correct |
80 |
Correct |
75 ms |
59732 KB |
Output is correct |
81 |
Correct |
96 ms |
62988 KB |
Output is correct |
82 |
Correct |
110 ms |
63664 KB |
Output is correct |
83 |
Correct |
112 ms |
66052 KB |
Output is correct |
84 |
Correct |
55 ms |
56832 KB |
Output is correct |
85 |
Correct |
106 ms |
63936 KB |
Output is correct |
86 |
Correct |
73 ms |
25932 KB |
Output is correct |
87 |
Correct |
162 ms |
43048 KB |
Output is correct |
88 |
Correct |
161 ms |
58216 KB |
Output is correct |
89 |
Correct |
216 ms |
61412 KB |
Output is correct |
90 |
Correct |
159 ms |
67788 KB |
Output is correct |
91 |
Correct |
185 ms |
67292 KB |
Output is correct |
92 |
Correct |
202 ms |
64804 KB |
Output is correct |
93 |
Correct |
195 ms |
67164 KB |
Output is correct |
94 |
Correct |
250 ms |
66508 KB |
Output is correct |
95 |
Correct |
234 ms |
69136 KB |
Output is correct |
96 |
Correct |
256 ms |
61628 KB |
Output is correct |
97 |
Correct |
289 ms |
63108 KB |
Output is correct |