BZOJ4668 冷战

其实就是维护一下最小生成树上的链上边权最大值,而且加入的边权还是递增的,随便写个LCT不就过了

TLE了,妈蛋

这题有个特殊的性质就是加入的边权就是递增的,所以连接两个连通块之前两个连通块里的边权都小于这个边的边权,这样的话其实只要把这两个连通块连起来,连哪两个点对答案是没影响的,所以按秩合并就行了

按秩合并:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
using namespace std;
#define MAXN 500010
#define MAXM 1010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
int f[MAXN],h[MAXN],v[MAXN],dep[MAXN];
int la;
int T;
int n,m;
int fa(int x){
	return f[x]==x?x:fa(f[x]);
}
void pre(int x){
	if(f[x]==x){
		return ;
	}
	pre(f[x]);
	dep[x]=dep[f[x]]+1;
}
int ask(int x,int y){
	pre(x);
	pre(y);
	if(dep[x]<dep[y]){
		swap(x,y);
	}
	int re=0;
	while(dep[x]>dep[y]&&x!=y){
		re=max(re,v[x]);
		x=f[x];
	}
	while(x!=y){
		re=max(re,max(v[x],v[y]));
		x=f[x];
		y=f[y];
	}
	return re;
}
int main(){
	int i,o,x,y;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		f[i]=i;
	}
	while(m--){
		scanf("%d%d%d",&o,&x,&y);
		x^=la;
		y^=la;
		if(o==0){
			int fx=fa(x),fy=fa(y);
			T++;
			if(fx!=fy){
				if(h[fx]<=h[fy]){
					f[fx]=fy;
					v[fx]=T;
					if(h[fx]==h[fy]){
						h[fy]++;
					}
				}else{
					f[fy]=fx;
					v[fy]=T;
				}
			}
		}
		if(o==1){
			int fx=fa(x),fy=fa(y);
			if(fx!=fy){
				printf("%d\n",la=0);
			}else{
				printf("%d\n",la=ask(x,y));
			}
		}
	}
	return 0;
}

/*

*/
LCT(TLE):

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
using namespace std;
#define MAXN 500010
#define MAXM 1010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
int fa[MAXN],son[MAXN][2],mx[MAXN],vu[MAXN],vd[MAXN],vl[MAXN],vr[MAXN];
bool rev[MAXN];
int st[MAXN],tp;
int r[MAXN];
int n,m;
int T;
int la;
int R(int x){
	return r[x]==x?x:r[x]=R(r[x]);
}
inline void ud(int x){
	mx[x]=max(max(vu[x],vd[x]),max(mx[son[x][0]],mx[son[x][1]]));
	vl[x]=vu[x];
	vr[x]=vd[x];
	if(son[x][0]){
		vl[x]=vl[son[x][0]];
	}
	if(son[x][1]){
		vr[x]=vr[son[x][1]];
	}
}
inline void torev(int x){
	if(!x){
		return ;
	}
	rev[x]^=1;
	swap(son[x][0],son[x][1]);
	swap(vu[x],vd[x]);
	swap(vl[x],vr[x]);
}
inline void pd(int x){
	if(rev[x]){
		torev(son[x][0]);
		torev(son[x][1]);
		rev[x]=0;
	}
}
inline bool ir(int x){
	return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
inline void cot(int x,int y,int z){
	if(x){
		fa[x]=y;
	}
	if(y){
		son[y][z]=x;
	}
}
inline void rot(int x,bool z){
	int xx=fa[x],xxx=fa[xx];
	cot(son[x][z],xx,z^1);
	if(!ir(xx)){
		cot(x,xxx,son[xxx][1]==xx);
	}else{
		fa[x]=xxx;
	}
	cot(xx,x,z);
	ud(xx);
}
inline void apd(int x){
	int i;
	st[++tp]=x;
	for(i=x;!ir(i);i=fa[i]){
		st[++tp]=fa[i];
	}
	for(;tp;tp--){
		pd(st[tp]);
	}
}
void splay(int x){
	apd(x);
	while(!ir(x)){
		int xx=fa[x],xxx=fa[xx];
		if(ir(xx)){
			rot(x,son[xx][0]==x);
		}else{
			bool z=son[xxx][0]==xx;
			if(son[xx][z]==x){
				rot(x,z^1);
				rot(x,z);
			}else{
				rot(xx,z);
				rot(x,z);
			}
		}
	}
	ud(x);
}
inline void acs(int x){
	int t=0;
	while(x){
		
		splay(x);
		son[x][1]=t;
		vd[x]=vl[t];
		ud(x);
		t=x;
		x=fa[x];
	}
}
inline void reboot(int x){
	acs(x);
	splay(x);
	torev(x);
}
inline void link(int x,int y,int z){
	reboot(x);
	reboot(y);
	fa[x]=y;
	vu[x]=z;
	ud(x);
}
int main(){
	int i,o,x,y;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		r[i]=i;
	}
	while(m--){
		scanf("%d%d%d",&o,&x,&y);
		if(o==0){
			T++;
			if(R(x)!=R(y)){
				r[R(x)]=R(y);
				link(x,y,T);
			}
		}
		if(o==1){
			if(R(x)!=R(y)){
				printf("%d\n",la=0);
			}else{
				reboot(x);
				acs(y);
				splay(y);
				printf("%d\n",la=mx[y]);
			}
		}
	}
	return 0;
}

/*

*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值