#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
const int maxN = 3e5 + 5;
int pre[maxN], sz[maxN], curr[maxN], val[maxN], par[20][maxN], depth[maxN], mx[20][maxN], deg[maxN], node;
bool take[maxN], good[maxN], is_true[20][maxN], is_there_any_answer = false;
vector<int> adj[maxN];
int find_set(int u) {
return (u == pre[u]) ? u : pre[u] = find_set(pre[u]);
}
void union_set(int u, int v, int w) {
int a = find_set(u), b = find_set(v);
++node;
if (a == b) {
adj[node].push_back(curr[a]);
curr[a] = node;
good[a] = true;
take[node] = true;
val[node] = w;
return ;
}
if (sz[a] < sz[b]) swap(a, b);
pre[b] = a;
sz[b] += sz[a];
adj[node].push_back(curr[a]);
adj[node].push_back(curr[b]);
curr[a] = node;
if (good[a] || good[b] || ++deg[u] >= 3 || ++deg[v] >= 3) good[a] = true;
take[node] = good[a];
val[node] = w;
}
void dfs(int u, int pre) {
is_there_any_answer |= take[u];
for (auto v: adj[u]) {
if (v == pre) continue;
par[0][v] = u;
mx[0][v] = val[u];
is_true[0][v] = take[u];
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
struct Ed {
int u, v, w;
bool operator < (const Ed& rhs) const {
return w < rhs.w;
}
} edge[maxN];
int get_lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for (int k = 18; k >= 0; k--) {
if (depth[par[k][u]] >= depth[v])
u = par[k][u];
}
if (u == v) return u;
for (int k = 18; k >= 0; k--) {
if (par[k][u] != par[k][v]) {
u = par[k][u];
v = par[k][v];
}
}
return par[0][u];
}
void init(int n, int m, vector<int> U, vector<int> V, vector<int> W) {
for (int i = 1; i <= n + m; i++) {
pre[i] = i;
sz[i] = 1;
if (i <= n) curr[i] = i;
}
for (int i = 0; i < m; i++) {
U[i]++; V[i]++;
edge[i + 1] = {U[i], V[i], W[i]};
}
sort(edge + 1, edge + m + 1);
node = n;
for (int i = 1; i <= m; i++) {
auto [u, v, w] = edge[i];
union_set(u, v, w);
}
depth[n + m] = 1;
dfs(n + m, n + m);
//for (int i = 1; i <= n + m; i++) {
//cout << "Debug " << val[i] << ' '<< take[i] << '\n';
//}
for (int k = 1; k <= 18; k++)
for (int i = 1; i <= n + m; i++) {
par[k][i] = par[k - 1][par[k - 1][i]];
mx[k][i] = max(mx[k - 1][i], mx[k - 1][par[k - 1][i]]);
is_true[k][i] = (is_true[k - 1][i] || is_true[k - 1][par[k - 1][i]]) ? true : false;
}
}
int getMinimumFuelCapacity(int x, int y) {
if (!is_there_any_answer) return -1;
x++; y++;
int lca = get_lca(x, y), ans = 0; bool valid = false;
for (int k = 18; k >= 0; k--)
if (depth[par[k][x]] >= depth[lca]) {
ans = max(ans, mx[k][x]);
valid |= is_true[k][x];
x = par[k][x];
}
for (int k = 18; k >= 0; k--)
if (depth[par[k][y]] >= depth[lca]) {
ans = max(ans, mx[k][y]);
valid |= is_true[k][y];
y = par[k][y];
}
if (valid) return ans;
for (int k = 18; k >= 0; k--)
if (!is_true[k][lca]) {
ans = max(ans, mx[k][lca]);
lca = par[k][lca];
}
ans = max(ans, mx[0][lca]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
4 ms |
7760 KB |
Output is correct |
4 |
Correct |
4 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8028 KB |
Output is correct |
7 |
Correct |
4 ms |
8312 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
62 ms |
43488 KB |
Output is correct |
10 |
Correct |
70 ms |
51508 KB |
Output is correct |
11 |
Correct |
75 ms |
50824 KB |
Output is correct |
12 |
Correct |
75 ms |
53332 KB |
Output is correct |
13 |
Correct |
73 ms |
57428 KB |
Output is correct |
14 |
Correct |
59 ms |
43868 KB |
Output is correct |
15 |
Correct |
110 ms |
55404 KB |
Output is correct |
16 |
Correct |
96 ms |
54140 KB |
Output is correct |
17 |
Correct |
102 ms |
56924 KB |
Output is correct |
18 |
Correct |
101 ms |
60972 KB |
Output is correct |
19 |
Correct |
100 ms |
19796 KB |
Output is correct |
20 |
Correct |
221 ms |
56776 KB |
Output is correct |
21 |
Correct |
223 ms |
53916 KB |
Output is correct |
22 |
Correct |
240 ms |
57016 KB |
Output is correct |
23 |
Correct |
365 ms |
60996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
307 ms |
61044 KB |
Output is correct |
4 |
Correct |
338 ms |
63928 KB |
Output is correct |
5 |
Correct |
282 ms |
62336 KB |
Output is correct |
6 |
Correct |
291 ms |
63420 KB |
Output is correct |
7 |
Correct |
298 ms |
63180 KB |
Output is correct |
8 |
Correct |
280 ms |
60676 KB |
Output is correct |
9 |
Correct |
274 ms |
62808 KB |
Output is correct |
10 |
Correct |
286 ms |
60540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
4 ms |
7760 KB |
Output is correct |
4 |
Correct |
4 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8028 KB |
Output is correct |
7 |
Correct |
4 ms |
8312 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
7772 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8116 KB |
Output is correct |
12 |
Correct |
4 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8028 KB |
Output is correct |
14 |
Correct |
5 ms |
8028 KB |
Output is correct |
15 |
Correct |
6 ms |
8312 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
5 ms |
8284 KB |
Output is correct |
19 |
Correct |
6 ms |
8052 KB |
Output is correct |
20 |
Correct |
4 ms |
8280 KB |
Output is correct |
21 |
Correct |
4 ms |
8284 KB |
Output is correct |
22 |
Correct |
4 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8024 KB |
Output is correct |
24 |
Correct |
4 ms |
8540 KB |
Output is correct |
25 |
Correct |
4 ms |
8536 KB |
Output is correct |
26 |
Correct |
4 ms |
8580 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
3 ms |
7772 KB |
Output is correct |
4 |
Correct |
4 ms |
7760 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8028 KB |
Output is correct |
8 |
Correct |
4 ms |
8312 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
62 ms |
43488 KB |
Output is correct |
11 |
Correct |
70 ms |
51508 KB |
Output is correct |
12 |
Correct |
75 ms |
50824 KB |
Output is correct |
13 |
Correct |
75 ms |
53332 KB |
Output is correct |
14 |
Correct |
73 ms |
57428 KB |
Output is correct |
15 |
Correct |
4 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8116 KB |
Output is correct |
17 |
Correct |
4 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8028 KB |
Output is correct |
19 |
Correct |
5 ms |
8028 KB |
Output is correct |
20 |
Correct |
6 ms |
8312 KB |
Output is correct |
21 |
Correct |
5 ms |
8284 KB |
Output is correct |
22 |
Correct |
3 ms |
8284 KB |
Output is correct |
23 |
Correct |
5 ms |
8284 KB |
Output is correct |
24 |
Correct |
6 ms |
8052 KB |
Output is correct |
25 |
Correct |
4 ms |
8280 KB |
Output is correct |
26 |
Correct |
4 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
8024 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8536 KB |
Output is correct |
31 |
Correct |
4 ms |
8580 KB |
Output is correct |
32 |
Correct |
4 ms |
8284 KB |
Output is correct |
33 |
Correct |
4 ms |
8488 KB |
Output is correct |
34 |
Correct |
11 ms |
13916 KB |
Output is correct |
35 |
Correct |
76 ms |
52624 KB |
Output is correct |
36 |
Correct |
72 ms |
52684 KB |
Output is correct |
37 |
Correct |
66 ms |
52808 KB |
Output is correct |
38 |
Correct |
66 ms |
52132 KB |
Output is correct |
39 |
Correct |
69 ms |
51716 KB |
Output is correct |
40 |
Correct |
65 ms |
48212 KB |
Output is correct |
41 |
Correct |
69 ms |
52784 KB |
Output is correct |
42 |
Correct |
72 ms |
52560 KB |
Output is correct |
43 |
Correct |
77 ms |
57940 KB |
Output is correct |
44 |
Correct |
78 ms |
52820 KB |
Output is correct |
45 |
Correct |
104 ms |
68316 KB |
Output is correct |
46 |
Correct |
84 ms |
52816 KB |
Output is correct |
47 |
Correct |
69 ms |
52944 KB |
Output is correct |
48 |
Correct |
80 ms |
57944 KB |
Output is correct |
49 |
Correct |
75 ms |
61268 KB |
Output is correct |
50 |
Correct |
63 ms |
49936 KB |
Output is correct |
51 |
Correct |
122 ms |
60052 KB |
Output is correct |
52 |
Correct |
128 ms |
73640 KB |
Output is correct |
53 |
Correct |
117 ms |
77648 KB |
Output is correct |
54 |
Correct |
123 ms |
86372 KB |
Output is correct |
55 |
Correct |
69 ms |
57428 KB |
Output is correct |
56 |
Correct |
116 ms |
79496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
4 ms |
7760 KB |
Output is correct |
4 |
Correct |
4 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8028 KB |
Output is correct |
7 |
Correct |
4 ms |
8312 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
62 ms |
43488 KB |
Output is correct |
10 |
Correct |
70 ms |
51508 KB |
Output is correct |
11 |
Correct |
75 ms |
50824 KB |
Output is correct |
12 |
Correct |
75 ms |
53332 KB |
Output is correct |
13 |
Correct |
73 ms |
57428 KB |
Output is correct |
14 |
Correct |
59 ms |
43868 KB |
Output is correct |
15 |
Correct |
110 ms |
55404 KB |
Output is correct |
16 |
Correct |
96 ms |
54140 KB |
Output is correct |
17 |
Correct |
102 ms |
56924 KB |
Output is correct |
18 |
Correct |
101 ms |
60972 KB |
Output is correct |
19 |
Correct |
307 ms |
61044 KB |
Output is correct |
20 |
Correct |
338 ms |
63928 KB |
Output is correct |
21 |
Correct |
282 ms |
62336 KB |
Output is correct |
22 |
Correct |
291 ms |
63420 KB |
Output is correct |
23 |
Correct |
298 ms |
63180 KB |
Output is correct |
24 |
Correct |
280 ms |
60676 KB |
Output is correct |
25 |
Correct |
274 ms |
62808 KB |
Output is correct |
26 |
Correct |
286 ms |
60540 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
3 ms |
8116 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8028 KB |
Output is correct |
31 |
Correct |
5 ms |
8028 KB |
Output is correct |
32 |
Correct |
6 ms |
8312 KB |
Output is correct |
33 |
Correct |
5 ms |
8284 KB |
Output is correct |
34 |
Correct |
3 ms |
8284 KB |
Output is correct |
35 |
Correct |
5 ms |
8284 KB |
Output is correct |
36 |
Correct |
11 ms |
13916 KB |
Output is correct |
37 |
Correct |
76 ms |
52624 KB |
Output is correct |
38 |
Correct |
72 ms |
52684 KB |
Output is correct |
39 |
Correct |
66 ms |
52808 KB |
Output is correct |
40 |
Correct |
66 ms |
52132 KB |
Output is correct |
41 |
Correct |
69 ms |
51716 KB |
Output is correct |
42 |
Correct |
65 ms |
48212 KB |
Output is correct |
43 |
Correct |
69 ms |
52784 KB |
Output is correct |
44 |
Correct |
72 ms |
52560 KB |
Output is correct |
45 |
Correct |
77 ms |
57940 KB |
Output is correct |
46 |
Correct |
78 ms |
52820 KB |
Output is correct |
47 |
Correct |
20 ms |
13852 KB |
Output is correct |
48 |
Correct |
242 ms |
56196 KB |
Output is correct |
49 |
Correct |
242 ms |
56252 KB |
Output is correct |
50 |
Correct |
194 ms |
56248 KB |
Output is correct |
51 |
Correct |
189 ms |
56120 KB |
Output is correct |
52 |
Correct |
193 ms |
53568 KB |
Output is correct |
53 |
Correct |
221 ms |
42312 KB |
Output is correct |
54 |
Correct |
217 ms |
57272 KB |
Output is correct |
55 |
Correct |
210 ms |
56324 KB |
Output is correct |
56 |
Correct |
326 ms |
60432 KB |
Output is correct |
57 |
Correct |
212 ms |
57436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7772 KB |
Output is correct |
2 |
Correct |
3 ms |
7772 KB |
Output is correct |
3 |
Correct |
3 ms |
7772 KB |
Output is correct |
4 |
Correct |
4 ms |
7760 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
4 ms |
8028 KB |
Output is correct |
8 |
Correct |
4 ms |
8312 KB |
Output is correct |
9 |
Correct |
4 ms |
8284 KB |
Output is correct |
10 |
Correct |
62 ms |
43488 KB |
Output is correct |
11 |
Correct |
70 ms |
51508 KB |
Output is correct |
12 |
Correct |
75 ms |
50824 KB |
Output is correct |
13 |
Correct |
75 ms |
53332 KB |
Output is correct |
14 |
Correct |
73 ms |
57428 KB |
Output is correct |
15 |
Correct |
59 ms |
43868 KB |
Output is correct |
16 |
Correct |
110 ms |
55404 KB |
Output is correct |
17 |
Correct |
96 ms |
54140 KB |
Output is correct |
18 |
Correct |
102 ms |
56924 KB |
Output is correct |
19 |
Correct |
101 ms |
60972 KB |
Output is correct |
20 |
Correct |
307 ms |
61044 KB |
Output is correct |
21 |
Correct |
338 ms |
63928 KB |
Output is correct |
22 |
Correct |
282 ms |
62336 KB |
Output is correct |
23 |
Correct |
291 ms |
63420 KB |
Output is correct |
24 |
Correct |
298 ms |
63180 KB |
Output is correct |
25 |
Correct |
280 ms |
60676 KB |
Output is correct |
26 |
Correct |
274 ms |
62808 KB |
Output is correct |
27 |
Correct |
286 ms |
60540 KB |
Output is correct |
28 |
Correct |
4 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8116 KB |
Output is correct |
30 |
Correct |
4 ms |
8284 KB |
Output is correct |
31 |
Correct |
3 ms |
8028 KB |
Output is correct |
32 |
Correct |
5 ms |
8028 KB |
Output is correct |
33 |
Correct |
6 ms |
8312 KB |
Output is correct |
34 |
Correct |
5 ms |
8284 KB |
Output is correct |
35 |
Correct |
3 ms |
8284 KB |
Output is correct |
36 |
Correct |
5 ms |
8284 KB |
Output is correct |
37 |
Correct |
11 ms |
13916 KB |
Output is correct |
38 |
Correct |
76 ms |
52624 KB |
Output is correct |
39 |
Correct |
72 ms |
52684 KB |
Output is correct |
40 |
Correct |
66 ms |
52808 KB |
Output is correct |
41 |
Correct |
66 ms |
52132 KB |
Output is correct |
42 |
Correct |
69 ms |
51716 KB |
Output is correct |
43 |
Correct |
65 ms |
48212 KB |
Output is correct |
44 |
Correct |
69 ms |
52784 KB |
Output is correct |
45 |
Correct |
72 ms |
52560 KB |
Output is correct |
46 |
Correct |
77 ms |
57940 KB |
Output is correct |
47 |
Correct |
78 ms |
52820 KB |
Output is correct |
48 |
Correct |
20 ms |
13852 KB |
Output is correct |
49 |
Correct |
242 ms |
56196 KB |
Output is correct |
50 |
Correct |
242 ms |
56252 KB |
Output is correct |
51 |
Correct |
194 ms |
56248 KB |
Output is correct |
52 |
Correct |
189 ms |
56120 KB |
Output is correct |
53 |
Correct |
193 ms |
53568 KB |
Output is correct |
54 |
Correct |
221 ms |
42312 KB |
Output is correct |
55 |
Correct |
217 ms |
57272 KB |
Output is correct |
56 |
Correct |
210 ms |
56324 KB |
Output is correct |
57 |
Correct |
326 ms |
60432 KB |
Output is correct |
58 |
Correct |
212 ms |
57436 KB |
Output is correct |
59 |
Correct |
100 ms |
19796 KB |
Output is correct |
60 |
Correct |
221 ms |
56776 KB |
Output is correct |
61 |
Correct |
223 ms |
53916 KB |
Output is correct |
62 |
Correct |
240 ms |
57016 KB |
Output is correct |
63 |
Correct |
365 ms |
60996 KB |
Output is correct |
64 |
Correct |
6 ms |
8052 KB |
Output is correct |
65 |
Correct |
4 ms |
8280 KB |
Output is correct |
66 |
Correct |
4 ms |
8284 KB |
Output is correct |
67 |
Correct |
4 ms |
8284 KB |
Output is correct |
68 |
Correct |
4 ms |
8024 KB |
Output is correct |
69 |
Correct |
4 ms |
8540 KB |
Output is correct |
70 |
Correct |
4 ms |
8536 KB |
Output is correct |
71 |
Correct |
4 ms |
8580 KB |
Output is correct |
72 |
Correct |
4 ms |
8284 KB |
Output is correct |
73 |
Correct |
4 ms |
8488 KB |
Output is correct |
74 |
Correct |
104 ms |
68316 KB |
Output is correct |
75 |
Correct |
84 ms |
52816 KB |
Output is correct |
76 |
Correct |
69 ms |
52944 KB |
Output is correct |
77 |
Correct |
80 ms |
57944 KB |
Output is correct |
78 |
Correct |
75 ms |
61268 KB |
Output is correct |
79 |
Correct |
63 ms |
49936 KB |
Output is correct |
80 |
Correct |
122 ms |
60052 KB |
Output is correct |
81 |
Correct |
128 ms |
73640 KB |
Output is correct |
82 |
Correct |
117 ms |
77648 KB |
Output is correct |
83 |
Correct |
123 ms |
86372 KB |
Output is correct |
84 |
Correct |
69 ms |
57428 KB |
Output is correct |
85 |
Correct |
116 ms |
79496 KB |
Output is correct |
86 |
Correct |
74 ms |
29780 KB |
Output is correct |
87 |
Correct |
186 ms |
56252 KB |
Output is correct |
88 |
Correct |
200 ms |
56460 KB |
Output is correct |
89 |
Correct |
282 ms |
59368 KB |
Output is correct |
90 |
Correct |
177 ms |
67424 KB |
Output is correct |
91 |
Correct |
223 ms |
63092 KB |
Output is correct |
92 |
Correct |
286 ms |
61904 KB |
Output is correct |
93 |
Correct |
268 ms |
76216 KB |
Output is correct |
94 |
Correct |
299 ms |
81192 KB |
Output is correct |
95 |
Correct |
250 ms |
88916 KB |
Output is correct |
96 |
Correct |
339 ms |
60816 KB |
Output is correct |
97 |
Correct |
380 ms |
70820 KB |
Output is correct |