BZOJ4519 CQOI2016不同的最小割(最小割+分治)
最小割树:新建一个图,包含原图的所有点,初始没有边。任取两点跑最小割,给两点连上权值为最小割的边,之后对于两个割集分别做同样的操作。最后会形成一棵树,树上两点间路径的最小值即为两点最小割。证明一点都不会。
那么这个题就很好做了,连树都不用建。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<map> using namespace std; int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 900 #define M 9000 #define inf 1000000000 int n,m,p[N],v[N],u[N],tot=0,t=-1; bool flag[N]; int d[N],cur[N],q[N]; struct data{int to,nxt,cap,flow; }edge[M<<1]; map<int,bool> f; void addedge(int x,int y,int z) { t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].cap=z,edge[t].flow=0,p[x]=t; } bool bfs(int S,int T) { memset(d,255,sizeof(d));d[S]=0; int head=0,tail=1;q[1]=S; do { int x=q[++head]; for (int i=p[x];~i;i=edge[i].nxt) if (d[edge[i].to]==-1&&edge[i].flow<edge[i].cap) { d[edge[i].to]=d[x]+1; q[++tail]=edge[i].to; } }while (head<tail); return ~d[T]; } int work(int k,int T,int f) { if (k==T) return f; int used=0; for (int i=cur[k];~i;i=edge[i].nxt) if (d[k]+1==d[edge[i].to]) { int w=work(edge[i].to,T,min(f-used,edge[i].cap-edge[i].flow)); edge[i].flow+=w,edge[i^1].flow-=w; if (edge[i].flow<edge[i].cap) cur[k]=i; used+=w;if (used==f) return f; } if (used==0) d[k]=-1; return used; } void dinic(int S,int T) { for (int i=0;i<=t;i++) edge[i].flow=0; int ans=0; while (bfs(S,T)) { memcpy(cur,p,sizeof(p)); ans+=work(S,T,inf); } if (!f[ans]) f[ans]=1,tot++; } void dfs(int k) { flag[k]=1; for (int i=p[k];~i;i=edge[i].nxt) if (!flag[edge[i].to]&&edge[i].flow<edge[i].cap) dfs(edge[i].to); } void solve(int l,int r) { if (l>=r) return; dinic(v[l],v[r]); memset(flag,0,sizeof(flag)); dfs(v[l]); int cnt=l-1; for (int i=l;i<=r;i++) if (flag[v[i]]) u[++cnt]=v[i]; cnt=r+1; for (int i=l;i<=r;i++) if (!flag[v[i]]) u[--cnt]=v[i]; for (int i=l;i<=r;i++) v[i]=u[i]; solve(l,cnt-1); solve(cnt,r); } int main() { #ifndef ONLINE_JUDGE freopen("bzoj4519.in","r",stdin); freopen("bzoj4519.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(),m=read(); memset(p,255,sizeof(p)); for (int i=1;i<=m;i++) { int x=read(),y=read(),z=read(); addedge(x,y,z),addedge(y,x,z); } for (int i=1;i<=n;i++) v[i]=i; solve(1,n); cout<<tot; return 0; }