zoukankan      html  css  js  c++  java
  • 【洛谷4172】 [WC2006]水管局长(LCT)

    传送门

    洛谷
    BZOJ

    Solution

    如果不需要动态的话,那就是一个裸的最小生成树上的最大边权对吧。
    现在动态了的话,把这个过程反着来,就是加边对吧。
    现在问题变成了怎么动态维护加边的最小生成树,这是一个比较常见的套路了,使用LCT(暴力)。。。
    深刻理解一下Kruscal的过程,就是每一次选不在连通块内的最小边权。
    那么我们如果加了一条边,显然会形成一个环,我们只要删除这个环里面最大的边权就好了。
    所以LCT动态维护路径上的最大值然后根据情况(Cut*2+Link*2)就好了。。。

    代码实现

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<queue>
    #include<set>
    #include<map>
    #include<iostream>
    using namespace std;
    #define re register
    #define ll long long
    inline int gi()
    {
    	int f=1,sum=0;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    typedef pair<int,int> pii;
    #define mp make_pair
    const int N=100010,Mm=1000010;
    struct edge{int u,v,w;bool operator<(const edge &b)const{return w<b.w;}}e[Mm];
    struct ques{int opt,x,y;}q[Mm];
    struct node{int ff,ch[2],rev,mx,val;}t[(N+Mm)<<1];
    map<pii,int>M;
    int n,m,Q,Cut[Mm],ans[Mm];
    void init(int id,int val){t[id].ch[0]=t[id].ch[1]=0;t[id].rev=0;t[id].ff=0;t[id].val=t[id].mx=val;}
    namespace LinkCutTree
    {
    	int sta[(N+Mm)<<1],top;
    	void pushup(int x)
    	{
    		t[x].mx=t[x].val;
    		if(e[t[x].mx].w<e[t[t[x].ch[0]].mx].w)t[x].mx=t[t[x].ch[0]].mx;
    		if(e[t[x].mx].w<e[t[t[x].ch[1]].mx].w)t[x].mx=t[t[x].ch[1]].mx;
    	}
    	void reverse(int x){t[x].rev^=1;swap(t[x].ch[0],t[x].ch[1]);}
    	void pushdown(int x){if(!t[x].rev)return;if(t[x].ch[0])reverse(t[x].ch[0]);if(t[x].ch[1])reverse(t[x].ch[1]);t[x].rev^=1;}
    	bool isroot(int x){return (t[t[x].ff].ch[0]!=x)&(t[t[x].ff].ch[1]!=x);}
    	void rotate(int x)
    	{
    		int y=t[x].ff,z=t[y].ff;
    		if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;
    		t[x].ff=z;int k=t[y].ch[1]==x;
    		t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
    		t[x].ch[k^1]=y;t[y].ff=x;
    		pushup(y);
    	}
    	void splay(int x)
    	{
    		sta[++top]=x;
    		for(int pos=x;!isroot(pos);pos=t[pos].ff)sta[++top]=t[pos].ff;
    		while(top)pushdown(sta[top--]);
    		while(!isroot(x))
    		{
    			int y=t[x].ff,z=t[y].ff;
    			if(!isroot(y))
    				(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
    			rotate(x);
    		}
    		pushup(x);
    	}
    	void access(int x){for(int y=0;x;y=x,x=t[x].ff)splay(x),t[x].ch[1]=y,pushup(x);}
    	void makeroot(int x){access(x);splay(x);reverse(x);}
    	int findroot(int x){access(x);splay(x);while(t[x].ch[0])x=t[x].ch[0];return x;}
    	void split(int x,int y){makeroot(x);access(y);splay(y);}
    	void link(int x,int y){makeroot(x);t[x].ff=y;}
    	void cut(int x,int y){split(x,y);t[x].ff=t[y].ch[0]=0;}
    }
    using namespace LinkCutTree;
    int main()
    {
    	n=gi();m=gi();Q=gi();
    	for(int i=1;i<=m;i++){int u=gi(),v=gi(),w=gi();if(u>v)swap(u,v);e[i]=(edge){u,v,w};}
    	sort(e+1,e+m+1);
    	for(int i=1;i<=m;i++)M[mp(e[i].u,e[i].v)]=i;
    	for(int i=1;i<=n+m;i++)init(i,i<=n?0:i-n);
    	int tot=0;
    	for(int i=1;i<=Q;i++){int opt=gi(),x=gi(),y=gi();if(x>y)swap(x,y);q[i]=(ques){opt,x,y};if(q[i].opt==2)Cut[M[mp(q[i].x,q[i].y)]]=1;}
    	for(int i=1;i<=m;i++)
    		if(!Cut[i])
    		{
    			int u=e[i].u,v=e[i].v;
    			if(tot==n-1)break;
    			if(findroot(u)!=findroot(v))
    			{
    				link(u,i+n);
    				link(v,i+n);
    				tot++;
    			}
    		}
    	for(int i=Q;i>=1;i--)
    	{
    		int x=q[i].x,y=q[i].y;
    		split(x,y);
    		if(q[i].opt&1){ans[i]=e[t[y].mx].w;}
    		else
    		{
    			int id=M[mp(x,y)],d=t[y].mx;
    			if(e[d].w<=e[id].w)continue;
    			cut(e[d].u,d+n);cut(d+n,e[d].v);
    			link(x,id+n);link(y,id+n);
    		}
    	}
    	for(int i=1;i<=Q;i++)
    		if(q[i].opt&1)printf("%d
    ",ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    VOIP开源项目源码地址(一)
    iptables下udp穿越实用篇
    function socket about http://net.pku.edu.cn/~yhf/linux_c/function/14.html
    IOKE的SIP协议专栏
    XviD core API overview: Decoding
    Socket about
    sql海量数据优化
    Socket、多线程、消息队列、共享资源并发下的性能研究
    【转】SQL 索引理解
    SQL 索引理解
  • 原文地址:https://www.cnblogs.com/mleautomaton/p/10674121.html
Copyright © 2011-2022 走看看