Max Density sub-group
Remember to zoom in on the edge when you're looking for the last time, so that it helps to select points when each side is full of flow
#include <iostream>#include<algorithm>#include<string.h>#include<cstdio>#include<vector>#include<queue>#include<cmath>using namespacestd;Const intmaxn= the;Const Doubleeps=0.00000001;Const Doubleinf=10005*10005; Vector<int>ans;structdinic{structEdge {int from, to; DoubleCap,flow; Edge (intCfrom=0,intcto=0,Doubleccap=0,Doublecflow=0) { from=cfrom; To=cto; Cap=ccap; flow=Cflow; } }; intn,m,s,t; Vector<Edge>edges; Vector<int>G[MAXN]; BOOLVIS[MAXN]; intD[MAXN]; intCUR[MAXN]; voidInitintN) { This->n=N; M=0; Edges.clear (); for(intI=0; i<=n; i++) g[i].clear (); } voidAddedge (int from,intTo,Doublecap) {Edges.push_back (Edge ( from, To,cap,0)); Edges.push_back (Edge to, from,0,0)); M+=2; g[ from].push_back (M-2); G[to].push_back (M-1); } BOOLBFS () {memset (Vis,false,sizeof(VIS)); Queue<int>p; Q.push (s); D[s]=0; Vis[s]=1; while(!Q.empty ()) { intx=Q.front (); Q.pop (); for(intI=0; I<g[x].size (); i++) {Edge&e =Edges[g[x][i]]; if(vis[e.to]==false&&e.cap>e.flow) {vis[e.to]=1; D[e.to]=d[x]+1; Q.push (e.to); } } } returnVis[t]; } DoubleDFS (intXDoublea) {if(x==t| | a==0)returnA; Doubleflow=0, F; for(int&i=cur[x]; I<g[x].size (); i++) {Edge&e=Edges[g[x][i]]; if(d[x]+1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {E.flow+=F; Edges[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(a==0) Break; } } returnflow; } DoubleMaxflow (intSintt) { This->s=s; This->t=T; Doubleflow=0; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S,inf); } returnflow; } voidFinde (intx) {vis[x]=1; Ans.push_back (x); for(intI=0; I<g[x].size (); i++) {Edge&e=Edges[g[x][i]]; if(vis[e.to]| | E.cap<=e.flow)Continue; Finde (e.to); } } voidsolve () {memset (Vis,false,sizeof(VIS)); Vis[s]=1; Vis[t]=1; for(intI=0; I<g[s].size (); i++) {Edge&e=Edges[g[s][i]]; if(vis[e.to]| | E.cap<=e.flow)Continue; Finde (e.to); }}}t;intD[MAXN];inta[1005],b[1005];DoubleU;voidBuildintNintMDoubleGintop=0) {t.init (n+2); for(intI=1; i<=n; i++) {T.addedge (n+1, i,u+op*eps*2); T.addedge (I,n+2, u+g*2-D[i]); } for(intI=1; i<=m; i++) {T.addedge (a[i],b[i],1); T.addedge (B[i],a[i],1); }}intMain () {intn,m; while(SCANF ("%d%d", &n,&m) = =2) { if(m==0) {printf ("1\n1\n");Continue; } memset (d,0,sizeof(d)); for(intI=1; i<=m; i++) {scanf ("%d%d",&a[i],&B[i]); D[a[i]]+ +;d [b[i]]++; } U=m; DoubleCl=0.0, cr=1.0*m; for(intI=0; i< -; i++) { DoubleMid= (CL+CR)/2; Build (N,m,mid); DoubleAns=t.maxflow (n+1, n+2); Ans= (U*n-ans)/2; if(ans>0) cl=mid; ElseCr=mid; } build (N,M,CL,1); T.maxflow (n+1, n+2); Ans.clear (); T.solve (); Sort (Ans.begin (), Ans.end ()); intGe=unique (Ans.begin (), Ans.end ())-Ans.begin (); printf ("%d\n", GE); for(intI=0; i<ge;i++) printf ("%d\n", Ans[i]); } return 0;}
poj3155 Maximum density sub-group