• CF603E Pastoral Oddities


    XXIII.CF603E Pastoral Oddities

    结论1:只有点数为偶的连通块,才有可能使每个点的度数为奇。(偶连通块的必要性)

    证明1:如果在奇连通块中,每个点的度数为奇,则总度数为奇,但是总度数必定是偶数(每条边增加两点度数),因此不可能存在奇的合法连通块。

    结论2:任何点数为偶的连通块,都可以使每个点的度数为奇。(偶连通块的充分性)

    证明2:我们对这个偶连通块跑出任何一棵生成树。从叶子节点开始考虑。如果当前节点的度数为偶数,那么就加上和父亲的边。这样的话最终就只有根节点的度数不能确定。但是那些被加上的边,总度数为偶数;除了根以外,其它节点的度数都能保证是偶数;偶数-偶数=偶数。这就保证了根节点的度数也为偶数。

    结论1+结论2,则偶连通块是充分必要条件。

    我们之后就可以维护原图的最小生成森林。当原图中所有连通块的点数都为偶时,这时就可以删边了;因为在我们的证明2中,在一棵生成树中,也有不选的边。我们在一个大根堆中维护所有图上的边。从堆中不断删边,直到删完一条边后,出现了奇连通块。则答案即为所有曾经删去的边中的最小值。

    我们接下来证明各操作的正确性。

    维护森林的正确性:凭什么你连边维护最小生成森林就不会使得偶连通块变成奇连通块?

    证明:当你连一条边时,就将两个连通块相连了。如果原本是两个偶连通块,连完还是偶连通块,奇连通块数量并没有增加;一奇一偶,连完是奇,奇连通块数量还是没有增加;两个奇连通块,连完就成了偶连通块,奇连通块数量减小了!

    因此无论如何,连边都不会使得奇连通块数量增加;因此连了并不会使答案变差。

    删边的正确性:为什么可以用大根堆删边?

    首先,大根堆的堆顶,就是当前局面的最大值;但是,如果删完堆顶仍然没有奇连通块,则这个最大边是可有可无的,不如删掉。并且,答案不降,因此可以取\(\min\)

    LCT复杂度\(O\Big((n+m)\log(n+m)\Big)\);大根堆复杂度\(O(m\log m)\);总复杂度\(O\Big((n+m)\log(n+m)\Big)\)

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    int n,m,tot;
    #define lson t[x].ch[0]
    #define rson t[x].ch[1]
    struct LCT{
    	int fa,ch[2],si,sr,mx,val;
    	bool rev;
    }t[400100];
    inline int identify(int x){
    	if(x==t[t[x].fa].ch[0])return 0;
    	if(x==t[t[x].fa].ch[1])return 1;
    	return -1;
    }
    inline void pushup(int x){
    	t[x].sr=t[x].si,t[x].mx=x;
    	if(lson){
    		t[x].sr+=t[lson].sr;
    		if(t[t[x].mx].val<t[t[lson].mx].val)t[x].mx=t[lson].mx;
    	}
    	if(rson){
    		t[x].sr+=t[rson].sr;
    		if(t[t[x].mx].val<t[t[rson].mx].val)t[x].mx=t[rson].mx;
    	}
    }
    inline void REV(int x){
    	t[x].rev^=1,swap(lson,rson);
    }
    inline void pushdown(int x){
    	if(!t[x].rev)return;
    	if(lson)REV(lson);
    	if(rson)REV(rson);
    	t[x].rev=0;
    }
    inline void rotate(int x){
    	register int y=t[x].fa;
    	register int z=t[y].fa;
    	register int dirx=identify(x);
    	register int diry=identify(y);
    	register int b=t[x].ch[!dirx];
    	if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
    	if(b)t[b].fa=y;t[y].ch[dirx]=b;
    	t[y].fa=x,t[x].ch[!dirx]=y;
    	pushup(y),pushup(x);
    }
    inline void pushall(int x){
    	if(identify(x)!=-1)pushall(t[x].fa);
    	pushdown(x);
    }
    inline void splay(int x){
    	pushall(x);
    	while(identify(x)!=-1){
    		register int fa=t[x].fa;
    		if(identify(fa)==-1)rotate(x);
    		else if(identify(x)==identify(fa))rotate(fa),rotate(x);
    		else rotate(x),rotate(x);
    	}
    }
    inline void access(int x){
    	for(register int y=0;x;x=t[y=x].fa){
    		splay(x);
    		t[x].si+=t[rson].sr-t[y].sr;
    		rson=y;
    		pushup(x);
    	}
    }
    inline void makeroot(int x){
    	access(x),splay(x),REV(x);
    }
    inline int findroot(int x){
    	access(x),splay(x);
    	pushdown(x);
    	while(lson)x=lson,pushdown(x);
    	splay(x);
    	return x;
    }
    inline void split(int x,int y){
    	makeroot(x),access(y),splay(y);
    }
    inline void link(int x,int y){
    	split(x,y);
    	tot-=(t[x].sr&1);
    	tot-=(t[y].sr&1);
    	t[x].fa=y;
    	t[y].si+=t[x].sr;
    	pushup(y);
    	tot+=(t[y].sr&1);
    }
    inline void cut(int x,int y){
    	split(x,y);
    	tot-=(t[y].sr&1);
    	t[y].ch[0]=t[x].fa=0,pushup(y);
    	tot+=(t[x].sr&1);
    	tot+=(t[y].sr&1);
    }
    struct edge{
    	int x,y,z;
    }e[300100];
    struct op{
    	int id;
    	op(int x=0){id=x;}
    	friend bool operator<(const op&x,const op&y){
    		return e[x.id].z<e[y.id].z;
    	}
    };
    priority_queue<op>q;
    bool era[300100];
    int main(){
    	scanf("%d%d",&n,&m),tot=n;
    	for(int i=1;i<=n;i++)t[i].sr=t[i].si=1;
    	for(int i=1,res=0x3f3f3f3f;i<=m;i++){
    		scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z),t[i+n].val=e[i].z;
    		if(findroot(e[i].x)!=findroot(e[i].y))link(e[i].x,i+n),link(e[i].y,i+n),q.push(op(i));
    		else{
    			split(e[i].x,e[i].y);
    			int tmp=t[e[i].y].mx;
    			if(t[tmp].val>e[i].z){
    				era[tmp-n]=true;
    				cut(tmp,e[tmp-n].x),cut(tmp,e[tmp-n].y);
    				link(e[i].x,i+n),link(e[i].y,i+n);
    				q.push(op(i));
    			}else era[i]=true;
    		}
    		while(!tot){
    			while(era[q.top().id])q.pop();
    			int tmp=q.top().id;
    			cut(e[tmp].x,tmp+n),cut(e[tmp].y,tmp+n),era[tmp]=true;
    			res=min(res,e[tmp].z);
    			q.pop();
    		}
    		printf("%d\n",res==0x3f3f3f3f?-1:res);
    	}
    	return 0;
    }
    

  • 相关阅读:
    Windows下的SASS环境搭建
    cocos开发环境搭建
    cocos2d-x中描述精灵帧图片的plist和json文件各个key的含义
    seajs模块路径解析 简单总结
    【LESS系列】内置函数说明
    Zepto自定义模块打包构建
    【LESS系列】高级特性
    【LESS系列】三角形Mixins
    关于delete和对象复制
    vue使用技巧
  • 原文地址:https://www.cnblogs.com/Troverld/p/14602204.html
Copyright © 2020-2023  润新知