[分治最小割] BZOJ 4519 [Cqoi2016]不同的最小割

最多有N-1种zuixiaoge

分治最小割裸题


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<set>
#define V G[p].v
#define cl(x) memset(x,0,sizeof(x))
using namespace std;

namespace hash_map{
	struct node{
		int x; int next;
	}G[10005];
	int head[10000007],P=10000007,inum;
	inline void add(int x){
		G[++inum].x=x; G[inum].next=head[x%P]; head[x%P]=inum;
	}
	inline int find(int x){
		for (int p=head[x%P];p;p=G[p].next)
			if (G[p].x==x)
				return p;
		return 0;
	}
}

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;
}

struct edge{
	int u,v,f;
	int next;
};

edge G[20005];
int head[1005],inum=1;

inline void add(int u,int v,int f,int p){
	G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
}

inline void link(int u,int v,int f){
	add(u,v,f,++inum); add(v,u,f,++inum);
}

int S,T;

int dis[1005];
int Q[1005],l,r;

inline bool bfs(){
	memset(dis,-1,sizeof(dis)); l=r=-1;
	Q[++r]=S; dis[S]=1;
	while (l<r){
		int u=Q[++l];
		for (int p=head[u];p;p=G[p].next)
			if (G[p].f && dis[V]==-1)
			{
				dis[V]=dis[u]+1;
				Q[++r]=V;
				if (V==T) return 1;
			}
	}
	return 0;
}

int minimum,cur[1005];

int dfs(int u,int flow)
{
	if (u==T) return flow;
	int used=0,now;
	for (int p=cur[u];p;p=G[p].next)
	{
		cur[u]=p;
		if (G[p].f && dis[V]==dis[u]+1)
		{
			now=dfs(V,min(flow-used,G[p].f));
			G[p].f-=now; G[p^1].f+=now;
			used+=now; if (flow==used) break;
		}
	}
	if (!used) dis[u]=-1;
	return used;
}

int mark[1005];

inline void restore(){
	for (int p=2;p<=inum;p+=2)
		G[p].f=G[p^1].f=(G[p].f+G[p^1].f)>>1;
}

void dfs(int u)
{
	mark[u]=1;
	for (int p=head[u];p;p=G[p].next)
		if (G[p].f && !mark[V])
			dfs(V);
}

int n,m;
int a[1005],tmp[1005];
int tot;

void Solve(int l,int r){
	if (l==r) return;
	restore(); S=a[l],T=a[r];
	int ret=0;
	while (bfs()) 
	{
		memcpy(cur,head,sizeof(head));
		ret+=dfs(S,1<<30);
	}
	if (!hash_map::find(ret)) tot++,hash_map::add(ret);
	cl(mark);
	dfs(S);
	int L=l,R=r;
	for(int i=l;i<=r;i++)
		if(mark[a[i]])
			tmp[L++]=a[i];
		else 
			tmp[R--]=a[i];
	for(int i=l;i<=r;i++) a[i]=tmp[i];
	Solve(l,L-1); Solve(R+1,r);
}

int main()
{
	int iu,iv,iw;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(m);
	for (int i=1;i<=m;i++)
		read(iu),read(iv),read(iw),link(iu,iv,iw);
	for (int i=1;i<=n;i++)
		a[i]=i;
	Solve(1,n);
	printf("%d\n",tot);
	return 0;
}


你可能感兴趣的:([分治最小割] BZOJ 4519 [Cqoi2016]不同的最小割)