#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;
int nxtnode = 0;
// ******************** global variables ********************
vector<int> g[MXS];
// ******************** 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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8688 KB |
Output is correct |
3 |
Correct |
6 ms |
8608 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 |
6 ms |
8888 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
118 ms |
27972 KB |
Output is correct |
10 |
Correct |
147 ms |
32336 KB |
Output is correct |
11 |
Correct |
146 ms |
31940 KB |
Output is correct |
12 |
Correct |
160 ms |
33348 KB |
Output is correct |
13 |
Correct |
121 ms |
35560 KB |
Output is correct |
14 |
Correct |
142 ms |
28100 KB |
Output is correct |
15 |
Correct |
381 ms |
34128 KB |
Output is correct |
16 |
Correct |
367 ms |
33348 KB |
Output is correct |
17 |
Correct |
382 ms |
34884 KB |
Output is correct |
18 |
Correct |
460 ms |
37268 KB |
Output is correct |
19 |
Correct |
142 ms |
15880 KB |
Output is correct |
20 |
Correct |
374 ms |
34408 KB |
Output is correct |
21 |
Correct |
371 ms |
33720 KB |
Output is correct |
22 |
Correct |
382 ms |
35260 KB |
Output is correct |
23 |
Correct |
518 ms |
37584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8688 KB |
Output is correct |
3 |
Correct |
458 ms |
38368 KB |
Output is correct |
4 |
Correct |
449 ms |
39572 KB |
Output is correct |
5 |
Correct |
531 ms |
38864 KB |
Output is correct |
6 |
Correct |
453 ms |
39576 KB |
Output is correct |
7 |
Correct |
498 ms |
39268 KB |
Output is correct |
8 |
Correct |
451 ms |
38132 KB |
Output is correct |
9 |
Correct |
482 ms |
38956 KB |
Output is correct |
10 |
Correct |
477 ms |
37960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8688 KB |
Output is correct |
3 |
Correct |
6 ms |
8608 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 |
6 ms |
8888 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
5 ms |
8652 KB |
Output is correct |
10 |
Correct |
6 ms |
8908 KB |
Output is correct |
11 |
Correct |
6 ms |
8908 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
6 ms |
8908 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
7 ms |
8916 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 |
8940 KB |
Output is correct |
20 |
Correct |
6 ms |
8936 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
9000 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
8 ms |
9036 KB |
Output is correct |
25 |
Correct |
7 ms |
9036 KB |
Output is correct |
26 |
Correct |
7 ms |
9156 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
7 ms |
9164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8652 KB |
Output is correct |
3 |
Correct |
5 ms |
8688 KB |
Output is correct |
4 |
Correct |
6 ms |
8608 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 |
6 ms |
8888 KB |
Output is correct |
9 |
Correct |
6 ms |
8908 KB |
Output is correct |
10 |
Correct |
118 ms |
27972 KB |
Output is correct |
11 |
Correct |
147 ms |
32336 KB |
Output is correct |
12 |
Correct |
146 ms |
31940 KB |
Output is correct |
13 |
Correct |
160 ms |
33348 KB |
Output is correct |
14 |
Correct |
121 ms |
35560 KB |
Output is correct |
15 |
Correct |
6 ms |
8908 KB |
Output is correct |
16 |
Correct |
6 ms |
8908 KB |
Output is correct |
17 |
Correct |
7 ms |
8940 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
7 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8908 KB |
Output is correct |
22 |
Correct |
6 ms |
8908 KB |
Output is correct |
23 |
Correct |
6 ms |
8908 KB |
Output is correct |
24 |
Correct |
6 ms |
8940 KB |
Output is correct |
25 |
Correct |
6 ms |
8936 KB |
Output is correct |
26 |
Correct |
6 ms |
8908 KB |
Output is correct |
27 |
Correct |
7 ms |
9000 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
8 ms |
9036 KB |
Output is correct |
30 |
Correct |
7 ms |
9036 KB |
Output is correct |
31 |
Correct |
7 ms |
9156 KB |
Output is correct |
32 |
Correct |
6 ms |
8908 KB |
Output is correct |
33 |
Correct |
7 ms |
9164 KB |
Output is correct |
34 |
Correct |
18 ms |
12352 KB |
Output is correct |
35 |
Correct |
154 ms |
35056 KB |
Output is correct |
36 |
Correct |
153 ms |
35260 KB |
Output is correct |
37 |
Correct |
162 ms |
35044 KB |
Output is correct |
38 |
Correct |
149 ms |
34756 KB |
Output is correct |
39 |
Correct |
156 ms |
34772 KB |
Output is correct |
40 |
Correct |
138 ms |
32452 KB |
Output is correct |
41 |
Correct |
157 ms |
35444 KB |
Output is correct |
42 |
Correct |
153 ms |
35052 KB |
Output is correct |
43 |
Correct |
120 ms |
38212 KB |
Output is correct |
44 |
Correct |
159 ms |
35484 KB |
Output is correct |
45 |
Correct |
190 ms |
46092 KB |
Output is correct |
46 |
Correct |
154 ms |
35060 KB |
Output is correct |
47 |
Correct |
152 ms |
35264 KB |
Output is correct |
48 |
Correct |
155 ms |
38620 KB |
Output is correct |
49 |
Correct |
126 ms |
43588 KB |
Output is correct |
50 |
Correct |
112 ms |
35920 KB |
Output is correct |
51 |
Correct |
155 ms |
41128 KB |
Output is correct |
52 |
Correct |
211 ms |
49216 KB |
Output is correct |
53 |
Correct |
237 ms |
52072 KB |
Output is correct |
54 |
Correct |
271 ms |
57412 KB |
Output is correct |
55 |
Correct |
119 ms |
37872 KB |
Output is correct |
56 |
Correct |
220 ms |
52932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8688 KB |
Output is correct |
3 |
Correct |
6 ms |
8608 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 |
6 ms |
8888 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
118 ms |
27972 KB |
Output is correct |
10 |
Correct |
147 ms |
32336 KB |
Output is correct |
11 |
Correct |
146 ms |
31940 KB |
Output is correct |
12 |
Correct |
160 ms |
33348 KB |
Output is correct |
13 |
Correct |
121 ms |
35560 KB |
Output is correct |
14 |
Correct |
142 ms |
28100 KB |
Output is correct |
15 |
Correct |
381 ms |
34128 KB |
Output is correct |
16 |
Correct |
367 ms |
33348 KB |
Output is correct |
17 |
Correct |
382 ms |
34884 KB |
Output is correct |
18 |
Correct |
460 ms |
37268 KB |
Output is correct |
19 |
Correct |
458 ms |
38368 KB |
Output is correct |
20 |
Correct |
449 ms |
39572 KB |
Output is correct |
21 |
Correct |
531 ms |
38864 KB |
Output is correct |
22 |
Correct |
453 ms |
39576 KB |
Output is correct |
23 |
Correct |
498 ms |
39268 KB |
Output is correct |
24 |
Correct |
451 ms |
38132 KB |
Output is correct |
25 |
Correct |
482 ms |
38956 KB |
Output is correct |
26 |
Correct |
477 ms |
37960 KB |
Output is correct |
27 |
Correct |
6 ms |
8908 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
7 ms |
8940 KB |
Output is correct |
30 |
Correct |
6 ms |
8908 KB |
Output is correct |
31 |
Correct |
6 ms |
8908 KB |
Output is correct |
32 |
Correct |
7 ms |
8916 KB |
Output is correct |
33 |
Correct |
6 ms |
8908 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
18 ms |
12352 KB |
Output is correct |
37 |
Correct |
154 ms |
35056 KB |
Output is correct |
38 |
Correct |
153 ms |
35260 KB |
Output is correct |
39 |
Correct |
162 ms |
35044 KB |
Output is correct |
40 |
Correct |
149 ms |
34756 KB |
Output is correct |
41 |
Correct |
156 ms |
34772 KB |
Output is correct |
42 |
Correct |
138 ms |
32452 KB |
Output is correct |
43 |
Correct |
157 ms |
35444 KB |
Output is correct |
44 |
Correct |
153 ms |
35052 KB |
Output is correct |
45 |
Correct |
120 ms |
38212 KB |
Output is correct |
46 |
Correct |
159 ms |
35484 KB |
Output is correct |
47 |
Correct |
29 ms |
12336 KB |
Output is correct |
48 |
Correct |
360 ms |
39016 KB |
Output is correct |
49 |
Correct |
387 ms |
39108 KB |
Output is correct |
50 |
Correct |
372 ms |
38964 KB |
Output is correct |
51 |
Correct |
359 ms |
38756 KB |
Output is correct |
52 |
Correct |
342 ms |
37248 KB |
Output is correct |
53 |
Correct |
273 ms |
31404 KB |
Output is correct |
54 |
Correct |
374 ms |
39852 KB |
Output is correct |
55 |
Correct |
422 ms |
38980 KB |
Output is correct |
56 |
Correct |
421 ms |
41412 KB |
Output is correct |
57 |
Correct |
354 ms |
39744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8652 KB |
Output is correct |
2 |
Correct |
5 ms |
8652 KB |
Output is correct |
3 |
Correct |
5 ms |
8688 KB |
Output is correct |
4 |
Correct |
6 ms |
8608 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 |
6 ms |
8888 KB |
Output is correct |
9 |
Correct |
6 ms |
8908 KB |
Output is correct |
10 |
Correct |
118 ms |
27972 KB |
Output is correct |
11 |
Correct |
147 ms |
32336 KB |
Output is correct |
12 |
Correct |
146 ms |
31940 KB |
Output is correct |
13 |
Correct |
160 ms |
33348 KB |
Output is correct |
14 |
Correct |
121 ms |
35560 KB |
Output is correct |
15 |
Correct |
142 ms |
28100 KB |
Output is correct |
16 |
Correct |
381 ms |
34128 KB |
Output is correct |
17 |
Correct |
367 ms |
33348 KB |
Output is correct |
18 |
Correct |
382 ms |
34884 KB |
Output is correct |
19 |
Correct |
460 ms |
37268 KB |
Output is correct |
20 |
Correct |
458 ms |
38368 KB |
Output is correct |
21 |
Correct |
449 ms |
39572 KB |
Output is correct |
22 |
Correct |
531 ms |
38864 KB |
Output is correct |
23 |
Correct |
453 ms |
39576 KB |
Output is correct |
24 |
Correct |
498 ms |
39268 KB |
Output is correct |
25 |
Correct |
451 ms |
38132 KB |
Output is correct |
26 |
Correct |
482 ms |
38956 KB |
Output is correct |
27 |
Correct |
477 ms |
37960 KB |
Output is correct |
28 |
Correct |
6 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
7 ms |
8940 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 |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
8908 KB |
Output is correct |
36 |
Correct |
6 ms |
8908 KB |
Output is correct |
37 |
Correct |
18 ms |
12352 KB |
Output is correct |
38 |
Correct |
154 ms |
35056 KB |
Output is correct |
39 |
Correct |
153 ms |
35260 KB |
Output is correct |
40 |
Correct |
162 ms |
35044 KB |
Output is correct |
41 |
Correct |
149 ms |
34756 KB |
Output is correct |
42 |
Correct |
156 ms |
34772 KB |
Output is correct |
43 |
Correct |
138 ms |
32452 KB |
Output is correct |
44 |
Correct |
157 ms |
35444 KB |
Output is correct |
45 |
Correct |
153 ms |
35052 KB |
Output is correct |
46 |
Correct |
120 ms |
38212 KB |
Output is correct |
47 |
Correct |
159 ms |
35484 KB |
Output is correct |
48 |
Correct |
29 ms |
12336 KB |
Output is correct |
49 |
Correct |
360 ms |
39016 KB |
Output is correct |
50 |
Correct |
387 ms |
39108 KB |
Output is correct |
51 |
Correct |
372 ms |
38964 KB |
Output is correct |
52 |
Correct |
359 ms |
38756 KB |
Output is correct |
53 |
Correct |
342 ms |
37248 KB |
Output is correct |
54 |
Correct |
273 ms |
31404 KB |
Output is correct |
55 |
Correct |
374 ms |
39852 KB |
Output is correct |
56 |
Correct |
422 ms |
38980 KB |
Output is correct |
57 |
Correct |
421 ms |
41412 KB |
Output is correct |
58 |
Correct |
354 ms |
39744 KB |
Output is correct |
59 |
Correct |
142 ms |
15880 KB |
Output is correct |
60 |
Correct |
374 ms |
34408 KB |
Output is correct |
61 |
Correct |
371 ms |
33720 KB |
Output is correct |
62 |
Correct |
382 ms |
35260 KB |
Output is correct |
63 |
Correct |
518 ms |
37584 KB |
Output is correct |
64 |
Correct |
6 ms |
8940 KB |
Output is correct |
65 |
Correct |
6 ms |
8936 KB |
Output is correct |
66 |
Correct |
6 ms |
8908 KB |
Output is correct |
67 |
Correct |
7 ms |
9000 KB |
Output is correct |
68 |
Correct |
6 ms |
8908 KB |
Output is correct |
69 |
Correct |
8 ms |
9036 KB |
Output is correct |
70 |
Correct |
7 ms |
9036 KB |
Output is correct |
71 |
Correct |
7 ms |
9156 KB |
Output is correct |
72 |
Correct |
6 ms |
8908 KB |
Output is correct |
73 |
Correct |
7 ms |
9164 KB |
Output is correct |
74 |
Correct |
190 ms |
46092 KB |
Output is correct |
75 |
Correct |
154 ms |
35060 KB |
Output is correct |
76 |
Correct |
152 ms |
35264 KB |
Output is correct |
77 |
Correct |
155 ms |
38620 KB |
Output is correct |
78 |
Correct |
126 ms |
43588 KB |
Output is correct |
79 |
Correct |
112 ms |
35920 KB |
Output is correct |
80 |
Correct |
155 ms |
41128 KB |
Output is correct |
81 |
Correct |
211 ms |
49216 KB |
Output is correct |
82 |
Correct |
237 ms |
52072 KB |
Output is correct |
83 |
Correct |
271 ms |
57412 KB |
Output is correct |
84 |
Correct |
119 ms |
37872 KB |
Output is correct |
85 |
Correct |
220 ms |
52932 KB |
Output is correct |
86 |
Correct |
105 ms |
22524 KB |
Output is correct |
87 |
Correct |
376 ms |
39008 KB |
Output is correct |
88 |
Correct |
351 ms |
39020 KB |
Output is correct |
89 |
Correct |
469 ms |
41284 KB |
Output is correct |
90 |
Correct |
267 ms |
49040 KB |
Output is correct |
91 |
Correct |
312 ms |
46112 KB |
Output is correct |
92 |
Correct |
450 ms |
43612 KB |
Output is correct |
93 |
Correct |
442 ms |
52380 KB |
Output is correct |
94 |
Correct |
665 ms |
55928 KB |
Output is correct |
95 |
Correct |
511 ms |
61380 KB |
Output is correct |
96 |
Correct |
422 ms |
41544 KB |
Output is correct |
97 |
Correct |
543 ms |
48572 KB |
Output is correct |