题意:给出n个点和m条边的无向图 对于每次询问给出a b,求a与b之间的最长边的最小值
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define lim 30500
using namespace std;
int n,m,k,tot=0;
int nextt[lim],head[lim],v[lim],c[lim],fa[lim],f[lim][31],depth[lim],maxx[lim][31];
struct edge{int xx,yy,zz;}a[lim];
void add_edge(int x,int y,int z){v[++tot]=y;c[tot]=z;nextt[tot]=head[x];head[x]=tot;}
bool cmp(edge p,edge q){return p.zz<q.zz;}
int getfa(int x){return x==fa[x]?x:fa[x]=getfa(fa[x]);}
void kruskal()
{
int cur=0;
sort(a,a+m,cmp);
for (int i=0;i<m;i++)
{
int p=getfa(a[i].xx),q=getfa(a[i].yy);
if (p!=q)
{
fa[p]=q;cur++;
add_edge(a[i].xx,a[i].yy,a[i].zz);
add_edge(a[i].yy,a[i].xx,a[i].zz);
if (cur==n-1) break;
}
}
}
void dfs(int x,int g)
{
for(int i=head[x];i;i=nextt[i])
{
int y=v[i];
if (y!=g)
{
depth[y]=depth[x]+1;
f[y][0]=x;maxx[y][0]=c[i];
for (int j=1;j<31;j++) f[y][j]=f[f[y][j-1]][j-1],maxx[y][j]=max(maxx[y][j-1],maxx[f[y][j-1]][j-1]);
dfs(y,x);
}
}
}
int lca(int x,int y)
{
if (depth[x]<depth[y]) swap(x,y);
int d=depth[x]-depth[y];
for (int i=0;i<31;i++)
if (d&(1<<i)) x=f[x][i];
for (int i=30;i>=0;i--)
if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return x==y?x:f[x][0];
}
int query(int x,int r)
{
int d=depth[x]-depth[r],ret=0;
for (int i=0;i<31;i++)
if (d&(1<<i)) ret=max(maxx[x][i],ret),x=f[x][i];
return ret;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=0;i<m;i++)
scanf("%d%d%d",&a[i].xx,&a[i].yy,&a[i].zz);
kruskal();
dfs(1,0);
for (int i=0;i<k;i++)
{
int x,y,r;
scanf("%d%d",&x,&y);r=lca(x,y);
printf("%d\n",max(query(x,r),query(y,r)));
}
}