bzoj 4519: [Cqoi2016]不同的最小割 最小割树

时间:2025-04-23 19:37:31

怎么求一张无向图中任意两点之间的最小割?

http://fanhq666.blog.163.com/blog/static/8194342620113495335724/

一张无向图不同的最小割最多有n-1个。

所以可以用这些最小割建出一棵最小割树。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define N 855
#define M 400005
using namespace std;
int read()
{
int p=;char c=getchar();
while(c<''||c>'')c=getchar();
while(c>=''&&c<='')p=p*+c-'',c=getchar();
return p;
}
int head[N],ver[M],nxt[M],f[M],tot,ch[N];
void add(int a,int b,int c)
{
tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=b;f[tot]=c;return ;
}
queue<int>q;int S,T;
bool tell()
{
memset(ch,-,sizeof(ch));
q.push(S);ch[S]=;
while(!q.empty())
{
int tmp=q.front();q.pop();
for(int i=head[tmp];i;i=nxt[i])
{
if(f[i]&&ch[ver[i]]==-)
{
ch[ver[i]]=ch[tmp]+;
q.push(ver[i]);
}
}
}
return ch[T]!=-;
}
int zeng(int a,int b)
{
if(a==T)return b;
int r=;
for(int i=head[a];i&&b>r;i=nxt[i])
{
if(f[i]&&ch[ver[i]]==ch[a]+)
{
int t=zeng(ver[i],min(f[i],b-r));
f[i]-=t;f[i^]+=t;r+=t;
}
}
if(!r)ch[a]=-;
return r;
}
int dinic()
{
int r=,t;
while(tell())while(t=zeng(S,inf))r+=t;
return r;
}
void hui()
{
for(int i=;i<=tot;i+=)
{
f[i]=f[i^]=(f[i]+f[i^])>>;
}return ;
}
int tim;
int c[N];
void dfs(int x)
{
c[x]=tim;
for(int i=head[x];i;i=nxt[i])
{
if(f[i]&&c[ver[i]]!=tim)dfs(ver[i]);
}
return ;
}
int p[N];
int ans[N],cnt;
int n,m;
int st[N],st2[N];
void solve(int l,int r)
{
if(l==r)return ;
hui();
S=p[l];T=p[r];
int tmp=dinic();
tim++;dfs(S);
ans[++cnt]=tmp;
int top1=,top2=;
for(int i=l;i<=r;i++)
{
if(c[p[i]]==tim)st[++top1]=p[i];
else st2[++top2]=p[i];
}
for(int i=l;i<=l+top1-;i++)p[i]=st[i-l+];
for(int i=l+top1;i<=r;i++)p[i]=st2[i-l-top1+];
solve(l,l+top1-);solve(l+top1,r);
}
int main()
{
scanf("%d%d",&n,&m);
int t1,t2,t3;tot=;
for(int i=;i<=m;i++)
{
t1=read();t2=read();t3=read();
add(t1,t2,t3);add(t2,t1,t3);
}
for(int i=;i<=n;i++)p[i]=i;
solve(,n);
sort(ans+,ans+cnt+);
int as=;
for(int i=;i<=cnt;i++)
{
if(i==||ans[i]!=ans[i-])
{
as++;
}
}
printf("%d\n",as);
}