Strongly connected
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
problem DescriptionGive a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph was still a simple directed graph. Also, after you add these edges, the this graph must isn't be strongly connected.
a simple directed graph was a directed graph having no multiple edges or graph loops.
A strongly connected digraph is a directed graph in which it's possible to reach all node starting from all other node by Traversing edges in the direction (s) in which they point.
InputThe first line of date is a integer T, which is the number of the text cases.
Then T cases follow, each case starts of the numbers N and M, 1<=n<=100000, 1<=m<=100000, representing the NUM ber of nodes and the number of edges, then M lines follow. Each line contains the integers x and y, means that there was a edge from X to Y.
OutputFor each case, you should output the maximum number of the edges you can add.
If The original graph is strongly connected, just output-1.
Sample Input33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
Sample OutputCase 1: -1case 2:1case 3:15
SolvingQ: How many edges are added so that the original image is not a strong connected graphcan think backwards, assuming the original image is full, i.e. sum=n* (n-1), minus how many edges make it not a strong connected graphDirect plus Edge sss=n* (n-1)-M;using Tarjan to find strong connected components and shrink points, the points are divided into two parts, part to the other part of the no edge (that is, the point of the degree or out of 0), and then use SSS minus the two parts can constitute a variable. assuming that the first strong connected component has a num[i] point, then it is connected to the other half of the graph can be num[i]* (N-num[i]), the minimum value, minus the line.
Code
#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;Const intMAXN =100000+ -;/** Tarjan algorithm * complexity O (n+m)*/structedge{intTo,next;} EDGE[MAXN];intHead[maxn],tot;intLOW[MAXN],DFN[MAXN],STACK[MAXN],BELONG[MAXN];//the value of the belong array is 1~SCCintIndex,top;intScc//number of strongly connected componentsBOOLINSTACK[MAXN];intNUM[MAXN];//each strong connected component contains the number of points, array number 1~SCCvoidinit () {tot=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]=tot++;}voidTarjan (intu) { intv; Low[u]=dfn[u]=++Index; Stack[top++]=u; Instack[u]=true; for(inti=head[u];i!=-1; i=Edge[i].next) {v=edge[i].to; if(!Dfn[v]) {Tarjan (v); if(Low[u]>low[v]) low[u]=Low[v]; } Else if(instack[v]&&low[u]>Dfn[v]) Low[u]=Dfn[v]; } if(low[u]==Dfn[u]) {SCC++; Do{v=stack[--top]; INSTACK[V]=false; BELONG[V]=SCC; NUM[SCC]++; } while(v!=T); }}voidSolveintN) {memset (DFN,0,sizeof(DFN)); memset (Instack,false,sizeof(Instack)); memset (num,0,sizeof(num)); Index=top=scc=0; for(intI=1; i<=n;i++) if(!Dfn[i]) Tarjan (i);}int inch[MAXN], out[MAXN];intMain () {intT; intIcase=0; scanf ("%d",&T); while(t--) {icase++; Init (); intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=m;i++){ intu,v; scanf ("%d%d",&u,&v); Addedge (U,V); } solve (n); if(scc==1) {printf ("Case %d: -1\n", icase); Continue; } for(intI=1; i<=scc;i++){ inch[i]=0; out[i]=0; } for(intu=1; u<=n;u++){ for(inti=head[u];i!=-1; i=Edge[i].next) { intv=edge[i].to; if(Belong[u]==belong[v])Continue; out[belong[u]]++; inch[belong[v]]++; } } Long LongSss= (Long Long) N (n1)-m; Long Longans=0; for(intI=1; i<=scc;i++){ if(inch[i]==0|| out[i]==0) ans=max (ans,sss-(Long Long) num[i]* (nnum[i])); } printf ("Case %d:%lld\n", Icase,ans); }}
HDU 4635 strongly connected (strongly connected components of a graph)