HDU 4635 strongly connected (strong connectivity) Classic

Source: Internet
Author: User

Strongly connectedTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1828 Accepted Submission (s): 752


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

Source2013 multi-university Training Contest 4 test instructions: Give a simple undirected graph of n points, ask the maximum number of edges that can be added so that the graph is still a simple undirected graph, and not a strong connected graph. Simple graph definition: No heavy edges, no self-loops. Strongly connected graphs: The entire graph has only one point after the contraction point, which contains n origin, that is, a connected component. If the first is a strong connected graph, then output-1. Problem solving: To add the edge of the most so the graph of the number of connected components, the less the better, then the minimum connectivity component is 2. First, a complete graph is constructed with n points (the direction graph has: N (n-1) edge, the non-direction graph has: N (n-1)/2 edges), and then with the structure of the edge minus the original M-Bar edge =ans. Then use the strong connectivity algorithm to indent points, record each new point contains the number of points, from the degree of 0 or a new point with a degree of 0 to find the Minnum, and then use the remaining Edge ans-minnum* (n-minnum) is the answer. Because if you do not reduce the degree to 0 or the out of 0 related edges, then the point itself contains the in and out edges, plus the edge is always a strong connected graph. So can only be removed with the degree of 0 or out of the relative edge of 0 points, only one direction of the edge, or all (N-minnum) points to the minnum points in the edge, then is minnum points to (N-minnum) points out of the edge.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define LL __ int64const int N = 100005;struct edg{int to,next;}    Edg[n];int eid,head[n];int low[n],dfn[n],vist[n],num[n],id[n],deep,stack1[n],tn,top;int in[N],out[N];void init () {    Eid=tn=top=deep=0;    memset (head,-1,sizeof (head));    memset (vist,0,sizeof (vist));    memset (In,0,sizeof (in));    Memset (out,0,sizeof (out)); memset (num,0,sizeof (num));} void addedg (int u,int v) {edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;}    void Tarjer (int u) {stack1[++top]=u;    Vist[u]=1;    deep++;    Low[u]=dfn[u]=deep;        for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to;            if (vist[v]==0) {vist[v]=1;            Tarjer (v);        Low[u]=min (Low[u],low[v]);    } else if (vist[v]==1) low[u]=min (Low[u],dfn[v]);        } if (Low[u]==dfn[u]) {tn++;            do{vist[stack1[top]]=2;            num[tn]++; Id[stack1[top]]=tN    }while (Stack1[top--]!=u);    }}ll solve (int n,int m) {ll ans=n* (n-1)-M;    int minnum=n;    for (int i=1; i<=n; i++) if (vist[i]==0) Tarjer (i);    if (tn==1) return-1;        for (int u=1, u<=n; u++) for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to;    if (Id[u]!=id[v]) in[id[v]]++,out[id[u]]++; } for (int i=1; i<=tn; i++) if (in[i]==0| |    out[i]==0) {minnum=min (minnum,num[i]);        } ans-=minnum* (N-minnum); return ans;}    int main () {int t,n,m,c=0,a,b;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        Init ();            for (int i=1; i<=m; i++) {scanf ("%d%d", &a,&b);        Addedg (A, b);    } printf ("Case%d:%i64d\n", ++c,solve (n,m)); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4635 strongly connected (strong connectivity) Classic

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.