题目链接 https://cn.vjudge.net/problem/UVA-11354
【题意】
给定
n
n
n个点,
m
m
m条边的无向图,每条边有一个权值。要求回答若干组询问,每个询问包含一个起点
s
s
s和一个终点
t
t
t,要求找到一条从
s
s
s到
v
v
v的路径,使得途径所有边的最大边权最小
n
<
=
50000
,
m
<
=
100000
n<=50000,m<=100000
n<=50000,m<=100000
【思路】
题目中问的其实也就是
s
s
s 到
t
t
t 的最小瓶颈路,但是不能用传统方法做,首先求出最小生成树然后把它变成有根树,令
p
a
r
e
n
t
[
i
]
[
j
]
parent[i][j]
parent[i][j]表示节点
i
i
i 向上走
2
j
2^j
2j 步到达的节点,
c
o
s
t
[
i
]
[
j
]
cost[i][j]
cost[i][j] 记录这一过程中经过所有边的最大边权,
d
e
p
t
h
[
i
]
depth[i]
depth[i] 记录节点
i
i
i 的深度,那么可以有如下状态转移
p
a
r
e
n
t
[
i
]
[
j
]
=
p
a
r
e
n
t
[
p
a
r
e
n
t
[
i
]
[
j
−
1
]
]
[
j
−
1
]
parent[i][j]=parent[parent[i][j-1]][j-1]
parent[i][j]=parent[parent[i][j−1]][j−1]
c
o
s
t
[
i
]
[
j
]
=
m
a
x
{
c
o
s
t
[
i
]
[
j
−
1
]
,
c
o
s
t
[
p
a
r
e
n
t
[
i
]
[
j
−
1
]
]
[
j
−
1
]
}
cost[i][j]=max\{cost[i][j-1],cost[parent[i][j-1]][j-1]\}
cost[i][j]=max{cost[i][j−1],cost[parent[i][j−1]][j−1]}
递推的边界是
p
a
r
e
n
t
[
i
]
[
0
]
和
c
o
s
t
[
i
]
[
0
]
parent[i][0] 和 cost[i][0]
parent[i][0]和cost[i][0] 它们以及
d
e
p
t
h
[
i
]
depth[i]
depth[i] 都可以通过一次树上的dfs处理出来。之后查询函数就和求LCA类似,先让不同两个节点
u
,
v
u,v
u,v 到达同一深度,然后共同向上走寻找LCA,在这个过程中不断更新答案
#include<bits/stdc++.h>
using namespace std;
const int inf=2e9;
const int maxn=50005;
const int logn=20;
struct Edge{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d){}
bool operator<(const Edge& e)const{
return dist<e.dist;
}
};
int n,m,q;
int par[maxn];
vector<Edge> edges,g[maxn];
int find(int x){ return par[x]==x?x:par[x]=find(par[x]); }
void kruscal(){
for(int i=0;i<n;++i){ par[i]=i;g[i].clear(); }
sort(edges.begin(),edges.end());
int cnt=0;
for(int i=0;i<m;++i){
Edge& e=edges[i];
int x=find(e.from);
int y=find(e.to);
if(x!=y){
par[x]=y;
g[e.from].push_back(Edge(e.from,e.to,e.dist));
g[e.to].push_back(Edge(e.to,e.from,e.dist));
if(++cnt==n-1) break;
}
}
}
int depth[maxn];
int parent[maxn][logn];
int cost[maxn][logn];
void dfs(int u,int fa,int d,int w){
depth[u]=d;
parent[u][0]=fa;
cost[u][0]=w;
for(int i=0;i<g[u].size();++i){
Edge& e=g[u][i];
int v=e.to;
if(v!=fa) dfs(v,u,d+1,e.dist);
}
}
void init(){
dfs(0,-1,0,0);
for(int j=1;(1<<j)<n;++j){
for(int i=0;i<n;++i){
int a=parent[i][j-1];
if(a==-1){
parent[i][j]=-1;
cost[i][j]=0;
}
else{
parent[i][j]=parent[a][j-1];
cost[i][j]=max(cost[i][j-1],cost[a][j-1]);
}
}
}
}
int query(int u,int v){
int ans=-inf,log=0;
if(depth[u]<depth[v]) swap(u,v);
while( (1<<(log+1)) <= depth[u]) ++log;
for(int k=log;k>=0;--k){
if((depth[u]-depth[v])>>k&1){
ans=max(ans,cost[u][k]);
u=parent[u][k];
}
}
if(u==v) return ans;
for(int k=log;k>=0;--k){
if(parent[u][k]!=-1 && parent[u][k]!=parent[v][k]){
ans=max(ans,cost[u][k]);
ans=max(ans,cost[v][k]);
u=parent[u][k];
v=parent[v][k];
}
}
ans=max(ans,cost[u][0]);
ans=max(ans,cost[v][0]);
return ans;
}
int main(){
int kase=0;
while(scanf("%d%d",&n,&m)==2){
if(kase) puts("");
kase=1;
edges.clear();
for(int i=0;i<m;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edges.push_back(Edge(u-1,v-1,w));
}
kruscal();
init();
scanf("%d",&q);
while(q--){
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",query(u-1,v-1));
}
}
return 0;
}