#include <bits/stdc++.h>
#ifndef LOCAL
#include "swap.h"
#endif
using namespace std;
// ******************** Constants ********************
const int MXN = 100000;
const int MXM = 200000;
const int MXS = 300000;
const int lg = 20;
// ******************** global variables ********************
vector<int> g[MXS];
int nxtnode = 0;
// ******************** LCA ********************
int up[lg][MXS], tin[MXS], tout[MXS], timer = 0;
void dfs(int node, int par){
tin[node] = timer++;
up[0][node] = par;
for(int i = 1; i < lg; i++)
up[i][node] = up[i-1][up[i-1][node]];
for(int v : g[node]){
if(v == par)
continue;
dfs(v, node);
}
tout[node] = timer;
}
inline bool isAncestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if(isAncestor(u, v))
return u;
if(isAncestor(v, u))
return v;
for(int i = lg-1; i >= 0; --i)
if(!isAncestor(up[i][u], v))
u = up[i][u];
return up[0][u];
}
// ******************** KRT ********************
vector<bool> has(MXS);
int par[MXS], val[MXS], degree[MXN];
void init(){
for(int i = 0; i < MXS; i++)
par[i] = i;
}
int get(int x){
return (par[x] == x ? x : par[x] = get(par[x]));
}
void unite(int u, int v, int w){
degree[u]++;
degree[v]++;
has[nxtnode] = (degree[u] >= 3 || degree[v] >= 3);
u = get(u), v = get(v);
if(u == v){
g[nxtnode].push_back(u);
par[u] = nxtnode;
has[nxtnode] = 1;
val[nxtnode] = w;
nxtnode++;
return;
}
g[nxtnode].push_back(u);
g[nxtnode].push_back(v);
val[nxtnode] = w;
has[nxtnode] = has[nxtnode] | has[u] | has[v];
par[u] = par[v] = nxtnode;
nxtnode++;
return;
}
int minup(int node){
if(has[node] == 1)
return node;
for(int i = lg-1; i >= 0; --i)
if(has[up[i][node]] == 0)
node = up[i][node];
if(has[up[0][node]] == 0) return -1;
return up[0][node];
}
// ******************** ********************
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
vector<array<int, 3>> edges(M);
for(int i = 0; i < M; i++)
edges[i] = {W[i], U[i], V[i]};
nxtnode = N;
init();
sort(edges.begin(), edges.end());
for(auto& [w, u, v] : edges){
unite(u, v, w);
}
dfs(nxtnode-1, nxtnode-1);
}
int getMinimumFuelCapacity(int X, int Y){
int Z = lca(X, Y);
Z = minup(Z);
if(Z == -1) return -1;
return val[Z];
}
#ifdef LOCAL
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
vector<int> U(M), V(M), W(M);
for(int i = 0; i < M; i++)
cin >> U[i] >> V[i] >> W[i];
init(N, M, U, V, W);
int Q;
cin >> Q;
while(Q--){
int X, Y;
cin >> X >> Y;
cout << getMinimumFuelCapacity(X, Y) << '\n';
}
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8652 KB |
Output is correct |
2 |
Correct |
6 ms |
8708 KB |
Output is correct |
3 |
Correct |
6 ms |
8668 KB |
Output is correct |
4 |
Correct |
6 ms |
8780 KB |
Output is correct |
5 |
Correct |
6 ms |
8908 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
7 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8968 KB |
Output is correct |
9 |
Correct |
127 ms |
28576 KB |
Output is correct |
10 |
Correct |
178 ms |
32904 KB |
Output is correct |
11 |
Correct |
165 ms |
32424 KB |
Output is correct |
12 |
Correct |
160 ms |
33880 KB |
Output is correct |
13 |
Correct |
124 ms |
36236 KB |
Output is correct |
14 |
Correct |
143 ms |
28568 KB |
Output is correct |
15 |
Correct |
368 ms |
34696 KB |
Output is correct |
16 |
Correct |
427 ms |
33984 KB |
Output is correct |
17 |
Correct |
388 ms |
35460 KB |
Output is correct |
18 |
Correct |
462 ms |
37772 KB |
Output is correct |
19 |
Correct |
145 ms |
16372 KB |
Output is correct |
20 |
Correct |
384 ms |
34936 KB |
Output is correct |
21 |
Correct |
372 ms |
34308 KB |
Output is correct |
22 |
Correct |
410 ms |
35932 KB |
Output is correct |
23 |
Correct |
514 ms |
38204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8652 KB |
Output is correct |
2 |
Correct |
6 ms |
8708 KB |
Output is correct |
3 |
Correct |
456 ms |
38800 KB |
Output is correct |
4 |
Correct |
458 ms |
40052 KB |
Output is correct |
5 |
Correct |
507 ms |
39308 KB |
Output is correct |
6 |
Correct |
453 ms |
39860 KB |
Output is correct |
7 |
Correct |
488 ms |
39748 KB |
Output is correct |
8 |
Correct |
463 ms |
38556 KB |
Output is correct |
9 |
Correct |
461 ms |
39368 KB |
Output is correct |
10 |
Correct |
459 ms |
38284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8652 KB |
Output is correct |
2 |
Correct |
6 ms |
8708 KB |
Output is correct |
3 |
Correct |
6 ms |
8668 KB |
Output is correct |
4 |
Correct |
6 ms |
8780 KB |
Output is correct |
5 |
Correct |
6 ms |
8908 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
7 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8968 KB |
Output is correct |
9 |
Correct |
6 ms |
8668 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
6 ms |
8908 KB |
Output is correct |
13 |
Correct |
6 ms |
8908 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
6 ms |
8908 KB |
Output is correct |
16 |
Correct |
7 ms |
8956 KB |
Output is correct |
17 |
Correct |
7 ms |
8864 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
7 ms |
8908 KB |
Output is correct |
20 |
Correct |
7 ms |
8908 KB |
Output is correct |
21 |
Correct |
7 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
9036 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
7 ms |
9144 KB |
Output is correct |
25 |
Correct |
7 ms |
9036 KB |
Output is correct |
26 |
Correct |
7 ms |
9072 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
8 ms |
9100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8668 KB |
Output is correct |
2 |
Correct |
6 ms |
8652 KB |
Output is correct |
3 |
Correct |
6 ms |
8708 KB |
Output is correct |
4 |
Correct |
6 ms |
8668 KB |
Output is correct |
5 |
Correct |
6 ms |
8780 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8968 KB |
Output is correct |
10 |
Correct |
127 ms |
28576 KB |
Output is correct |
11 |
Correct |
178 ms |
32904 KB |
Output is correct |
12 |
Correct |
165 ms |
32424 KB |
Output is correct |
13 |
Correct |
160 ms |
33880 KB |
Output is correct |
14 |
Correct |
124 ms |
36236 KB |
Output is correct |
15 |
Correct |
6 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8908 KB |
Output is correct |
21 |
Correct |
7 ms |
8956 KB |
Output is correct |
22 |
Correct |
7 ms |
8864 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
7 ms |
8908 KB |
Output is correct |
25 |
Correct |
7 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8908 KB |
Output is correct |
27 |
Correct |
7 ms |
9036 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
7 ms |
9144 KB |
Output is correct |
30 |
Correct |
7 ms |
9036 KB |
Output is correct |
31 |
Correct |
7 ms |
9072 KB |
Output is correct |
32 |
Correct |
6 ms |
8908 KB |
Output is correct |
33 |
Correct |
8 ms |
9100 KB |
Output is correct |
34 |
Correct |
18 ms |
12100 KB |
Output is correct |
35 |
Correct |
155 ms |
33348 KB |
Output is correct |
36 |
Correct |
154 ms |
33348 KB |
Output is correct |
37 |
Correct |
154 ms |
33344 KB |
Output is correct |
38 |
Correct |
156 ms |
32936 KB |
Output is correct |
39 |
Correct |
150 ms |
32836 KB |
Output is correct |
40 |
Correct |
141 ms |
30924 KB |
Output is correct |
41 |
Correct |
157 ms |
33380 KB |
Output is correct |
42 |
Correct |
160 ms |
33404 KB |
Output is correct |
43 |
Correct |
123 ms |
36444 KB |
Output is correct |
44 |
Correct |
182 ms |
33360 KB |
Output is correct |
45 |
Correct |
184 ms |
43076 KB |
Output is correct |
46 |
Correct |
157 ms |
33352 KB |
Output is correct |
47 |
Correct |
151 ms |
33472 KB |
Output is correct |
48 |
Correct |
157 ms |
36580 KB |
Output is correct |
49 |
Correct |
128 ms |
40956 KB |
Output is correct |
50 |
Correct |
104 ms |
33708 KB |
Output is correct |
51 |
Correct |
152 ms |
38496 KB |
Output is correct |
52 |
Correct |
211 ms |
45892 KB |
Output is correct |
53 |
Correct |
237 ms |
48356 KB |
Output is correct |
54 |
Correct |
245 ms |
53572 KB |
Output is correct |
55 |
Correct |
119 ms |
36204 KB |
Output is correct |
56 |
Correct |
223 ms |
49288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8652 KB |
Output is correct |
2 |
Correct |
6 ms |
8708 KB |
Output is correct |
3 |
Correct |
6 ms |
8668 KB |
Output is correct |
4 |
Correct |
6 ms |
8780 KB |
Output is correct |
5 |
Correct |
6 ms |
8908 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
7 ms |
8940 KB |
Output is correct |
8 |
Correct |
7 ms |
8968 KB |
Output is correct |
9 |
Correct |
127 ms |
28576 KB |
Output is correct |
10 |
Correct |
178 ms |
32904 KB |
Output is correct |
11 |
Correct |
165 ms |
32424 KB |
Output is correct |
12 |
Correct |
160 ms |
33880 KB |
Output is correct |
13 |
Correct |
124 ms |
36236 KB |
Output is correct |
14 |
Correct |
143 ms |
28568 KB |
Output is correct |
15 |
Correct |
368 ms |
34696 KB |
Output is correct |
16 |
Correct |
427 ms |
33984 KB |
Output is correct |
17 |
Correct |
388 ms |
35460 KB |
Output is correct |
18 |
Correct |
462 ms |
37772 KB |
Output is correct |
19 |
Correct |
456 ms |
38800 KB |
Output is correct |
20 |
Correct |
458 ms |
40052 KB |
Output is correct |
21 |
Correct |
507 ms |
39308 KB |
Output is correct |
22 |
Correct |
453 ms |
39860 KB |
Output is correct |
23 |
Correct |
488 ms |
39748 KB |
Output is correct |
24 |
Correct |
463 ms |
38556 KB |
Output is correct |
25 |
Correct |
461 ms |
39368 KB |
Output is correct |
26 |
Correct |
459 ms |
38284 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
6 ms |
8908 KB |
Output is correct |
33 |
Correct |
7 ms |
8956 KB |
Output is correct |
34 |
Correct |
7 ms |
8864 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
18 ms |
12100 KB |
Output is correct |
37 |
Correct |
155 ms |
33348 KB |
Output is correct |
38 |
Correct |
154 ms |
33348 KB |
Output is correct |
39 |
Correct |
154 ms |
33344 KB |
Output is correct |
40 |
Correct |
156 ms |
32936 KB |
Output is correct |
41 |
Correct |
150 ms |
32836 KB |
Output is correct |
42 |
Correct |
141 ms |
30924 KB |
Output is correct |
43 |
Correct |
157 ms |
33380 KB |
Output is correct |
44 |
Correct |
160 ms |
33404 KB |
Output is correct |
45 |
Correct |
123 ms |
36444 KB |
Output is correct |
46 |
Correct |
182 ms |
33360 KB |
Output is correct |
47 |
Correct |
30 ms |
11976 KB |
Output is correct |
48 |
Correct |
369 ms |
34880 KB |
Output is correct |
49 |
Correct |
364 ms |
34880 KB |
Output is correct |
50 |
Correct |
369 ms |
34920 KB |
Output is correct |
51 |
Correct |
349 ms |
34752 KB |
Output is correct |
52 |
Correct |
348 ms |
33224 KB |
Output is correct |
53 |
Correct |
269 ms |
27780 KB |
Output is correct |
54 |
Correct |
374 ms |
35316 KB |
Output is correct |
55 |
Correct |
376 ms |
34984 KB |
Output is correct |
56 |
Correct |
440 ms |
37316 KB |
Output is correct |
57 |
Correct |
351 ms |
35388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8668 KB |
Output is correct |
2 |
Correct |
6 ms |
8652 KB |
Output is correct |
3 |
Correct |
6 ms |
8708 KB |
Output is correct |
4 |
Correct |
6 ms |
8668 KB |
Output is correct |
5 |
Correct |
6 ms |
8780 KB |
Output is correct |
6 |
Correct |
6 ms |
8908 KB |
Output is correct |
7 |
Correct |
6 ms |
8908 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8968 KB |
Output is correct |
10 |
Correct |
127 ms |
28576 KB |
Output is correct |
11 |
Correct |
178 ms |
32904 KB |
Output is correct |
12 |
Correct |
165 ms |
32424 KB |
Output is correct |
13 |
Correct |
160 ms |
33880 KB |
Output is correct |
14 |
Correct |
124 ms |
36236 KB |
Output is correct |
15 |
Correct |
143 ms |
28568 KB |
Output is correct |
16 |
Correct |
368 ms |
34696 KB |
Output is correct |
17 |
Correct |
427 ms |
33984 KB |
Output is correct |
18 |
Correct |
388 ms |
35460 KB |
Output is correct |
19 |
Correct |
462 ms |
37772 KB |
Output is correct |
20 |
Correct |
456 ms |
38800 KB |
Output is correct |
21 |
Correct |
458 ms |
40052 KB |
Output is correct |
22 |
Correct |
507 ms |
39308 KB |
Output is correct |
23 |
Correct |
453 ms |
39860 KB |
Output is correct |
24 |
Correct |
488 ms |
39748 KB |
Output is correct |
25 |
Correct |
463 ms |
38556 KB |
Output is correct |
26 |
Correct |
461 ms |
39368 KB |
Output is correct |
27 |
Correct |
459 ms |
38284 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
6 ms |
8908 KB |
Output is correct |
33 |
Correct |
6 ms |
8908 KB |
Output is correct |
34 |
Correct |
7 ms |
8956 KB |
Output is correct |
35 |
Correct |
7 ms |
8864 KB |
Output is correct |
36 |
Correct |
6 ms |
8908 KB |
Output is correct |
37 |
Correct |
18 ms |
12100 KB |
Output is correct |
38 |
Correct |
155 ms |
33348 KB |
Output is correct |
39 |
Correct |
154 ms |
33348 KB |
Output is correct |
40 |
Correct |
154 ms |
33344 KB |
Output is correct |
41 |
Correct |
156 ms |
32936 KB |
Output is correct |
42 |
Correct |
150 ms |
32836 KB |
Output is correct |
43 |
Correct |
141 ms |
30924 KB |
Output is correct |
44 |
Correct |
157 ms |
33380 KB |
Output is correct |
45 |
Correct |
160 ms |
33404 KB |
Output is correct |
46 |
Correct |
123 ms |
36444 KB |
Output is correct |
47 |
Correct |
182 ms |
33360 KB |
Output is correct |
48 |
Correct |
30 ms |
11976 KB |
Output is correct |
49 |
Correct |
369 ms |
34880 KB |
Output is correct |
50 |
Correct |
364 ms |
34880 KB |
Output is correct |
51 |
Correct |
369 ms |
34920 KB |
Output is correct |
52 |
Correct |
349 ms |
34752 KB |
Output is correct |
53 |
Correct |
348 ms |
33224 KB |
Output is correct |
54 |
Correct |
269 ms |
27780 KB |
Output is correct |
55 |
Correct |
374 ms |
35316 KB |
Output is correct |
56 |
Correct |
376 ms |
34984 KB |
Output is correct |
57 |
Correct |
440 ms |
37316 KB |
Output is correct |
58 |
Correct |
351 ms |
35388 KB |
Output is correct |
59 |
Correct |
145 ms |
16372 KB |
Output is correct |
60 |
Correct |
384 ms |
34936 KB |
Output is correct |
61 |
Correct |
372 ms |
34308 KB |
Output is correct |
62 |
Correct |
410 ms |
35932 KB |
Output is correct |
63 |
Correct |
514 ms |
38204 KB |
Output is correct |
64 |
Correct |
7 ms |
8908 KB |
Output is correct |
65 |
Correct |
7 ms |
8908 KB |
Output is correct |
66 |
Correct |
7 ms |
8908 KB |
Output is correct |
67 |
Correct |
7 ms |
9036 KB |
Output is correct |
68 |
Correct |
6 ms |
8908 KB |
Output is correct |
69 |
Correct |
7 ms |
9144 KB |
Output is correct |
70 |
Correct |
7 ms |
9036 KB |
Output is correct |
71 |
Correct |
7 ms |
9072 KB |
Output is correct |
72 |
Correct |
6 ms |
8908 KB |
Output is correct |
73 |
Correct |
8 ms |
9100 KB |
Output is correct |
74 |
Correct |
184 ms |
43076 KB |
Output is correct |
75 |
Correct |
157 ms |
33352 KB |
Output is correct |
76 |
Correct |
151 ms |
33472 KB |
Output is correct |
77 |
Correct |
157 ms |
36580 KB |
Output is correct |
78 |
Correct |
128 ms |
40956 KB |
Output is correct |
79 |
Correct |
104 ms |
33708 KB |
Output is correct |
80 |
Correct |
152 ms |
38496 KB |
Output is correct |
81 |
Correct |
211 ms |
45892 KB |
Output is correct |
82 |
Correct |
237 ms |
48356 KB |
Output is correct |
83 |
Correct |
245 ms |
53572 KB |
Output is correct |
84 |
Correct |
119 ms |
36204 KB |
Output is correct |
85 |
Correct |
223 ms |
49288 KB |
Output is correct |
86 |
Correct |
108 ms |
21124 KB |
Output is correct |
87 |
Correct |
369 ms |
34888 KB |
Output is correct |
88 |
Correct |
352 ms |
34880 KB |
Output is correct |
89 |
Correct |
456 ms |
36932 KB |
Output is correct |
90 |
Correct |
254 ms |
44356 KB |
Output is correct |
91 |
Correct |
289 ms |
41196 KB |
Output is correct |
92 |
Correct |
460 ms |
38832 KB |
Output is correct |
93 |
Correct |
449 ms |
46772 KB |
Output is correct |
94 |
Correct |
679 ms |
49732 KB |
Output is correct |
95 |
Correct |
475 ms |
55144 KB |
Output is correct |
96 |
Correct |
420 ms |
37572 KB |
Output is correct |
97 |
Correct |
558 ms |
43332 KB |
Output is correct |