[动态树 LCT] BZOJ 2594 [Wc2006]水管局长数据加强版

动态树模板题

最开始加边用Kruskal比较快,不用LCT,然后再用LCT维护最小生成树

话说这道题小数据范围的正解是什么....

PS:以前LCT的求根都是打错的,没有pushdown()


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> abcd;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int n,m;

int u[1000005],v[1000005],w[1000005],ivst[1000005];

struct Splay{
	struct node{
		int size,idx,rev;
		int val,maximum;
		node *p,*ch[2],*fat,*mpos;
		inline void setc(node *c,int d) { ch[d]=c; c->p=this; }
		inline bool dir() { return p->ch[1]==this; }
		inline void update() {
			size=ch[0]->size+ch[1]->size+1;
			maximum=val; mpos=this;
			if (ch[0]->maximum>maximum)
				maximum=ch[0]->maximum,mpos=ch[0]->mpos;
			if (ch[1]->maximum>maximum)
				maximum=ch[1]->maximum,mpos=ch[1]->mpos;
		}
		inline void reverse() { rev^=1; swap(ch[0],ch[1]); }
		inline void pushdown(node *null){
			if (rev){
				if (ch[0]!=null) ch[0]->reverse();
				if (ch[1]!=null) ch[1]->reverse();
				rev=0;
			}
		}
	}*null,Mem[1200005];
	Splay() { null=Mem; null->p=null->ch[0]=null->ch[1]=null->fat=null; null->size=0; null->maximum=-(1<<30)-(1<<29); null->mpos=null; }
	inline void rot(node *x){
		if (x==null || x->p==null) return ;
		bool d=x->dir(); node *p=x->p;
		if (p->p!=null) p->p->setc(x,p->dir()); else x->p=null;
		p->setc(x->ch[d^1],d); x->setc(p,d^1); p->update(); x->update(); swap(x->fat,p->fat);
	}
	node *sta[1200005];
	inline void splay(node *x){
		int pnt=0; node *y=x;
		while (y!=null) sta[++pnt]=y,y=y->p;
		for (int i=pnt;i;i--) sta[i]->pushdown(null);
		while (x->p!=null)
			if (x->p->p==null)
				rot(x);
			else
				x->dir()==x->p->dir()?(rot(x->p),rot(x)):(rot(x),rot(x));
	}
	inline node *Access(node *x){
		node *y=null;
		while (x!=null)
		{
			splay(x);
			x->ch[1]->p=null; x->ch[1]->fat=x;
			x->setc(y,1); y->fat=null;
			x->update();
			y=x; x=x->fat;
		}
		return y;
	}
	inline void Link(node *x,node *y){
		if (Jud(x,y)) return;
		Access(x)->reverse();
		splay(x);
		x->fat=y;
		Access(x);
	}
	inline void Cut(node *x){
		Access(x);
		splay(x);
		x->ch[0]->p=null; x->ch[0]=null;
		x->fat=null; x->update();
	}
	inline void Cut(node *x,node *y){
		Access(x)->reverse();
		Cut(y);
	}
	inline node *Root(node *x){
		Access(x);
		splay(x);
		node *y=x;
		while (y->ch[0]!=null) y->pushdown(null),y=y->ch[0];
		return y;
	}
	inline bool Jud(node *x,node *y){
		return Root(x)==Root(y);
	}
	inline int Query(node *x,node *y){
		Access(x)->reverse();
		return Access(y)->maximum;
	}
	inline node *Road(node *x,node *y){
		Access(x)->reverse();
		return Access(y)->mpos;
	}
}LCT;

Splay::node *pos[1200005];

inline void Init(){
	for (int i=1;i<=n+m;i++){
		pos[i]=LCT.Mem+i; 
		pos[i]->p=pos[i]->ch[0]=pos[i]->ch[1]=pos[i]->fat=LCT.null; 
		pos[i]->val=-1<<30; pos[i]->maximum=-1<<30; pos[i]->mpos=pos[i];
		pos[i]->idx=i; pos[i]->size=1;
	}
}

struct event{
	int f,u,v;
	int idx;
	bool operator < (const event &B) const{
		return w[idx]<w[B.idx];
	}
}eve[1200005];
int tot,icnt,pnt;

int ans[100005];
map<abcd,int> Map;

int fat[100005];
inline int getfat(int u){
	return u==fat[u]?u:fat[u]=getfat(fat[u]);
}

inline void Kru()
{
	int fx,fy,itot=0;
	for (int i=1;i<=n;i++) fat[i]=i;
	for (int i=1;i<=m;i++)
		if (!ivst[i])
			eve[++tot].f=2,eve[tot].u=u[i],eve[tot].v=v[i],eve[tot].idx=i;
	sort(eve+pnt+1,eve+tot+1);
	for (int i=pnt+1;i<=tot;i++)
	{
		fx=getfat(eve[i].u); fy=getfat(eve[i].v);
		if (fx!=fy)
		{
			fat[fx]=fy;
			LCT.Link(pos[eve[i].u],pos[eve[i].idx+n]);
			LCT.Link(pos[eve[i].v],pos[eve[i].idx+n]);
			itot++;
			if (itot==n-1) break;
		}
	}
}

int main()
{
	int iu,iv,Q;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(m); read(Q);
	Init();
	for (int i=1;i<=m;i++)
	{
		read(u[i]),read(v[i]),read(w[i]);
		pos[i+n]->val=w[i]; pos[i+n]->update();
		if (u[i]>v[i]) swap(u[i],v[i]);
		Map.insert(make_pair(abcd(u[i],v[i]),i));
	}
	for (int i=1;i<=Q;i++)
	{
		read(eve[++tot].f); read(eve[tot].u); read(eve[tot].v);
		if (eve[tot].u>eve[tot].v) swap(eve[tot].u,eve[tot].v);
		if (eve[tot].f==2)
			ivst[eve[tot].idx=Map[abcd(eve[tot].u,eve[tot].v)]]=1;
		else if (eve[tot].f==1)
			eve[tot].idx=++icnt;
	}
	pnt=tot;
	Kru();
	for (int i=pnt;i;i--)
		if (eve[i].f==1)
		{
			ans[eve[i].idx]=LCT.Query(pos[eve[i].u],pos[eve[i].v]);
		}
		else
		{
			iu=eve[i].u; iv=eve[i].v;
			Splay::node *road=LCT.Road(pos[iu],pos[iv]);
			if (road->val>w[eve[i].idx])
			{
				LCT.Cut(road,pos[u[road->idx-n]]);
				LCT.Cut(road,pos[v[road->idx-n]]);
				LCT.Link(pos[n+eve[i].idx],pos[iu]);
				LCT.Link(pos[n+eve[i].idx],pos[iv]);
			}
		}
	for (int i=1;i<=icnt;i++)
		printf("%d\n",ans[i]);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值