BZOJ3732 LCA

倍增同时维护链上最大

#include
using namespace std;
const int N=1e5+10;
int head[N],Next[N],ver[N],edge[N],mf[N][20],f[N][20],d[N],fa[N],tot,t;
queue q;
struct P{
	int x,y,z;
}A[N];
bool cmp(P a,P b){
	return a.zd[y])swap(x,y);
	int val=0;
	for(int i=t;i>=0;--i)
		if(d[f[y][i]]>=d[x])val=max(val,mf[y][i]),y=f[y][i];
	//cout<=0;--i){
		if(f[x][i]!=f[y][i])val=max(val,max(mf[x][i],mf[y][i])),x=f[x][i],y=f[y][i];
	}
	return max(val,max(mf[x][0],mf[y][0]));
}
int main(){
	//freopen("1.in","r",stdin);
	int n,m,k,x,y;
	scanf("%d%d%d",&n,&m,&k);
	t=int(log(n)/log(2))+1;
	for(int i=1;i<=m;++i)scanf("%d%d%d",&A[i].x,&A[i].y,&A[i].z);
	sort(A+1,A+1+m,cmp);
	for(int i=1;i<=n;++i)fa[i]=i;
	int ans=0;
	for(int i=1;i<=m;++i){
		int x=get(A[i].x);int y=get(A[i].y);
		if(x==y)continue;
		fa[x]=y;
		add(A[i].x,A[i].y,A[i].z);
		add(A[i].y,A[i].x,A[i].z);
		++ans;
	}
	bfs();
	for(int i=1;i<=k;++i){
		scanf("%d%d",&x,&y);
		printf("%d\n",lca(x,y));
	}
}

 

你可能感兴趣的:(数据结构,图论)