#include<bits/stdc++.h>
using namespace std;
//#define LOCAL
#ifndef LOCAL
#include "swap.h"
#endif // LOCAL
const int N = 2e5 + 10;
struct edge {
int u, v, w;
edge() : u(0), v(0), w(0) {}
edge(int u, int v, int w) : u(u), v(v), w(w) {}
} ED[N];
int par[N], id[N];
int root(int u, int ty = 0) {
if (ty) return (id[u] < 0 ? u : id[u] = root(id[u], ty));
return (par[u] < 0 ? u : par[u] = root(par[u]));
}
void add(int u, int v, int ty = 0) {
u = root(u, ty); v = root(v, ty);
if (u == v) return;
if (ty) {
if (u < v) swap(u, v);
id[u] += id[v];
id[v] = u;
} else {
if (par[u] > par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
}
}
int n, m;
vector<int> ad[N];
bool mk[N];
int cnt[N], weight[N];
int low[N];
int f[N][17], st[N], ed[N], it;
void dfs(int u, int pre = -1) {
if (mk[u]) low[u] = weight[u];
st[u] = ++it;
for (int i = 1; i <= 16; ++i)
f[u][i] = f[f[u][i - 1]][i - 1];
for (int v : ad[u]) {
if (v == pre) continue;
if (!mk[v]) low[v] = low[u];
f[v][0] = u;
dfs(v, u);
}
ed[u] = it;
}
bool anc(int u, int v) {
return st[u] <= st[v] && ed[u] >= ed[v];
}
int lca(int u, int v) {
if (anc(u, v)) return u;
if (anc(v, u)) return v;
for (int i = 16; i >= 0; --i)
if (!anc(f[u][i], v)) u = f[u][i];
return f[u][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N; m = M;
for (int i = 0; i < m; ++i) {
int u = U[i], v = V[i], w = W[i];
ED[i] = {u, v, w};
}
sort(ED, ED + m, [&] (const auto& a, const auto& b) {
return a.w < b.w;
});
memset(par, -1, sizeof par);
memset(id, -1, sizeof id);
int it = n - 1;
for (int i = 0; i < m; ++i) {
int u = ED[i].u, v = ED[i].v, w = ED[i].w;
cnt[u] += 1; cnt[v] += 1;
if (root(u) == root(v)) {
if (!mk[it]) {
mk[it] = 1;
weight[it] = w;
}
continue;
}
add(u, v);
it += 1;
if ((cnt[u] >= 3 || cnt[v] >= 3) && !mk[it]) {
mk[it] = 1;
weight[it] = w;
}
u = root(u, 1); v = root(v, 1);
ad[it].push_back(u); ad[it].push_back(v);
mk[it] |= (mk[u] | mk[v]);
add(it, u, 1); add(it, v, 1);
weight[it] = w;
}
memset(low, -1, sizeof low);
dfs(f[it][0] = it);
}
int getMinimumFuelCapacity(int X, int Y) {
return low[lca(X, Y)];
}
#ifdef LOCAL
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
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;
for (int i = 1; i <= q; ++i) {
int X, Y; cin >> X >> Y;
cout << getMinimumFuelCapacity(X, Y) << '\n';
}
}
#endif // LOCAL
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13964 KB |
Output is correct |
3 |
Correct |
5 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
4 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
83 ms |
29732 KB |
Output is correct |
10 |
Correct |
96 ms |
32848 KB |
Output is correct |
11 |
Correct |
101 ms |
32848 KB |
Output is correct |
12 |
Correct |
118 ms |
33160 KB |
Output is correct |
13 |
Correct |
110 ms |
37060 KB |
Output is correct |
14 |
Correct |
83 ms |
30124 KB |
Output is correct |
15 |
Correct |
165 ms |
35804 KB |
Output is correct |
16 |
Correct |
181 ms |
35592 KB |
Output is correct |
17 |
Correct |
170 ms |
36268 KB |
Output is correct |
18 |
Correct |
201 ms |
40120 KB |
Output is correct |
19 |
Correct |
64 ms |
21584 KB |
Output is correct |
20 |
Correct |
176 ms |
36816 KB |
Output is correct |
21 |
Correct |
168 ms |
36880 KB |
Output is correct |
22 |
Correct |
191 ms |
37496 KB |
Output is correct |
23 |
Correct |
181 ms |
41404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13964 KB |
Output is correct |
3 |
Correct |
184 ms |
44056 KB |
Output is correct |
4 |
Correct |
197 ms |
44664 KB |
Output is correct |
5 |
Correct |
193 ms |
44560 KB |
Output is correct |
6 |
Correct |
187 ms |
44308 KB |
Output is correct |
7 |
Correct |
190 ms |
44808 KB |
Output is correct |
8 |
Correct |
153 ms |
44032 KB |
Output is correct |
9 |
Correct |
159 ms |
44496 KB |
Output is correct |
10 |
Correct |
178 ms |
44040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13964 KB |
Output is correct |
3 |
Correct |
5 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
4 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
5 ms |
13912 KB |
Output is correct |
10 |
Correct |
5 ms |
13916 KB |
Output is correct |
11 |
Correct |
4 ms |
13916 KB |
Output is correct |
12 |
Correct |
5 ms |
13916 KB |
Output is correct |
13 |
Correct |
4 ms |
13916 KB |
Output is correct |
14 |
Correct |
4 ms |
14024 KB |
Output is correct |
15 |
Correct |
5 ms |
13916 KB |
Output is correct |
16 |
Correct |
5 ms |
13984 KB |
Output is correct |
17 |
Correct |
4 ms |
13916 KB |
Output is correct |
18 |
Correct |
4 ms |
14000 KB |
Output is correct |
19 |
Correct |
4 ms |
13912 KB |
Output is correct |
20 |
Correct |
4 ms |
13916 KB |
Output is correct |
21 |
Correct |
5 ms |
13916 KB |
Output is correct |
22 |
Correct |
4 ms |
13916 KB |
Output is correct |
23 |
Correct |
4 ms |
13916 KB |
Output is correct |
24 |
Incorrect |
5 ms |
13912 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13964 KB |
Output is correct |
4 |
Correct |
5 ms |
13916 KB |
Output is correct |
5 |
Correct |
4 ms |
13912 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13916 KB |
Output is correct |
10 |
Correct |
83 ms |
29732 KB |
Output is correct |
11 |
Correct |
96 ms |
32848 KB |
Output is correct |
12 |
Correct |
101 ms |
32848 KB |
Output is correct |
13 |
Correct |
118 ms |
33160 KB |
Output is correct |
14 |
Correct |
110 ms |
37060 KB |
Output is correct |
15 |
Correct |
5 ms |
13916 KB |
Output is correct |
16 |
Correct |
4 ms |
13916 KB |
Output is correct |
17 |
Correct |
5 ms |
13916 KB |
Output is correct |
18 |
Correct |
4 ms |
13916 KB |
Output is correct |
19 |
Correct |
4 ms |
14024 KB |
Output is correct |
20 |
Correct |
5 ms |
13916 KB |
Output is correct |
21 |
Correct |
5 ms |
13984 KB |
Output is correct |
22 |
Correct |
4 ms |
13916 KB |
Output is correct |
23 |
Correct |
4 ms |
14000 KB |
Output is correct |
24 |
Correct |
4 ms |
13912 KB |
Output is correct |
25 |
Correct |
4 ms |
13916 KB |
Output is correct |
26 |
Correct |
5 ms |
13916 KB |
Output is correct |
27 |
Correct |
4 ms |
13916 KB |
Output is correct |
28 |
Correct |
4 ms |
13916 KB |
Output is correct |
29 |
Incorrect |
5 ms |
13912 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13964 KB |
Output is correct |
3 |
Correct |
5 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
4 ms |
13916 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
83 ms |
29732 KB |
Output is correct |
10 |
Correct |
96 ms |
32848 KB |
Output is correct |
11 |
Correct |
101 ms |
32848 KB |
Output is correct |
12 |
Correct |
118 ms |
33160 KB |
Output is correct |
13 |
Correct |
110 ms |
37060 KB |
Output is correct |
14 |
Correct |
83 ms |
30124 KB |
Output is correct |
15 |
Correct |
165 ms |
35804 KB |
Output is correct |
16 |
Correct |
181 ms |
35592 KB |
Output is correct |
17 |
Correct |
170 ms |
36268 KB |
Output is correct |
18 |
Correct |
201 ms |
40120 KB |
Output is correct |
19 |
Correct |
184 ms |
44056 KB |
Output is correct |
20 |
Correct |
197 ms |
44664 KB |
Output is correct |
21 |
Correct |
193 ms |
44560 KB |
Output is correct |
22 |
Correct |
187 ms |
44308 KB |
Output is correct |
23 |
Correct |
190 ms |
44808 KB |
Output is correct |
24 |
Correct |
153 ms |
44032 KB |
Output is correct |
25 |
Correct |
159 ms |
44496 KB |
Output is correct |
26 |
Correct |
178 ms |
44040 KB |
Output is correct |
27 |
Correct |
5 ms |
13916 KB |
Output is correct |
28 |
Correct |
4 ms |
13916 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
4 ms |
13916 KB |
Output is correct |
31 |
Correct |
4 ms |
14024 KB |
Output is correct |
32 |
Correct |
5 ms |
13916 KB |
Output is correct |
33 |
Correct |
5 ms |
13984 KB |
Output is correct |
34 |
Correct |
4 ms |
13916 KB |
Output is correct |
35 |
Correct |
4 ms |
14000 KB |
Output is correct |
36 |
Correct |
11 ms |
16988 KB |
Output is correct |
37 |
Correct |
110 ms |
33212 KB |
Output is correct |
38 |
Correct |
121 ms |
33104 KB |
Output is correct |
39 |
Correct |
105 ms |
33108 KB |
Output is correct |
40 |
Correct |
101 ms |
33104 KB |
Output is correct |
41 |
Correct |
116 ms |
33124 KB |
Output is correct |
42 |
Correct |
96 ms |
32204 KB |
Output is correct |
43 |
Correct |
109 ms |
33052 KB |
Output is correct |
44 |
Correct |
120 ms |
33180 KB |
Output is correct |
45 |
Correct |
90 ms |
38228 KB |
Output is correct |
46 |
Correct |
107 ms |
33280 KB |
Output is correct |
47 |
Correct |
18 ms |
17500 KB |
Output is correct |
48 |
Correct |
172 ms |
36796 KB |
Output is correct |
49 |
Correct |
179 ms |
36792 KB |
Output is correct |
50 |
Correct |
182 ms |
36796 KB |
Output is correct |
51 |
Correct |
178 ms |
36552 KB |
Output is correct |
52 |
Correct |
194 ms |
36348 KB |
Output is correct |
53 |
Correct |
131 ms |
31456 KB |
Output is correct |
54 |
Correct |
177 ms |
37556 KB |
Output is correct |
55 |
Correct |
179 ms |
36896 KB |
Output is correct |
56 |
Correct |
178 ms |
40864 KB |
Output is correct |
57 |
Correct |
207 ms |
37560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13964 KB |
Output is correct |
4 |
Correct |
5 ms |
13916 KB |
Output is correct |
5 |
Correct |
4 ms |
13912 KB |
Output is correct |
6 |
Correct |
4 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13916 KB |
Output is correct |
10 |
Correct |
83 ms |
29732 KB |
Output is correct |
11 |
Correct |
96 ms |
32848 KB |
Output is correct |
12 |
Correct |
101 ms |
32848 KB |
Output is correct |
13 |
Correct |
118 ms |
33160 KB |
Output is correct |
14 |
Correct |
110 ms |
37060 KB |
Output is correct |
15 |
Correct |
83 ms |
30124 KB |
Output is correct |
16 |
Correct |
165 ms |
35804 KB |
Output is correct |
17 |
Correct |
181 ms |
35592 KB |
Output is correct |
18 |
Correct |
170 ms |
36268 KB |
Output is correct |
19 |
Correct |
201 ms |
40120 KB |
Output is correct |
20 |
Correct |
184 ms |
44056 KB |
Output is correct |
21 |
Correct |
197 ms |
44664 KB |
Output is correct |
22 |
Correct |
193 ms |
44560 KB |
Output is correct |
23 |
Correct |
187 ms |
44308 KB |
Output is correct |
24 |
Correct |
190 ms |
44808 KB |
Output is correct |
25 |
Correct |
153 ms |
44032 KB |
Output is correct |
26 |
Correct |
159 ms |
44496 KB |
Output is correct |
27 |
Correct |
178 ms |
44040 KB |
Output is correct |
28 |
Correct |
5 ms |
13916 KB |
Output is correct |
29 |
Correct |
4 ms |
13916 KB |
Output is correct |
30 |
Correct |
5 ms |
13916 KB |
Output is correct |
31 |
Correct |
4 ms |
13916 KB |
Output is correct |
32 |
Correct |
4 ms |
14024 KB |
Output is correct |
33 |
Correct |
5 ms |
13916 KB |
Output is correct |
34 |
Correct |
5 ms |
13984 KB |
Output is correct |
35 |
Correct |
4 ms |
13916 KB |
Output is correct |
36 |
Correct |
4 ms |
14000 KB |
Output is correct |
37 |
Correct |
11 ms |
16988 KB |
Output is correct |
38 |
Correct |
110 ms |
33212 KB |
Output is correct |
39 |
Correct |
121 ms |
33104 KB |
Output is correct |
40 |
Correct |
105 ms |
33108 KB |
Output is correct |
41 |
Correct |
101 ms |
33104 KB |
Output is correct |
42 |
Correct |
116 ms |
33124 KB |
Output is correct |
43 |
Correct |
96 ms |
32204 KB |
Output is correct |
44 |
Correct |
109 ms |
33052 KB |
Output is correct |
45 |
Correct |
120 ms |
33180 KB |
Output is correct |
46 |
Correct |
90 ms |
38228 KB |
Output is correct |
47 |
Correct |
107 ms |
33280 KB |
Output is correct |
48 |
Correct |
18 ms |
17500 KB |
Output is correct |
49 |
Correct |
172 ms |
36796 KB |
Output is correct |
50 |
Correct |
179 ms |
36792 KB |
Output is correct |
51 |
Correct |
182 ms |
36796 KB |
Output is correct |
52 |
Correct |
178 ms |
36552 KB |
Output is correct |
53 |
Correct |
194 ms |
36348 KB |
Output is correct |
54 |
Correct |
131 ms |
31456 KB |
Output is correct |
55 |
Correct |
177 ms |
37556 KB |
Output is correct |
56 |
Correct |
179 ms |
36896 KB |
Output is correct |
57 |
Correct |
178 ms |
40864 KB |
Output is correct |
58 |
Correct |
207 ms |
37560 KB |
Output is correct |
59 |
Correct |
64 ms |
21584 KB |
Output is correct |
60 |
Correct |
176 ms |
36816 KB |
Output is correct |
61 |
Correct |
168 ms |
36880 KB |
Output is correct |
62 |
Correct |
191 ms |
37496 KB |
Output is correct |
63 |
Correct |
181 ms |
41404 KB |
Output is correct |
64 |
Correct |
4 ms |
13912 KB |
Output is correct |
65 |
Correct |
4 ms |
13916 KB |
Output is correct |
66 |
Correct |
5 ms |
13916 KB |
Output is correct |
67 |
Correct |
4 ms |
13916 KB |
Output is correct |
68 |
Correct |
4 ms |
13916 KB |
Output is correct |
69 |
Incorrect |
5 ms |
13912 KB |
Output isn't correct |
70 |
Halted |
0 ms |
0 KB |
- |