#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 ((cnt[u] >= 3 || cnt[v] >= 3) && !mk[it]) {
mk[it] = 1;
weight[it] = w;
}
if (root(u) == root(v)) {
if (!mk[it]) {
mk[it] = 1;
weight[it] = w;
}
continue;
}
add(u, v);
it += 1;
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
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
5 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
70 ms |
28500 KB |
Output is correct |
10 |
Correct |
96 ms |
31568 KB |
Output is correct |
11 |
Correct |
111 ms |
31572 KB |
Output is correct |
12 |
Correct |
104 ms |
31828 KB |
Output is correct |
13 |
Correct |
87 ms |
35676 KB |
Output is correct |
14 |
Correct |
81 ms |
28848 KB |
Output is correct |
15 |
Correct |
164 ms |
33472 KB |
Output is correct |
16 |
Correct |
168 ms |
33280 KB |
Output is correct |
17 |
Correct |
169 ms |
33688 KB |
Output is correct |
18 |
Correct |
192 ms |
37792 KB |
Output is correct |
19 |
Correct |
62 ms |
20288 KB |
Output is correct |
20 |
Correct |
168 ms |
34508 KB |
Output is correct |
21 |
Correct |
159 ms |
34824 KB |
Output is correct |
22 |
Correct |
167 ms |
34904 KB |
Output is correct |
23 |
Correct |
172 ms |
38848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
156 ms |
41972 KB |
Output is correct |
4 |
Correct |
154 ms |
42568 KB |
Output is correct |
5 |
Correct |
161 ms |
42256 KB |
Output is correct |
6 |
Correct |
154 ms |
42388 KB |
Output is correct |
7 |
Correct |
153 ms |
42448 KB |
Output is correct |
8 |
Correct |
148 ms |
41984 KB |
Output is correct |
9 |
Correct |
155 ms |
42328 KB |
Output is correct |
10 |
Correct |
151 ms |
41848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
5 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
3 ms |
13916 KB |
Output is correct |
11 |
Correct |
5 ms |
13916 KB |
Output is correct |
12 |
Correct |
5 ms |
13956 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
4 ms |
14028 KB |
Output is correct |
15 |
Incorrect |
3 ms |
13916 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
4 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
5 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
70 ms |
28500 KB |
Output is correct |
11 |
Correct |
96 ms |
31568 KB |
Output is correct |
12 |
Correct |
111 ms |
31572 KB |
Output is correct |
13 |
Correct |
104 ms |
31828 KB |
Output is correct |
14 |
Correct |
87 ms |
35676 KB |
Output is correct |
15 |
Correct |
3 ms |
13916 KB |
Output is correct |
16 |
Correct |
5 ms |
13916 KB |
Output is correct |
17 |
Correct |
5 ms |
13956 KB |
Output is correct |
18 |
Correct |
3 ms |
13916 KB |
Output is correct |
19 |
Correct |
4 ms |
14028 KB |
Output is correct |
20 |
Incorrect |
3 ms |
13916 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
4 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
5 ms |
13916 KB |
Output is correct |
8 |
Correct |
3 ms |
13916 KB |
Output is correct |
9 |
Correct |
70 ms |
28500 KB |
Output is correct |
10 |
Correct |
96 ms |
31568 KB |
Output is correct |
11 |
Correct |
111 ms |
31572 KB |
Output is correct |
12 |
Correct |
104 ms |
31828 KB |
Output is correct |
13 |
Correct |
87 ms |
35676 KB |
Output is correct |
14 |
Correct |
81 ms |
28848 KB |
Output is correct |
15 |
Correct |
164 ms |
33472 KB |
Output is correct |
16 |
Correct |
168 ms |
33280 KB |
Output is correct |
17 |
Correct |
169 ms |
33688 KB |
Output is correct |
18 |
Correct |
192 ms |
37792 KB |
Output is correct |
19 |
Correct |
156 ms |
41972 KB |
Output is correct |
20 |
Correct |
154 ms |
42568 KB |
Output is correct |
21 |
Correct |
161 ms |
42256 KB |
Output is correct |
22 |
Correct |
154 ms |
42388 KB |
Output is correct |
23 |
Correct |
153 ms |
42448 KB |
Output is correct |
24 |
Correct |
148 ms |
41984 KB |
Output is correct |
25 |
Correct |
155 ms |
42328 KB |
Output is correct |
26 |
Correct |
151 ms |
41848 KB |
Output is correct |
27 |
Correct |
3 ms |
13916 KB |
Output is correct |
28 |
Correct |
5 ms |
13916 KB |
Output is correct |
29 |
Correct |
5 ms |
13956 KB |
Output is correct |
30 |
Correct |
3 ms |
13916 KB |
Output is correct |
31 |
Correct |
4 ms |
14028 KB |
Output is correct |
32 |
Incorrect |
3 ms |
13916 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13916 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
4 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
5 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13916 KB |
Output is correct |
10 |
Correct |
70 ms |
28500 KB |
Output is correct |
11 |
Correct |
96 ms |
31568 KB |
Output is correct |
12 |
Correct |
111 ms |
31572 KB |
Output is correct |
13 |
Correct |
104 ms |
31828 KB |
Output is correct |
14 |
Correct |
87 ms |
35676 KB |
Output is correct |
15 |
Correct |
81 ms |
28848 KB |
Output is correct |
16 |
Correct |
164 ms |
33472 KB |
Output is correct |
17 |
Correct |
168 ms |
33280 KB |
Output is correct |
18 |
Correct |
169 ms |
33688 KB |
Output is correct |
19 |
Correct |
192 ms |
37792 KB |
Output is correct |
20 |
Correct |
156 ms |
41972 KB |
Output is correct |
21 |
Correct |
154 ms |
42568 KB |
Output is correct |
22 |
Correct |
161 ms |
42256 KB |
Output is correct |
23 |
Correct |
154 ms |
42388 KB |
Output is correct |
24 |
Correct |
153 ms |
42448 KB |
Output is correct |
25 |
Correct |
148 ms |
41984 KB |
Output is correct |
26 |
Correct |
155 ms |
42328 KB |
Output is correct |
27 |
Correct |
151 ms |
41848 KB |
Output is correct |
28 |
Correct |
3 ms |
13916 KB |
Output is correct |
29 |
Correct |
5 ms |
13916 KB |
Output is correct |
30 |
Correct |
5 ms |
13956 KB |
Output is correct |
31 |
Correct |
3 ms |
13916 KB |
Output is correct |
32 |
Correct |
4 ms |
14028 KB |
Output is correct |
33 |
Incorrect |
3 ms |
13916 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |