传送门
题解:先求出MST,然后在跑倍增LCA时顺便维护一个dp数组记录路径上的最大边权。
P.S.WA了好久,因为LCA跳最后一步没取max。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=15004;
int n,m,q;
int head[MAXN],edge=0;
struct Kruskal {
int u,v,w;
bool operator <(const Kruskal &s) const {
return w<s.w;
}
}d[MAXN<<1];
struct EDGE {
int v,nxt,w;
}e[MAXN<<1];
int fa[MAXN],dis[MAXN],f[16][MAXN],dp[16][MAXN];
inline int read() {
int x=0;char c=getchar();
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x;
}
inline void adde(int u,int v,int w) {
e[edge].nxt=head[u],e[edge].v=v,e[edge].w=w,head[u]=edge++;
e[edge].nxt=head[v],e[edge].v=u,e[edge].w=w,head[v]=edge++;
}
void dfs(int p,int father) {
for (int i=head[p];~i;i=e[i].nxt) {
int v=e[i].v;
if (v^father) {
dis[v]=dis[p]+1;
f[0][v]=p;
dp[0][v]=e[i].w;
dfs(v,p);
}
}
}
inline int find(int x) {
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void da() {
for (int j=1;(1<<j)<=n;++j)
for (int i=1;i<=n;++i)
f[j][i]=f[j-1][f[j-1][i]],dp[j][i]=max(dp[j-1][i],dp[j-1][f[j-1][i]]);
}
int lca(int x,int y) {
int mx=0;
if (dis[x]<dis[y]) x^=y^=x^=y;
int t=dis[x]-dis[y];
for (int i=0;i<=14;++i)
if (t&(1<<i)) mx=max(mx,dp[i][x]),x=f[i][x];
if (x==y) return mx;
for (int i=14;~i;--i)
if (f[i][x]^f[i][y]) mx=max(mx,max(dp[i][x],dp[i][y])),x=f[i][x],y=f[i][y];
return max(mx,max(dp[0][x],dp[0][y]));
}
int main() {
// freopen("bzoj 3732.in","r",stdin);
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
n=read(),m=read(),q=read();
for (int i=1;i<=n;++i) fa[i]=i;
for (int i=0;i<m;++i) d[i].u=read(),d[i].v=read(),d[i].w=read();
sort(d,d+m);
int cnt=n;
for (int i=0;i<m&&cnt>1;++i) {
int u=d[i].u,v=d[i].v;
int uu=find(u),vv=find(v);
if (uu^vv) {
fa[uu]=vv;
--cnt;
adde(u,v,d[i].w);
}
}
dfs(1,0);
da();
while (q--) {
int u=read(),v=read();
printf("%d\n",lca(u,v));
}
return 0;
}