問題#
トラック輸送#
A 国には $n$ の都市があり、番号は $1$ から $n$ までです。都市間には $m$ 本の双方向道路があります。各道路には車両に対する重量制限があり、これを限重と呼びます。
現在、$q$ 台のトラックが貨物を輸送しており、ドライバーたちは各トラックが車両の限重を超えない範囲で、最大でどれだけの貨物を運べるかを知りたいと考えています。
入力形式#
最初の行には、空白で区切られた 2 つの整数 $n,m$ があり、A 国には $n$ の都市と $m$ 本の道路があることを示します。
次の $m$ 行には、各行に 3 つの整数 $x, y, z$ があり、各整数の間は空白で区切られています。これは、$x$ 番の都市から $y$ 番の都市に限重 $z$ の道路があることを示します。
注意: $x \neq y$ であり、2 つの都市間には複数の道路が存在する可能性があります。
次の行には、整数 $q$ があり、$q$ 台のトラックが貨物を運ぶ必要があることを示します。
次の $q$ 行には、各行に 2 つの整数 $x,y$ があり、空白で区切られています。これは、1 台のトラックが $x$ 都市から $y$ 都市に貨物を輸送する必要があることを示します。$x \neq y$ が保証されています。
$100%$ のデータに対して、$1 \le n < 10^4$、$1 \le m < 5\times 10^4$、$1 \le q< 3\times 10^4 $、$0 \le z \le 10^5$ です。
解答#
経路上の最小値を最大化する必要があるため、 ~ 二分(もちろん可能ですが、私はできません QAQ)~ 最大生成木を求め、その後、各クエリの 2 点間の lca を使って経路上の最小値を求めます。
具体的な実装は lca に似ています。
問題#
-
双方向の辺を構築する
-
并查集
fa[/*!!!*/x_fa] = y;
-
125 行目の min を anc に書き換えた
コード#
#include<bits/stdc++.h>
using namespace std;
int read()
{
int f = 1, x = 0;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
const int N = 1e4 + 10, M = 1e5 + 100;
struct U
{
int x, y, z;
bool operator < (const U o) const
{
return z > o.z;
}
}u[M >> 1]; int cnt = 1;
int fa[N], n, m;
int anc[N][20], path_min[N][20], depth[N];
int getfa(int u)
{
if(fa[u] == u) return u;
return fa[u] = getfa(fa[u]);
}
void merge(int x, int y)
{
int x_fa = getfa(x), y_fa = getfa(y);
fa[/*!!!*/x_fa] = y;
}
int head[N], nxt[M], to[M], edge[M], tot = 1;
void add(int x, int y, int z)
{
to[++tot] = y;
edge[tot] = z;
nxt[tot] = head[x];
head[x] = tot;
}
void dfs(int u, int fa)
{
depth[u] = depth[fa] + 1;
anc[u][0] = fa;
for(int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if(v == fa) continue;
path_min[v][0] = edge[i];
dfs(v, u);
}
}
void lca_init()
{
for(int j = 1;j < 20;j++)
{
for(int i = 1;i <= n;i++)
{
anc[i][j] = anc[anc[i][j - 1]][j - 1];
path_min[i][j] = min(path_min[i][j - 1], path_min[anc[i][j - 1]][j - 1]);
}
}
}
int lca_query_anc(int x, int y)
{
if(depth[x] < depth[y])
swap(x, y);
for(int i = 19;i >= 0;i--)
{
if(depth[anc[x][i]] >= depth[y])
x = anc[x][i];
}
if(x == y) return x;
for(int i = 19;i >= 0;i--)
{
if(anc[x][i] != anc[y][i])
x = anc[x][i], y = anc[y][i];
}
return anc[x][0];
}
int lca_query_min(int x, int y)
{
int ans = 0x3f3f3f3f;
if(depth[x] < depth[y])
swap(x, y);
for(int i = 19;i >= 0;i--)
{
if(depth[anc[x][i]] >= depth[y])
{
ans = min(ans, path_min[x][i]); x = anc[x][i];
}
}
if(x == y) return ans;
for(int i = 19;i >= 0;i--)
{
if(anc[x][i] != anc[y][i])
{
ans = min(ans, path_min[x][i]), ans = min(ans, path_min[y][i]);
x = anc[x][i], y = anc[y][i];
}
}
return min(ans, min(path_min[x][0], path_min[y][0]));
}
int main()
{
n = read(), m = read();
for(int i = 1;i <= n;i++)
{
fa[i] = i;
}
int x, y, z;
for(int i = 1;i <= m;i++)
{
x = read(), y = read(), z = read();
u[cnt].x = x;
u[cnt].y = y, u[cnt].z = z;
++cnt;
}
sort(u + 1, u + 1 + m);
for(int i = 1;i <= m;i++)
{
int x_fa = getfa(u[i].x), y_fa = getfa(u[i].y);
if(x_fa != y_fa)
{
merge(u[i].x, u[i].y);
add(u[i].x, u[i].y, u[i].z);
add(u[i].y, u[i].x, u[i].z);
}
}
for(int i = 1;i <= n;i++)
{
if(fa[i] == i)
{
dfs(i, 0);
}
}
lca_init();
int q;
q = read();
// for(int i = 1;i <= n;i++)
// {
// cout << "qwq "<<path_min[i][2] << endl;
// }
while(q--)
{
x = read(), y = read();
int x_fa = getfa(x), y_fa = getfa(y);
if(x_fa != y_fa)
{
printf("-1\n");
continue;
}
cout << lca_query_min(x, y) << endl;
}
}