[BZOJ4519][最小割树]CQOI2016:不同的最小割

BZOJ4519

BZOJ2229
这题unique就完了

Code:

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
	return res*f;
}
const int M=2e4+5,INF=1e9;
namespace Dinic{
	int vis[M],head[M],nxt[M],c[M],tot=1;
	inline void add(int x,int y,int z){
		vis[++tot]=y;nxt[tot]=head[x];head[x]=tot;c[tot]=z;
		vis[++tot]=x;nxt[tot]=head[y];head[y]=tot;c[tot]=z;	
	}
	int d[M],s,t;
	inline bool bfs(){
		memset(d,-1,sizeof(d));
		queue<int>q;
		d[s]=1,q.push(s);
		while(!q.empty()){
			int x=q.front();q.pop();
			for(int i=head[x];i;i=nxt[i]){
				int y=vis[i];
				if(d[y]==-1 && c[i]){
					d[y]=d[x]+1;
					q.push(y);
					if(y==t) return 1;	
				}
			}	
		}
		return 0;
 }
	inline int dfs(int v,int flow){
		if(v==t) return flow;
		int res=flow,k;
		for(int i=head[v];i && res;i=nxt[i]){
			int y=vis[i];
			if(c[i] && d[y]==d[v]+1){
				k=dfs(y,min(res,c[i]));
				if(!k) d[y]=-1;
				c[i]-=k;c[i^1]+=k;res-=k;
				if(!flow) break;
			}
		}
		return flow-res;
	}
	inline int dinic(int x,int y){
		s=x;t=y;
		int flow,mxflow=0;
		while(bfs()){
			while(flow=dfs(s,INF)) mxflow+=flow;
		}
		return mxflow;
	}
}
using namespace Dinic;
namespace MCT{
	inline void reset(){
		for(int i=2;i<=tot;i+=2)
			c[i]=c[i^1]=(c[i]+c[i^1])>>1;
	}
	int p[M],col[M],ans[M],tmp[M],cnt=0;
	inline void color(int v){
		col[v]=1;
		for(int i=head[v];i;i=nxt[i]){
			if(!col[vis[i]] && c[i]) color(vis[i]);
		}
	}
	inline void solve(int l,int r){
		if(l==r) return;
		reset();
		int res=dinic(p[l],p[r]);
		ans[++cnt]=res;
		memset(col,0,sizeof(col));
		color(p[l]);
		int cnt1=l-1,cnt2;
		for(int i=l;i<=r;i++)
			if(col[p[i]]==1) tmp[++cnt1]=p[i];
		cnt2=cnt1;
		for(int i=l;i<=r;i++)
			if(col[p[i]]==0) tmp[++cnt1]=p[i];
		for(int i=l;i<=r;i++) p[i]=tmp[i];
		solve(l,cnt2);solve(cnt2+1,r);
	}
}
using namespace MCT;
int main(){
	int n=read(),m=read();
	for(int x,y,z,i=1;i<=m;i++){
		x=read(),y=read(),z=read();
		add(x,y,z);
	}
	for(int i=1;i<=n;i++) p[i]=i;
	solve(1,n);
	sort(ans+1,ans+cnt+1);
	cnt=unique(ans+1,ans+cnt+1)-ans-1;
	cout<<cnt<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值