[Tarjan] HDU 4635 strongly connected

Source: Internet
Author: User
Tags cmath

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4635

Strongly CTED Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1568 accepted submission (s): 654


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 is still a simple directed graph. also, after you add these edges, this graph must not be strongly connected.
A simple directed graph is a directed graph having no multiple edges or graph loops.
A stronugly connected digraph is a directed graph in which it is possible to reach any node starting from any other node by traversing edges in the direction (s) in which they point.
 
Inputthe first line of date is an integer T, which is the number of the text cases.
Then T cases follow, each case starts of two numbers N and M, 1 <= n <= 100000, 1 <= m <= 100000, representing the number of nodes and the number of edges, then M lines follow. each line contains two integers x and y, means that there is a edge from X to Y.
Outputfor each case, You shocould 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
Recommendzhuyuanchen520 | we have carefully selected several similar problems for you: 5057 5056 5055 5054 5053
Statistic | submit | discuss | note

Question meaning:

For a graph, the maximum number of edges can be added, so that the graph cannot meet the requirement of strong connectivity.

Solution:

According to the analysis, the final result must be two full graphs A and B, and each node in A to B has a directed edge. The total number of edges is a * (A-1) + B * (b-1) + a * B = N * n-a * B.

Ans = N * n-a * B-m n/m is known, so to maximize ans, we need to minimize a * B to maximize the gap between A and B. So find the smallest A or B.

Tarjan strives for strong connectivity, and then finds out the strongly connected component with the degree of discrepancy or degree of exit being 0, and calculates a for the smallest number of knots in these Unicom components.

Code:

//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 110000int low[Maxn],dfn[Maxn],sta[Maxn],bc,sc,dep;ll n,m;int in[Maxn],dei[Maxn],deo[Maxn],nu[Maxn];vector<vector<int> >myv;bool iss[Maxn];void tarjan(int cur){    int ne;    low[cur]=dfn[cur]=++dep;    sta[++sc]=cur;    iss[cur]=true;    for(int i=0;i<myv[cur].size();i++)    {        ne=myv[cur][i];        if(!dfn[ne])        {            tarjan(ne);            if(low[ne]<low[cur])                low[cur]=low[ne];        }        else if(iss[ne]&&dfn[ne]<low[cur])            low[cur]=dfn[ne];    }    if(low[cur]==dfn[cur])    {        bc++;        do        {            ne=sta[sc--];            iss[ne]=false;            in[ne]=bc;            nu[bc]++;        }while(ne!=cur);    }}void solve(){    dep=sc=bc=0;    memset(nu,0,sizeof(nu));    memset(iss,false,sizeof(iss));    memset(dfn,0,sizeof(dfn));    for(int i=1;i<=n;i++)        if(!dfn[i])            tarjan(i);}int main(){    //freopen("in.txt","r",stdin);   //freopen("out.txt","w",stdout);   int t,kcas=0;   scanf("%d",&t);   while(t--)   {       scanf("%I64d%I64d",&n,&m);       myv.clear();       myv.resize(n+1);       for(int i=1;i<=m;i++)       {           int a,b;           scanf("%d%d",&a,&b);           myv[a].push_back(b);       }       solve();       if(bc==1)       {           printf("Case %d: -1\n",++kcas);           continue;       }       memset(dei,0,sizeof(dei));       memset(deo,0,sizeof(deo));       for(int i=1;i<=n;i++)            for(int j=0;j<myv[i].size();j++)            {               int ne=myv[i][j];               if(in[ne]!=in[i])               {                   dei[in[ne]]++;                   deo[in[i]]++;               }            }      ll ans=INF;      for(int i=1;i<=bc;i++)      {          if(!dei[i])            ans=min(ans,(ll)nu[i]);          else if(!deo[i])            ans=min(ans,(ll)nu[i]);      }      printf("Case %d: %I64d\n",++kcas,n*n-n-ans*(n-ans)-m);   }    return 0;}



[Tarjan] HDU 4635 strongly connected

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.