标签:class stdout scanf out puts main namespace 最小割 div
定理:n个点的无向图的最小割最多n-1个。
可能从某种形式上形成了一棵树,不是很清楚。
最小割分治:先任选两个点求一边最小割,然后将两边分别递归,就能找到所有的最小割。
这两个题是一样的,直接搬dinic模板即可。
BZOJ2229:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define mem(a,k) memset(a,k,sizeof(a)) 6 #define rep(i,l,r) for (int i=l; i<=r; i++) 7 #define For(i,x) for (int i=h[x],k; i; i=e[i].nxt) 8 using namespace std; 9 10 const int N=160,inf=1000000000; 11 int m,n,u,v,w,x,S,T,TT,Q,tot,cnt,tmp[N],a[N],b[N],d[N],q[N*10],h[N],ans[N][N]; 12 struct E{ int to,nxt,v; }e[7010]; 13 bool mark[N]; 14 15 void add(int u,int v,int w){ 16 e[++cnt]=(E){v,h[u],w}; h[u]=cnt; 17 e[++cnt]=(E){u,h[v],w}; h[v]=cnt; 18 } 19 20 bool bfs(){ 21 mem(d,0); q[1]=S; d[S]=1; 22 for (int st=0,ed=1; st!=ed; ){ 23 int x=q[++st]; 24 For(i,x) if (e[i].v && !d[k=e[i].to]) 25 d[k]=d[x]+1,q[++ed]=k; 26 } 27 return d[T]; 28 } 29 30 int dfs(int x,int lim){ 31 if (x==T) return lim; 32 int t,c=0; 33 For(i,x) if (d[k=e[i].to]==d[x]+1){ 34 t=dfs(k,min(lim-c,e[i].v)); 35 e[i].v-=t; e[i^1].v+=t; c+=t; 36 if (c==lim) return lim; 37 } 38 if (!c) d[x]=-1; return c; 39 } 40 41 int dinic(){ int ans=0; while(bfs()) ans+=dfs(S,inf); return ans; } 42 43 void get(int x){ 44 mark[x]=1; For(i,x) if (!mark[k=e[i].to] && e[i].v) get(k); 45 } 46 47 void solve(int l,int r){ 48 if (l==r) return; 49 S=a[l]; T=a[r]; int t=dinic(); 50 mem(mark,0); get(S); int p=l,p0; 51 rep(i,1,n) if (mark[i]) rep(j,1,n) if (!mark[j]) ans[i][j]=ans[j][i]=min(ans[i][j],t); 52 for (int i=2; i<=cnt; i+=2) e[i].v=e[i^1].v=(e[i].v+e[i^1].v)>>1; 53 rep(i,l,r) if (mark[a[i]]) tmp[p++]=a[i]; 54 p0=p; 55 rep(i,l,r) if (!mark[a[i]]) tmp[p++]=a[i]; 56 rep(i,l,r) a[i]=tmp[i]; 57 solve(l,p0-1); solve(p0,r); 58 } 59 60 int main(){ 61 freopen("bzoj2229.in","r",stdin); 62 freopen("bzoj2229.out","w",stdout); 63 for (scanf("%d",&TT); TT--; ){ 64 cnt=1; mem(h,0); mem(ans,0x3f); 65 scanf("%d%d",&n,&m); 66 rep(i,1,m) scanf("%d%d%d",&u,&v,&w),add(u,v,w); 67 rep(i,1,n) a[i]=i; solve(1,n); 68 for (scanf("%d",&Q); Q--; ){ 69 scanf("%d",&x); tot=0; 70 rep(i,1,n-1) rep(j,i+1,n) if (ans[i][j]<=x) tot++; 71 printf("%d\n",tot); 72 } 73 puts(""); 74 } 75 return 0; 76 }
BZOJ4519:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define rep(i,l,r) for (int i=l; i<=r; i++) 6 #define For(i,x) for (int i=h[x],k; i; i=e[i].nxt) 7 using namespace std; 8 9 const int N=1010,inf=1000000000; 10 int m,n,u,v,w,S,T,tot,cnt=1,tmp[N],a[N],b[N],d[N],q[N],h[N]; 11 struct E{ int to,nxt,v; }e[20010]; 12 bool mark[N]; 13 14 void add(int u,int v,int w){ 15 e[++cnt]=(E){v,h[u],w}; h[u]=cnt; 16 e[++cnt]=(E){u,h[v],w}; h[v]=cnt; 17 } 18 19 bool bfs(){ 20 memset(d,0,sizeof(d)); q[1]=S; d[S]=1; 21 for (int st=0,ed=1; st!=ed; ){ 22 int x=q[++st]; 23 For(i,x) if (e[i].v && !d[k=e[i].to]) 24 d[k]=d[x]+1,q[++ed]=k; 25 } 26 return d[T]; 27 } 28 29 int dfs(int x,int lim){ 30 if (x==T) return lim; 31 int t,c=0; 32 For(i,x) if (d[k=e[i].to]==d[x]+1){ 33 t=dfs(k,min(lim-c,e[i].v)); 34 e[i].v-=t; e[i^1].v+=t; c+=t; 35 if (c==lim) return lim; 36 } 37 if (!c) d[x]=-1; return c; 38 } 39 40 int dinic(){ int ans=0; while(bfs()) ans+=dfs(S,inf); return ans; } 41 42 void get(int x){ 43 mark[x]=1; 44 For(i,x) if (!mark[k=e[i].to] && e[i].v) get(k); 45 } 46 47 void solve(int l,int r){ 48 if (l==r) return; 49 S=a[l]; T=a[r]; b[++tot]=dinic(); 50 memset(mark,0,sizeof(mark)); 51 get(S); int p=l,p0; 52 for (int i=2; i<=cnt; i+=2) e[i].v=e[i^1].v=(e[i].v+e[i^1].v)>>1; 53 rep(i,l,r) if (mark[a[i]]) tmp[p++]=a[i]; 54 p0=p; 55 rep(i,l,r) if (!mark[a[i]]) tmp[p++]=a[i]; 56 rep(i,l,r) a[i]=tmp[i]; 57 solve(l,p0-1); solve(p0,r); 58 } 59 60 int main(){ 61 freopen("bzoj4519.in","r",stdin); 62 freopen("bzoj4519.out","w",stdout); 63 scanf("%d%d",&n,&m); 64 rep(i,1,m) scanf("%d%d%d",&u,&v,&w),add(u,v,w); 65 rep(i,1,n) a[i]=i; 66 solve(1,n); sort(b+1,b+tot+1); tot=unique(b+1,b+tot+1)-b-1; 67 printf("%d\n",tot); 68 return 0; 69 }
最小割分治(最小割树):BZOJ2229 && BZOJ4519
标签:class stdout scanf out puts main namespace 最小割 div
原文地址:https://www.cnblogs.com/HocRiser/p/9063144.html