#include "swap.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<int, int> pii;
const int N = 3e5 + 10;
const int Log = 20;
vector<pair<int, pii>> E;
int sp[Log][N], dep[N], wp[N];
int par[N], gd[N], la;
int deg[N], n, m;
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
void Unite(int u, int v, int w){
u = Find(u);
v = Find(v);
if(u == v){
la ++;
gd[la] = 1;
par[u] = la;
wp[u] = w;
sp[0][u] = la;
return ;
}
la ++;
par[u] = la;
par[v] = la;
wp[u] = w;
wp[v] = w;
sp[0][u] = la;
sp[0][v] = la;
gd[la] = gd[v] | gd[u];
}
void init(int n2, int m2, vector<int> U, vector<int> V, vector<int> W) {
n = n2;
m = m2;
for(int i = 0; i < m; i++)
E.pb({W[i], {U[i], V[i]}});
la = n - 1;
sort(all(E));
memset(gd, 0, sizeof gd);
memset(deg, 0, sizeof deg);
iota(par, par + N, 0);
memset(sp, 0, sizeof sp);
memset(wp, 0, sizeof wp);
memset(dep, 0, sizeof dep);
int u, v, w;
for(auto ob : E){
u = ob.S.F;
v = ob.S.S;
w = ob.F;
Unite(u, v, w);
deg[u] ++;
deg[v] ++;
if(max(deg[u], deg[v]) >= 3){
gd[Find(u)] = 1;
}
}
sp[0][la] = la;
for(int l = 1; l < Log; l++)
for(int i = 0; i <= la; i++)
sp[l][i] = sp[l - 1][sp[l - 1][i]];
dep[la] = 0;
for(int i = la - 1; i >= 0; i--)
dep[i] = dep[sp[0][i]] + 1;
}
int getMinimumFuelCapacity(int X, int Y){
if(dep[X] < dep[Y]) swap(X, Y);
//cerr << "! " << dep[X] << ' ' << dep[Y] << '\n';
int h = dep[X] - dep[Y];
for(int l = 0; l < Log; l++)
if(h >> l & 1)
X = sp[l][X];
for(int l = Log - 1; l >= 0; l--){
if((sp[l][X] != sp[l][Y]) || (gd[sp[l][X]] == 0)){
X = sp[l][X];
Y = sp[l][Y];
}
}
//cerr << "# " << X << ' ' << Y << '\n';
int res = max(wp[X], wp[Y]);
X = sp[0][X];
Y = sp[0][Y];
if((X != Y) || (gd[X] == 0))
return -1;
return res;
}
/*
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 |
18 ms |
29696 KB |
Output is correct |
2 |
Correct |
16 ms |
29696 KB |
Output is correct |
3 |
Correct |
17 ms |
29696 KB |
Output is correct |
4 |
Correct |
16 ms |
29696 KB |
Output is correct |
5 |
Correct |
17 ms |
29696 KB |
Output is correct |
6 |
Correct |
18 ms |
29688 KB |
Output is correct |
7 |
Correct |
18 ms |
29696 KB |
Output is correct |
8 |
Correct |
19 ms |
29696 KB |
Output is correct |
9 |
Correct |
71 ms |
32624 KB |
Output is correct |
10 |
Correct |
86 ms |
33132 KB |
Output is correct |
11 |
Correct |
87 ms |
33168 KB |
Output is correct |
12 |
Correct |
93 ms |
33388 KB |
Output is correct |
13 |
Correct |
100 ms |
34152 KB |
Output is correct |
14 |
Correct |
84 ms |
32748 KB |
Output is correct |
15 |
Correct |
197 ms |
35064 KB |
Output is correct |
16 |
Correct |
200 ms |
34972 KB |
Output is correct |
17 |
Correct |
205 ms |
35156 KB |
Output is correct |
18 |
Correct |
282 ms |
35924 KB |
Output is correct |
19 |
Correct |
118 ms |
33660 KB |
Output is correct |
20 |
Correct |
286 ms |
36016 KB |
Output is correct |
21 |
Correct |
286 ms |
36392 KB |
Output is correct |
22 |
Correct |
292 ms |
36564 KB |
Output is correct |
23 |
Correct |
395 ms |
37328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
29696 KB |
Output is correct |
2 |
Correct |
16 ms |
29696 KB |
Output is correct |
3 |
Correct |
374 ms |
36080 KB |
Output is correct |
4 |
Correct |
371 ms |
35984 KB |
Output is correct |
5 |
Correct |
395 ms |
36612 KB |
Output is correct |
6 |
Correct |
369 ms |
36052 KB |
Output is correct |
7 |
Correct |
381 ms |
36220 KB |
Output is correct |
8 |
Correct |
382 ms |
36052 KB |
Output is correct |
9 |
Correct |
376 ms |
36072 KB |
Output is correct |
10 |
Correct |
372 ms |
36140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
29696 KB |
Output is correct |
2 |
Correct |
18 ms |
29696 KB |
Output is correct |
3 |
Correct |
16 ms |
29696 KB |
Output is correct |
4 |
Correct |
17 ms |
29696 KB |
Output is correct |
5 |
Correct |
16 ms |
29696 KB |
Output is correct |
6 |
Correct |
17 ms |
29696 KB |
Output is correct |
7 |
Correct |
18 ms |
29688 KB |
Output is correct |
8 |
Correct |
18 ms |
29696 KB |
Output is correct |
9 |
Correct |
19 ms |
29696 KB |
Output is correct |
10 |
Correct |
18 ms |
29696 KB |
Output is correct |
11 |
Correct |
17 ms |
29696 KB |
Output is correct |
12 |
Correct |
17 ms |
29696 KB |
Output is correct |
13 |
Correct |
18 ms |
29696 KB |
Output is correct |
14 |
Correct |
16 ms |
29696 KB |
Output is correct |
15 |
Correct |
17 ms |
29696 KB |
Output is correct |
16 |
Correct |
18 ms |
29696 KB |
Output is correct |
17 |
Correct |
17 ms |
29696 KB |
Output is correct |
18 |
Correct |
17 ms |
29696 KB |
Output is correct |
19 |
Correct |
19 ms |
29688 KB |
Output is correct |
20 |
Correct |
17 ms |
29696 KB |
Output is correct |
21 |
Correct |
17 ms |
29696 KB |
Output is correct |
22 |
Correct |
17 ms |
29696 KB |
Output is correct |
23 |
Correct |
16 ms |
29760 KB |
Output is correct |
24 |
Correct |
18 ms |
29696 KB |
Output is correct |
25 |
Correct |
17 ms |
29696 KB |
Output is correct |
26 |
Correct |
21 ms |
29696 KB |
Output is correct |
27 |
Correct |
17 ms |
29696 KB |
Output is correct |
28 |
Correct |
17 ms |
29696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
29696 KB |
Output is correct |
2 |
Correct |
18 ms |
29696 KB |
Output is correct |
3 |
Correct |
16 ms |
29696 KB |
Output is correct |
4 |
Correct |
17 ms |
29696 KB |
Output is correct |
5 |
Correct |
16 ms |
29696 KB |
Output is correct |
6 |
Correct |
17 ms |
29696 KB |
Output is correct |
7 |
Correct |
18 ms |
29688 KB |
Output is correct |
8 |
Correct |
18 ms |
29696 KB |
Output is correct |
9 |
Correct |
19 ms |
29696 KB |
Output is correct |
10 |
Correct |
71 ms |
32624 KB |
Output is correct |
11 |
Correct |
86 ms |
33132 KB |
Output is correct |
12 |
Correct |
87 ms |
33168 KB |
Output is correct |
13 |
Correct |
93 ms |
33388 KB |
Output is correct |
14 |
Correct |
100 ms |
34152 KB |
Output is correct |
15 |
Correct |
18 ms |
29696 KB |
Output is correct |
16 |
Correct |
17 ms |
29696 KB |
Output is correct |
17 |
Correct |
17 ms |
29696 KB |
Output is correct |
18 |
Correct |
18 ms |
29696 KB |
Output is correct |
19 |
Correct |
16 ms |
29696 KB |
Output is correct |
20 |
Correct |
17 ms |
29696 KB |
Output is correct |
21 |
Correct |
18 ms |
29696 KB |
Output is correct |
22 |
Correct |
17 ms |
29696 KB |
Output is correct |
23 |
Correct |
17 ms |
29696 KB |
Output is correct |
24 |
Correct |
26 ms |
30392 KB |
Output is correct |
25 |
Correct |
82 ms |
33260 KB |
Output is correct |
26 |
Correct |
85 ms |
33404 KB |
Output is correct |
27 |
Correct |
85 ms |
33260 KB |
Output is correct |
28 |
Correct |
82 ms |
33260 KB |
Output is correct |
29 |
Correct |
81 ms |
33260 KB |
Output is correct |
30 |
Correct |
77 ms |
33004 KB |
Output is correct |
31 |
Correct |
87 ms |
33388 KB |
Output is correct |
32 |
Correct |
82 ms |
33388 KB |
Output is correct |
33 |
Correct |
80 ms |
33260 KB |
Output is correct |
34 |
Correct |
91 ms |
33388 KB |
Output is correct |
35 |
Correct |
19 ms |
29688 KB |
Output is correct |
36 |
Correct |
17 ms |
29696 KB |
Output is correct |
37 |
Correct |
17 ms |
29696 KB |
Output is correct |
38 |
Correct |
17 ms |
29696 KB |
Output is correct |
39 |
Correct |
16 ms |
29760 KB |
Output is correct |
40 |
Correct |
18 ms |
29696 KB |
Output is correct |
41 |
Correct |
17 ms |
29696 KB |
Output is correct |
42 |
Correct |
21 ms |
29696 KB |
Output is correct |
43 |
Correct |
17 ms |
29696 KB |
Output is correct |
44 |
Correct |
17 ms |
29696 KB |
Output is correct |
45 |
Correct |
138 ms |
34920 KB |
Output is correct |
46 |
Correct |
83 ms |
33264 KB |
Output is correct |
47 |
Correct |
83 ms |
33388 KB |
Output is correct |
48 |
Correct |
96 ms |
33772 KB |
Output is correct |
49 |
Correct |
123 ms |
35432 KB |
Output is correct |
50 |
Correct |
105 ms |
34156 KB |
Output is correct |
51 |
Correct |
118 ms |
34388 KB |
Output is correct |
52 |
Correct |
140 ms |
35552 KB |
Output is correct |
53 |
Correct |
152 ms |
36072 KB |
Output is correct |
54 |
Correct |
173 ms |
36964 KB |
Output is correct |
55 |
Correct |
80 ms |
33388 KB |
Output is correct |
56 |
Correct |
156 ms |
35940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
29696 KB |
Output is correct |
2 |
Correct |
16 ms |
29696 KB |
Output is correct |
3 |
Correct |
17 ms |
29696 KB |
Output is correct |
4 |
Correct |
16 ms |
29696 KB |
Output is correct |
5 |
Correct |
17 ms |
29696 KB |
Output is correct |
6 |
Correct |
18 ms |
29688 KB |
Output is correct |
7 |
Correct |
18 ms |
29696 KB |
Output is correct |
8 |
Correct |
19 ms |
29696 KB |
Output is correct |
9 |
Correct |
71 ms |
32624 KB |
Output is correct |
10 |
Correct |
86 ms |
33132 KB |
Output is correct |
11 |
Correct |
87 ms |
33168 KB |
Output is correct |
12 |
Correct |
93 ms |
33388 KB |
Output is correct |
13 |
Correct |
100 ms |
34152 KB |
Output is correct |
14 |
Correct |
84 ms |
32748 KB |
Output is correct |
15 |
Correct |
197 ms |
35064 KB |
Output is correct |
16 |
Correct |
200 ms |
34972 KB |
Output is correct |
17 |
Correct |
205 ms |
35156 KB |
Output is correct |
18 |
Correct |
282 ms |
35924 KB |
Output is correct |
19 |
Correct |
374 ms |
36080 KB |
Output is correct |
20 |
Correct |
371 ms |
35984 KB |
Output is correct |
21 |
Correct |
395 ms |
36612 KB |
Output is correct |
22 |
Correct |
369 ms |
36052 KB |
Output is correct |
23 |
Correct |
381 ms |
36220 KB |
Output is correct |
24 |
Correct |
382 ms |
36052 KB |
Output is correct |
25 |
Correct |
376 ms |
36072 KB |
Output is correct |
26 |
Correct |
372 ms |
36140 KB |
Output is correct |
27 |
Correct |
18 ms |
29696 KB |
Output is correct |
28 |
Correct |
17 ms |
29696 KB |
Output is correct |
29 |
Correct |
17 ms |
29696 KB |
Output is correct |
30 |
Correct |
18 ms |
29696 KB |
Output is correct |
31 |
Correct |
16 ms |
29696 KB |
Output is correct |
32 |
Correct |
17 ms |
29696 KB |
Output is correct |
33 |
Correct |
18 ms |
29696 KB |
Output is correct |
34 |
Correct |
17 ms |
29696 KB |
Output is correct |
35 |
Correct |
17 ms |
29696 KB |
Output is correct |
36 |
Correct |
26 ms |
30392 KB |
Output is correct |
37 |
Correct |
82 ms |
33260 KB |
Output is correct |
38 |
Correct |
85 ms |
33404 KB |
Output is correct |
39 |
Correct |
85 ms |
33260 KB |
Output is correct |
40 |
Correct |
82 ms |
33260 KB |
Output is correct |
41 |
Correct |
81 ms |
33260 KB |
Output is correct |
42 |
Correct |
77 ms |
33004 KB |
Output is correct |
43 |
Correct |
87 ms |
33388 KB |
Output is correct |
44 |
Correct |
82 ms |
33388 KB |
Output is correct |
45 |
Correct |
80 ms |
33260 KB |
Output is correct |
46 |
Correct |
91 ms |
33388 KB |
Output is correct |
47 |
Correct |
37 ms |
30504 KB |
Output is correct |
48 |
Correct |
287 ms |
36176 KB |
Output is correct |
49 |
Correct |
291 ms |
35924 KB |
Output is correct |
50 |
Correct |
294 ms |
36052 KB |
Output is correct |
51 |
Correct |
290 ms |
35932 KB |
Output is correct |
52 |
Correct |
275 ms |
35736 KB |
Output is correct |
53 |
Correct |
251 ms |
35808 KB |
Output is correct |
54 |
Correct |
292 ms |
36692 KB |
Output is correct |
55 |
Correct |
291 ms |
36052 KB |
Output is correct |
56 |
Correct |
402 ms |
36056 KB |
Output is correct |
57 |
Correct |
310 ms |
36564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
29696 KB |
Output is correct |
2 |
Correct |
18 ms |
29696 KB |
Output is correct |
3 |
Correct |
16 ms |
29696 KB |
Output is correct |
4 |
Correct |
17 ms |
29696 KB |
Output is correct |
5 |
Correct |
16 ms |
29696 KB |
Output is correct |
6 |
Correct |
17 ms |
29696 KB |
Output is correct |
7 |
Correct |
18 ms |
29688 KB |
Output is correct |
8 |
Correct |
18 ms |
29696 KB |
Output is correct |
9 |
Correct |
19 ms |
29696 KB |
Output is correct |
10 |
Correct |
71 ms |
32624 KB |
Output is correct |
11 |
Correct |
86 ms |
33132 KB |
Output is correct |
12 |
Correct |
87 ms |
33168 KB |
Output is correct |
13 |
Correct |
93 ms |
33388 KB |
Output is correct |
14 |
Correct |
100 ms |
34152 KB |
Output is correct |
15 |
Correct |
84 ms |
32748 KB |
Output is correct |
16 |
Correct |
197 ms |
35064 KB |
Output is correct |
17 |
Correct |
200 ms |
34972 KB |
Output is correct |
18 |
Correct |
205 ms |
35156 KB |
Output is correct |
19 |
Correct |
282 ms |
35924 KB |
Output is correct |
20 |
Correct |
374 ms |
36080 KB |
Output is correct |
21 |
Correct |
371 ms |
35984 KB |
Output is correct |
22 |
Correct |
395 ms |
36612 KB |
Output is correct |
23 |
Correct |
369 ms |
36052 KB |
Output is correct |
24 |
Correct |
381 ms |
36220 KB |
Output is correct |
25 |
Correct |
382 ms |
36052 KB |
Output is correct |
26 |
Correct |
376 ms |
36072 KB |
Output is correct |
27 |
Correct |
372 ms |
36140 KB |
Output is correct |
28 |
Correct |
18 ms |
29696 KB |
Output is correct |
29 |
Correct |
17 ms |
29696 KB |
Output is correct |
30 |
Correct |
17 ms |
29696 KB |
Output is correct |
31 |
Correct |
18 ms |
29696 KB |
Output is correct |
32 |
Correct |
16 ms |
29696 KB |
Output is correct |
33 |
Correct |
17 ms |
29696 KB |
Output is correct |
34 |
Correct |
18 ms |
29696 KB |
Output is correct |
35 |
Correct |
17 ms |
29696 KB |
Output is correct |
36 |
Correct |
17 ms |
29696 KB |
Output is correct |
37 |
Correct |
26 ms |
30392 KB |
Output is correct |
38 |
Correct |
82 ms |
33260 KB |
Output is correct |
39 |
Correct |
85 ms |
33404 KB |
Output is correct |
40 |
Correct |
85 ms |
33260 KB |
Output is correct |
41 |
Correct |
82 ms |
33260 KB |
Output is correct |
42 |
Correct |
81 ms |
33260 KB |
Output is correct |
43 |
Correct |
77 ms |
33004 KB |
Output is correct |
44 |
Correct |
87 ms |
33388 KB |
Output is correct |
45 |
Correct |
82 ms |
33388 KB |
Output is correct |
46 |
Correct |
80 ms |
33260 KB |
Output is correct |
47 |
Correct |
91 ms |
33388 KB |
Output is correct |
48 |
Correct |
37 ms |
30504 KB |
Output is correct |
49 |
Correct |
287 ms |
36176 KB |
Output is correct |
50 |
Correct |
291 ms |
35924 KB |
Output is correct |
51 |
Correct |
294 ms |
36052 KB |
Output is correct |
52 |
Correct |
290 ms |
35932 KB |
Output is correct |
53 |
Correct |
275 ms |
35736 KB |
Output is correct |
54 |
Correct |
251 ms |
35808 KB |
Output is correct |
55 |
Correct |
292 ms |
36692 KB |
Output is correct |
56 |
Correct |
291 ms |
36052 KB |
Output is correct |
57 |
Correct |
402 ms |
36056 KB |
Output is correct |
58 |
Correct |
310 ms |
36564 KB |
Output is correct |
59 |
Correct |
118 ms |
33660 KB |
Output is correct |
60 |
Correct |
286 ms |
36016 KB |
Output is correct |
61 |
Correct |
286 ms |
36392 KB |
Output is correct |
62 |
Correct |
292 ms |
36564 KB |
Output is correct |
63 |
Correct |
395 ms |
37328 KB |
Output is correct |
64 |
Correct |
19 ms |
29688 KB |
Output is correct |
65 |
Correct |
17 ms |
29696 KB |
Output is correct |
66 |
Correct |
17 ms |
29696 KB |
Output is correct |
67 |
Correct |
17 ms |
29696 KB |
Output is correct |
68 |
Correct |
16 ms |
29760 KB |
Output is correct |
69 |
Correct |
18 ms |
29696 KB |
Output is correct |
70 |
Correct |
17 ms |
29696 KB |
Output is correct |
71 |
Correct |
21 ms |
29696 KB |
Output is correct |
72 |
Correct |
17 ms |
29696 KB |
Output is correct |
73 |
Correct |
17 ms |
29696 KB |
Output is correct |
74 |
Correct |
138 ms |
34920 KB |
Output is correct |
75 |
Correct |
83 ms |
33264 KB |
Output is correct |
76 |
Correct |
83 ms |
33388 KB |
Output is correct |
77 |
Correct |
96 ms |
33772 KB |
Output is correct |
78 |
Correct |
123 ms |
35432 KB |
Output is correct |
79 |
Correct |
105 ms |
34156 KB |
Output is correct |
80 |
Correct |
118 ms |
34388 KB |
Output is correct |
81 |
Correct |
140 ms |
35552 KB |
Output is correct |
82 |
Correct |
152 ms |
36072 KB |
Output is correct |
83 |
Correct |
173 ms |
36964 KB |
Output is correct |
84 |
Correct |
80 ms |
33388 KB |
Output is correct |
85 |
Correct |
156 ms |
35940 KB |
Output is correct |
86 |
Correct |
101 ms |
31968 KB |
Output is correct |
87 |
Correct |
281 ms |
36052 KB |
Output is correct |
88 |
Correct |
283 ms |
35924 KB |
Output is correct |
89 |
Correct |
367 ms |
36108 KB |
Output is correct |
90 |
Correct |
238 ms |
37692 KB |
Output is correct |
91 |
Correct |
264 ms |
37516 KB |
Output is correct |
92 |
Correct |
372 ms |
37068 KB |
Output is correct |
93 |
Correct |
353 ms |
37916 KB |
Output is correct |
94 |
Correct |
423 ms |
38340 KB |
Output is correct |
95 |
Correct |
381 ms |
38376 KB |
Output is correct |
96 |
Correct |
425 ms |
36044 KB |
Output is correct |
97 |
Correct |
402 ms |
37284 KB |
Output is correct |