Reference: Http://www.cnblogs.com/jackge/p/3231767.html speaks very well.
Sentiment: the best situation must be a complete picture, but not allowed, so must be some conform to u->v, but V can not go to u,
In this case, maximize the number of edges, eventually forming two graphs, then apply inequalities
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<string>#include<stack>#include<map>#include<queue>#include<algorithm>#include<utility>using namespaceStd;typedefLong LongLL;Const intn=1e5+5;Const intinf=0x3f3f3f3f;structedge{intU,v,next;} Edge[n];inthead[n],tot,n,m;voidAddintUintv) {edge[tot].u=u; EDGE[TOT].V=v; Edge[tot].next=Head[u]; Head[u]=tot++;} Stack<int>s;BOOLInstack[n];intDfn[n],low[n],clk,cnt,bel[n],inch[N], out[n],bcc[n];voidTargin (intu) {Dfn[u]=low[u]=++CLK; Instack[u]=true; S.push (U); for(intI=head[u];~i;i=Edge[i].next) { intv=edge[i].v; if(!Dfn[v]) {Targin (v); Low[u]=min (low[u],low[v]); } Else if(Instack[v]) Low[u]=min (low[u],dfn[v]); } if(low[u]==Dfn[u]) { ++cnt;intK; Do{k=S.top (); S.pop (); INSTACK[K]=false; BEL[K]=cnt;++bcc[cnt]; } while(k!=u); }}intMain () {intt,cas=0; scanf ("%d",&T); while(t--) {printf ("Case %d:",++CAs); scanf ("%d%d",&n,&m); for(intI=1; i<=n;++i) {Head[i]=-1;d fn[i]=0; instack[i]=false; Bcc[i]=0; } CLK=cnt=tot=0; for(intI=1; i<=m;++i) { intu,v; scanf ("%d%d",&u,&v); Add (U,V); } for(intI=1; i<=n;++i)if(!Dfn[i]) targin (i); if(cnt==1) {printf ("-1\n");Continue; } for(intI=1; i<=cnt;++i)inch[i]= out[i]=0; for(intI=0; i<tot;++i) { intu=bel[edge[i].u],v=BEL[EDGE[I].V]; if(U==V)Continue; ++ out[u];++inch[v]; } intans=INF; for(intI=1; i<=cnt;++i)if(!inch[I]| |! out[i]) ans=min (ans,bcc[i]); LL Aim=1ll*n* (n1) -1ll*ans* (N-ans)-m; printf ("%i64d\n", AIM); } return 0;}
View Code
HDU 4635 strongly connected strong connected components