求最小割集
dinic处理后用dfs对所有点进行标记,遍历整个联接边,起点访问了,终点没访问或者起点没访问,终点访问了就是最小割集之一
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 100000000 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define MIN(a,b) a<b ? a:b using namespace std; const double g=10.0,eps=1e-9; const int N=10000+10,maxn=500+10,inf=40000000; struct edge{ int from,to,next,cap; }e[N]; int dis[maxn],head[maxn]; bool vis[maxn],mp[maxn][maxn]; int cnt,s,t; void add(int u,int v,int c) { e[cnt].from=u; e[cnt].to=v; e[cnt].cap=c; e[cnt].next=head[u]; head[u]=cnt++; e[cnt].from=v; e[cnt].to=u; e[cnt].cap=0; e[cnt].next=head[v]; head[v]=cnt++; } bool bfs() { memset(dis,-1,sizeof dis); dis[s]=0; queue<int>q; q.push(s); while(!q.empty()){ int x=q.front(); q.pop(); for(int i=head[x];i!=-1;i=e[i].next) { int p=e[i].to; if(dis[p]==-1&&e[i].cap>0) { dis[p]=dis[x]+1; q.push(p); } } } return dis[t]>-1; } int dfs(int x,int mx) { if(x==t)return mx; for(int i=head[x];i!=-1;i=e[i].next) { int p=e[i].to,f; if(dis[p]==dis[x]+1&&e[i].cap>0&&(f=dfs(p,min(mx,e[i].cap)))) { e[i].cap-=f; e[i^1].cap+=f; return f; } } dis[x]=-2; return 0; } int max_flow() { int flow=0,f; while(bfs()){ while(f=dfs(s,inf))flow+=f; } // cout<<flow<<endl; return flow; } void BFS() { queue<int>q; q.push(s); memset(vis,0,sizeof vis); vis[s]=1; while(!q.empty()){ int x=q.front(); q.pop(); for(int i=head[x];i!=-1;i=e[i].next) { int p=e[i].to; if(!vis[p]&&e[i].cap>0) { vis[p]=1; q.push(p); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n,m; while(cin>>n>>m){ if(n==0||m==0)break; cnt=0; memset(head,-1,sizeof head); int a,b,c; while(m--){ cin>>a>>b>>c; add(a,b,c); add(b,a,c); } s=1,t=2; max_flow(); BFS(); memset(mp,0,sizeof mp); for(int i=0;i<cnt;i++) { a=e[i].from,b=e[i].to; if((vis[a]&&!vis[b])||(vis[b]&&!vis[a])) mp[a][b]=mp[b][a]=1; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(mp[i][j]) cout<<i<<" "<<j<<endl; cout<<endl; } return 0; }