HDU 4635 strongly connected strong connected components

Source: Internet
Author: User
Tags cas

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.