hdu acm 4635 Strongly connected

Problem Description
Give 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 strongly 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.
 

Input
The 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.
 

Output
For 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 Input
  
  
3 3 3 1 2 2 3 3 1 3 3 1 2 2 3 1 3 6 6 1 2 2 3 3 1 4 5 5 6 6 4
 

Sample Output
  
  
Case 1: -1 Case 2: 1 Case 3: 15
tarjan算法。。。
解题关键:要使添加最多的边使得整张图仍为非强连通分量,最后连成的模样一定是只剩下2个强连通分量(X和Y),只需要找出入度=0或出度=0的联通分量中联通分量点数最小的那个。(X+Y=n,只有X与Y差距最大时,X*Y最小。)n个顶点最多可以连n*(n-1)条边。要去掉原来有的m条边。
ans=n*(n-1)-m-X*Y;
#include<iostream>
using namespace std;
#include<string.h>
#include<stdio.h>
#include<algorithm>
#define maxn 0x7fffffff
int n,m,instack[100010],stackn[100010];
int low[100010],DFN[100010],head[100010];
int f,top,scnt,cnt,Belong[100010];
long long num[100010];
struct node
{
    int e,next;
}edge[100010];
void add(int s,int e)
 {
     edge[f].e=e;
     edge[f].next=head[s];
     head[s]=f++;
 }
void tarjan(int s)
 {
     int t,k,i;
     DFN[s]=low[s]=++cnt;
     stackn[top++]=s;
     instack[s]=1;
     for(i=head[s];i!=-1;i=edge[i].next)
     {
         k=edge[i].e;
         if(!DFN[k])
         {
             tarjan(k);
             low[s]=min(low[k],low[s]);
         }
         else if(instack[k])
         {
             low[s]=min(low[s],DFN[k]);
         }
     }
     if(low[s]==DFN[s])
     {
         scnt++;
         num[scnt]=0;
         do
         {
             t=stackn[--top];
             Belong[t]=scnt;
             num[scnt]++;
             instack[t]=0;
         }
         while(s!=t);
     }
 }
int main()
{
    int t,i,a,b,j,h,k;
    long long s1,ans,h1;
    scanf("%d",&t);
    for(j=1;j<=t;j++)
    {
        scanf("%d %d",&n,&m);
        memset(head,-1,sizeof(head));
        f=1;
        for(i=0;i<m;i++)
        {
            scanf("%d %d",&a,&b);
            add(a,b);
        }
        memset(DFN,0,sizeof(DFN));
        memset(instack,0,sizeof(instack));
        memset(low,0,sizeof(low));
        scnt=top=cnt=0;
        for(i=1;i<=n;i++)
            if(!DFN[i])
              tarjan(i);
         ans=0;s1=maxn;
       int in[100010],out[100010];
       memset(in,0,sizeof(in));
       memset(out,0,sizeof(out));
       for(i=1;i<=n;i++)
          for(k=head[i];k!=-1;k=edge[k].next)
       {
           int v=edge[k].e;
           if(Belong[i]!=Belong[v])
               {
                  out[Belong[i]]++;
                  in[Belong[v]]++;
               }
       }
       for(i=1;i<=scnt;i++)
        if(!out[i]||!in[i])
        { 
            s1=min(s1,num[i]);
        }
      ans=n*(n-1)-m-s1*(n-s1);
      if(scnt==1)ans=-1;
     printf("Case %d: %I64d\n",j,ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值