http://acm.hdu.edu.cn/showproblem.php?pid=4635
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 the should output the maximum number of the edges you can add.
If The original graph is strongly connected, just output-1.
Sample Input
33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
Sample Output
Case 1: -1case 2:1case 3:15
/**hdu 4635 Strong connected Components + Shrinkage Point topic: Given a graph, ask how many sides can be added to make it is not connected to the schematic idea: http://www.xuebuyuan.com/1606580.html*/#include <stdio.h > #include <string.h> #include <algorithm> #include <iostream>using namespace Std;typedef long Long ll;const int maxn=100005;struct note{int v,next;} Edge[maxn*10];int Head[maxn],ip;int St[maxn],ins[maxn],dfn[maxn],low[maxn],cnt_tar,index,top;int in[maxn],out[ Maxn],num[maxn],belong[maxn];int m,n;void addedge (int u,int v) {edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;} void Init () {memset (head,-1,sizeof (head)); Ip=0;} void Tarjan (int u) {dfn[u]=low[u]=++index; St[++top]=u; Ins[u]=1; for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; if (!dfn[v]) {Tarjan (v); Low[u]=min (Low[u],low[v]); } else if (Ins[v]) {low[u]=min (low[u],dfn[v]); }} if (Dfn[u]==low[u]) {cnt_tar++; Int J; do {j=st[top--]; ins[j]=0; Belong[j]=cnt_tar; num[cnt_tar]++; }while (J!=u); }}void solve () {top=0,index=0,cnt_tar=0; memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i); }}int Main () {int t,tt=0; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); Init (); for (int i=1;i<=m;i++) {int u,v; scanf ("%d%d", &u,&v); Addedge (U,V); } memset (Num,0,sizeof (num)); Solve (); if (cnt_tar==1) {printf ("Case%d: -1\n", ++TT); Continue } memset (In,0,sizeof (in)); Memset (out,0,sizeof (out)); for (int u=1;u<=n;u++) {for (int i=head[u];i!=-1;i=edge[i].next) {int V=EDG E[I].V; if (Belong[u]==belong[v]) continue; out[belong[u]]++; in[belong[v]]++; }} LL all= (LL) n (n-1)-M; LL ans=0; for (int i=1;i<=cnt_tar;i++) {if (in[i]==0| | out[i]==0) {Ans=max (ans,all-(LL) num[i]* (N-num[i])); }} printf ("Case%d:%i64d\n", ++tt,ans); } return 0;}
HDU 4635 Strong connected component + Pinch point