#include "swap.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
const int N = 1e5 + 5;
int par[N][20], mx[N][20];
int tin[N], tout[N], t, is[N];
vector<ar<int, 2>> edges[N];
void dfs(int u, int p = -1){
for(int j=1;j<20;j++){
par[u][j] = par[par[u][j-1]][j-1];
mx[u][j] = max(mx[u][j-1], mx[par[u][j-1]][j-1]);
}
tin[u] = ++t;
for(auto x : edges[u]){
if(x[0] == p) continue;
par[x[0]][0] = u, mx[x[0]][0] = x[1];
dfs(x[0], u);
} tout[u] = t;
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
vector<int> p(m); iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
return (w[i] < w[j]);
});
vector<int> par(n), sz(n, 1), d(n);
vector<vector<int>> sub(n);
for(int i=0;i<n;i++){
par[i] = i;
sub[i].push_back(i);
}
auto upd = [&](int a, int c){
for(auto x : sub[a]) is[x] = c;
sub[a].clear();
};
function<int(int)> f = [&](int x) { return (par[x] == x ? x : par[x] = f(par[x])); };
auto merge = [&](int a, int b, int c){
d[a]++, d[b]++;
bool t = (d[a] >= 3 || d[b] >= 3);
a = f(a), b = f(b);
if(a == b){
upd(a, c);
return false;
}
if(sz[a] < sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b];
sub[a].insert(sub[a].end(), sub[b].begin(), sub[b].end());
if(is[a] && is[b]) return true;
if(!is[a] && !is[b]){
if(t) upd(a, c);
return true;
}
upd(a, c);
return true;
};
for(auto i : p){
if(merge(u[i], v[i], w[i])){
edges[u[i]].push_back({v[i], w[i]});
edges[v[i]].push_back({u[i], w[i]});
}
}
::par[0][0] = 0;
dfs(0, -1);
}
bool P(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int lca(int a, int b){
if(P(a, b)) return a;
if(P(b, a)) return b;
for(int j=19;~j;j--){
if(!P(par[a][j], b)) a = par[a][j];
} return par[a][0];
}
int getMinimumFuelCapacity(int a, int b) {
int mx = 0;
int p = lca(a, b);
//~ cout<<a<<" "<<b<<" "<<p<<"\n";
{
int x = a;
for(int j=19;~j;j--){
if(!P(par[x][j], p)){
mx = max(mx, ::mx[x][j]);
x = par[x][j];
}
} if(x != p) mx = max(mx, ::mx[x][0]);
}
{
int x = b;
for(int j=19;~j;j--){
if(!P(par[x][j], p)){
mx = max(mx, ::mx[x][j]);
x = par[x][j];
}
} if(x != p) mx = max(mx, ::mx[x][0]);
}
assert(mx);
if(!is[a]) return -1;
return max({mx, is[a], is[b]});
}
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
3 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
137 ms |
31740 KB |
Output is correct |
10 |
Correct |
194 ms |
39360 KB |
Output is correct |
11 |
Correct |
179 ms |
38168 KB |
Output is correct |
12 |
Correct |
212 ms |
40860 KB |
Output is correct |
13 |
Correct |
160 ms |
39796 KB |
Output is correct |
14 |
Correct |
175 ms |
31124 KB |
Output is correct |
15 |
Correct |
467 ms |
41076 KB |
Output is correct |
16 |
Correct |
450 ms |
38328 KB |
Output is correct |
17 |
Correct |
432 ms |
44068 KB |
Output is correct |
18 |
Correct |
403 ms |
39984 KB |
Output is correct |
19 |
Correct |
106 ms |
11956 KB |
Output is correct |
20 |
Correct |
522 ms |
40744 KB |
Output is correct |
21 |
Correct |
466 ms |
38432 KB |
Output is correct |
22 |
Correct |
517 ms |
42268 KB |
Output is correct |
23 |
Correct |
399 ms |
39936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
274 ms |
32708 KB |
Output is correct |
4 |
Correct |
258 ms |
38144 KB |
Output is correct |
5 |
Correct |
265 ms |
37160 KB |
Output is correct |
6 |
Correct |
254 ms |
37840 KB |
Output is correct |
7 |
Correct |
237 ms |
37632 KB |
Output is correct |
8 |
Correct |
252 ms |
36232 KB |
Output is correct |
9 |
Correct |
239 ms |
37392 KB |
Output is correct |
10 |
Correct |
248 ms |
35972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
3 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2924 KB |
Output is correct |
11 |
Correct |
2 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Correct |
3 ms |
2900 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
4 ms |
3028 KB |
Output is correct |
17 |
Correct |
2 ms |
3028 KB |
Output is correct |
18 |
Correct |
3 ms |
2932 KB |
Output is correct |
19 |
Correct |
3 ms |
2900 KB |
Output is correct |
20 |
Correct |
3 ms |
3028 KB |
Output is correct |
21 |
Correct |
3 ms |
2928 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2900 KB |
Output is correct |
24 |
Correct |
3 ms |
3028 KB |
Output is correct |
25 |
Correct |
3 ms |
3028 KB |
Output is correct |
26 |
Correct |
3 ms |
3028 KB |
Output is correct |
27 |
Correct |
2 ms |
2932 KB |
Output is correct |
28 |
Correct |
3 ms |
2928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
3028 KB |
Output is correct |
10 |
Correct |
137 ms |
31740 KB |
Output is correct |
11 |
Correct |
194 ms |
39360 KB |
Output is correct |
12 |
Correct |
179 ms |
38168 KB |
Output is correct |
13 |
Correct |
212 ms |
40860 KB |
Output is correct |
14 |
Correct |
160 ms |
39796 KB |
Output is correct |
15 |
Correct |
2 ms |
2924 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
4 ms |
3028 KB |
Output is correct |
22 |
Correct |
2 ms |
3028 KB |
Output is correct |
23 |
Correct |
3 ms |
2932 KB |
Output is correct |
24 |
Correct |
3 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
3028 KB |
Output is correct |
26 |
Correct |
3 ms |
2928 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
3 ms |
3028 KB |
Output is correct |
30 |
Correct |
3 ms |
3028 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
2932 KB |
Output is correct |
33 |
Correct |
3 ms |
2928 KB |
Output is correct |
34 |
Correct |
16 ms |
7208 KB |
Output is correct |
35 |
Correct |
190 ms |
41832 KB |
Output is correct |
36 |
Correct |
209 ms |
38720 KB |
Output is correct |
37 |
Correct |
171 ms |
36128 KB |
Output is correct |
38 |
Correct |
174 ms |
34772 KB |
Output is correct |
39 |
Correct |
187 ms |
33616 KB |
Output is correct |
40 |
Correct |
140 ms |
31084 KB |
Output is correct |
41 |
Correct |
192 ms |
40680 KB |
Output is correct |
42 |
Correct |
205 ms |
41844 KB |
Output is correct |
43 |
Correct |
169 ms |
39780 KB |
Output is correct |
44 |
Correct |
176 ms |
34808 KB |
Output is correct |
45 |
Correct |
161 ms |
31152 KB |
Output is correct |
46 |
Correct |
203 ms |
37748 KB |
Output is correct |
47 |
Correct |
173 ms |
34516 KB |
Output is correct |
48 |
Correct |
170 ms |
34000 KB |
Output is correct |
49 |
Correct |
71 ms |
10020 KB |
Output is correct |
50 |
Correct |
58 ms |
10212 KB |
Output is correct |
51 |
Correct |
132 ms |
26168 KB |
Output is correct |
52 |
Correct |
234 ms |
41128 KB |
Output is correct |
53 |
Correct |
197 ms |
39052 KB |
Output is correct |
54 |
Correct |
262 ms |
45320 KB |
Output is correct |
55 |
Correct |
182 ms |
41120 KB |
Output is correct |
56 |
Correct |
202 ms |
38088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
3 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
137 ms |
31740 KB |
Output is correct |
10 |
Correct |
194 ms |
39360 KB |
Output is correct |
11 |
Correct |
179 ms |
38168 KB |
Output is correct |
12 |
Correct |
212 ms |
40860 KB |
Output is correct |
13 |
Correct |
160 ms |
39796 KB |
Output is correct |
14 |
Correct |
175 ms |
31124 KB |
Output is correct |
15 |
Correct |
467 ms |
41076 KB |
Output is correct |
16 |
Correct |
450 ms |
38328 KB |
Output is correct |
17 |
Correct |
432 ms |
44068 KB |
Output is correct |
18 |
Correct |
403 ms |
39984 KB |
Output is correct |
19 |
Correct |
274 ms |
32708 KB |
Output is correct |
20 |
Correct |
258 ms |
38144 KB |
Output is correct |
21 |
Correct |
265 ms |
37160 KB |
Output is correct |
22 |
Correct |
254 ms |
37840 KB |
Output is correct |
23 |
Correct |
237 ms |
37632 KB |
Output is correct |
24 |
Correct |
252 ms |
36232 KB |
Output is correct |
25 |
Correct |
239 ms |
37392 KB |
Output is correct |
26 |
Correct |
248 ms |
35972 KB |
Output is correct |
27 |
Correct |
2 ms |
2924 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Correct |
3 ms |
2900 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
4 ms |
3028 KB |
Output is correct |
34 |
Correct |
2 ms |
3028 KB |
Output is correct |
35 |
Correct |
3 ms |
2932 KB |
Output is correct |
36 |
Correct |
16 ms |
7208 KB |
Output is correct |
37 |
Correct |
190 ms |
41832 KB |
Output is correct |
38 |
Correct |
209 ms |
38720 KB |
Output is correct |
39 |
Correct |
171 ms |
36128 KB |
Output is correct |
40 |
Correct |
174 ms |
34772 KB |
Output is correct |
41 |
Correct |
187 ms |
33616 KB |
Output is correct |
42 |
Correct |
140 ms |
31084 KB |
Output is correct |
43 |
Correct |
192 ms |
40680 KB |
Output is correct |
44 |
Correct |
205 ms |
41844 KB |
Output is correct |
45 |
Correct |
169 ms |
39780 KB |
Output is correct |
46 |
Correct |
176 ms |
34808 KB |
Output is correct |
47 |
Correct |
28 ms |
6996 KB |
Output is correct |
48 |
Correct |
468 ms |
44188 KB |
Output is correct |
49 |
Correct |
493 ms |
41720 KB |
Output is correct |
50 |
Correct |
454 ms |
40172 KB |
Output is correct |
51 |
Correct |
448 ms |
38976 KB |
Output is correct |
52 |
Correct |
404 ms |
36196 KB |
Output is correct |
53 |
Correct |
278 ms |
27692 KB |
Output is correct |
54 |
Correct |
423 ms |
43600 KB |
Output is correct |
55 |
Correct |
513 ms |
45008 KB |
Output is correct |
56 |
Correct |
453 ms |
44300 KB |
Output is correct |
57 |
Correct |
355 ms |
38628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
3 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
3028 KB |
Output is correct |
10 |
Correct |
137 ms |
31740 KB |
Output is correct |
11 |
Correct |
194 ms |
39360 KB |
Output is correct |
12 |
Correct |
179 ms |
38168 KB |
Output is correct |
13 |
Correct |
212 ms |
40860 KB |
Output is correct |
14 |
Correct |
160 ms |
39796 KB |
Output is correct |
15 |
Correct |
175 ms |
31124 KB |
Output is correct |
16 |
Correct |
467 ms |
41076 KB |
Output is correct |
17 |
Correct |
450 ms |
38328 KB |
Output is correct |
18 |
Correct |
432 ms |
44068 KB |
Output is correct |
19 |
Correct |
403 ms |
39984 KB |
Output is correct |
20 |
Correct |
274 ms |
32708 KB |
Output is correct |
21 |
Correct |
258 ms |
38144 KB |
Output is correct |
22 |
Correct |
265 ms |
37160 KB |
Output is correct |
23 |
Correct |
254 ms |
37840 KB |
Output is correct |
24 |
Correct |
237 ms |
37632 KB |
Output is correct |
25 |
Correct |
252 ms |
36232 KB |
Output is correct |
26 |
Correct |
239 ms |
37392 KB |
Output is correct |
27 |
Correct |
248 ms |
35972 KB |
Output is correct |
28 |
Correct |
2 ms |
2924 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
2900 KB |
Output is correct |
33 |
Correct |
2 ms |
2900 KB |
Output is correct |
34 |
Correct |
4 ms |
3028 KB |
Output is correct |
35 |
Correct |
2 ms |
3028 KB |
Output is correct |
36 |
Correct |
3 ms |
2932 KB |
Output is correct |
37 |
Correct |
16 ms |
7208 KB |
Output is correct |
38 |
Correct |
190 ms |
41832 KB |
Output is correct |
39 |
Correct |
209 ms |
38720 KB |
Output is correct |
40 |
Correct |
171 ms |
36128 KB |
Output is correct |
41 |
Correct |
174 ms |
34772 KB |
Output is correct |
42 |
Correct |
187 ms |
33616 KB |
Output is correct |
43 |
Correct |
140 ms |
31084 KB |
Output is correct |
44 |
Correct |
192 ms |
40680 KB |
Output is correct |
45 |
Correct |
205 ms |
41844 KB |
Output is correct |
46 |
Correct |
169 ms |
39780 KB |
Output is correct |
47 |
Correct |
176 ms |
34808 KB |
Output is correct |
48 |
Correct |
28 ms |
6996 KB |
Output is correct |
49 |
Correct |
468 ms |
44188 KB |
Output is correct |
50 |
Correct |
493 ms |
41720 KB |
Output is correct |
51 |
Correct |
454 ms |
40172 KB |
Output is correct |
52 |
Correct |
448 ms |
38976 KB |
Output is correct |
53 |
Correct |
404 ms |
36196 KB |
Output is correct |
54 |
Correct |
278 ms |
27692 KB |
Output is correct |
55 |
Correct |
423 ms |
43600 KB |
Output is correct |
56 |
Correct |
513 ms |
45008 KB |
Output is correct |
57 |
Correct |
453 ms |
44300 KB |
Output is correct |
58 |
Correct |
355 ms |
38628 KB |
Output is correct |
59 |
Correct |
106 ms |
11956 KB |
Output is correct |
60 |
Correct |
522 ms |
40744 KB |
Output is correct |
61 |
Correct |
466 ms |
38432 KB |
Output is correct |
62 |
Correct |
517 ms |
42268 KB |
Output is correct |
63 |
Correct |
399 ms |
39936 KB |
Output is correct |
64 |
Correct |
3 ms |
2900 KB |
Output is correct |
65 |
Correct |
3 ms |
3028 KB |
Output is correct |
66 |
Correct |
3 ms |
2928 KB |
Output is correct |
67 |
Correct |
3 ms |
2772 KB |
Output is correct |
68 |
Correct |
2 ms |
2900 KB |
Output is correct |
69 |
Correct |
3 ms |
3028 KB |
Output is correct |
70 |
Correct |
3 ms |
3028 KB |
Output is correct |
71 |
Correct |
3 ms |
3028 KB |
Output is correct |
72 |
Correct |
2 ms |
2932 KB |
Output is correct |
73 |
Correct |
3 ms |
2928 KB |
Output is correct |
74 |
Correct |
161 ms |
31152 KB |
Output is correct |
75 |
Correct |
203 ms |
37748 KB |
Output is correct |
76 |
Correct |
173 ms |
34516 KB |
Output is correct |
77 |
Correct |
170 ms |
34000 KB |
Output is correct |
78 |
Correct |
71 ms |
10020 KB |
Output is correct |
79 |
Correct |
58 ms |
10212 KB |
Output is correct |
80 |
Correct |
132 ms |
26168 KB |
Output is correct |
81 |
Correct |
234 ms |
41128 KB |
Output is correct |
82 |
Correct |
197 ms |
39052 KB |
Output is correct |
83 |
Correct |
262 ms |
45320 KB |
Output is correct |
84 |
Correct |
182 ms |
41120 KB |
Output is correct |
85 |
Correct |
202 ms |
38088 KB |
Output is correct |
86 |
Correct |
88 ms |
14504 KB |
Output is correct |
87 |
Correct |
457 ms |
41244 KB |
Output is correct |
88 |
Correct |
491 ms |
41104 KB |
Output is correct |
89 |
Correct |
307 ms |
35916 KB |
Output is correct |
90 |
Correct |
190 ms |
13940 KB |
Output is correct |
91 |
Correct |
189 ms |
16268 KB |
Output is correct |
92 |
Correct |
282 ms |
29844 KB |
Output is correct |
93 |
Correct |
491 ms |
45004 KB |
Output is correct |
94 |
Correct |
396 ms |
42824 KB |
Output is correct |
95 |
Correct |
516 ms |
49088 KB |
Output is correct |
96 |
Correct |
428 ms |
42368 KB |
Output is correct |
97 |
Correct |
346 ms |
40268 KB |
Output is correct |