Strongly connected HDU - 4635(Tarjan+缩点)

 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

题意:给你一个有向图,问你最多能添加多少条边使得这个图依然不是强联通的。
并且图中没有重边和自环。
思路:思路来源
最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边,假设X部有x个点,Y部有y个点,有x+y=n,同时边数F=x*y+x*(x-1)+y*(y-1),整理得:F=N*N-N-x*y,(然后去掉已经有了的边m,就是答案),当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部的点数的个数差距就要越大,所以首先对于给定的有向图缩点,对于缩点后的每个点,如果它的出度或者入度为0,那么它才有可能成为X部或者Y部,所以只要求缩点之后的出度或者入度为0的点中,包含节点数最少的那个点,令它为一个部,其它所有点加起来做另一个部,就可以得到最多边数的图了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int VM=200010;
const int EM=200010;
const int INF=0x3f3f3f3f;

struct Edge
{
    int to,nxt;
} edge[EM<<1];

long long n,m,cnt,head[VM];
long long dep,top,atype;
long long dfn[VM],low[VM],vis[VM],stack[VM],belong[VM],indeg[VM],outdeg[VM],sum[VM];

void addedge(int cu,int cv)
{
    edge[cnt].to=cv;
    edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
}

void Tarjan(int u)
{
    dfn[u]=low[u]=++dep;
    stack[top++]=u;
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    int j;
    if(dfn[u]==low[u])//这个判断条件说明该点是一个强连通分量,进行缩点
    {
        atype++;//记录连通分量的个数
        do
        {
            j=stack[--top];//弹出栈顶元素
            belong[j]=atype;//j点属于哪个连通分量,belong[i] = j 表示原图的点i缩点后为点j 
            sum[atype]++;   //记录每个连通分量中点的个数
            vis[j]=0;//出栈解除标记
        }
        while(u!=j);
    }
}

void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    dep=0,  top=0,  atype=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(belong,0,sizeof(belong));
    memset(indeg,0,sizeof(indeg));
    memset(outdeg,0,sizeof(outdeg));
    memset(sum,0,sizeof(sum));
}

int main()
{
    int t,cases=0;
    scanf("%d",&t);
    while(t--)
    {
        cin>>n>>m;
        if(n==1)
        {
            printf("Case %d: -1\n",++cases);
            continue;
        }
        init();
        int u,v;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        for(int i=1; i<=n; i++)
            if(!dfn[i])
                Tarjan(i);
        if(atype==1)
        {
            printf("Case %d: -1\n",++cases);
            continue;
        }
        for(int u=1; u<=n; u++)
            for(int i=head[u]; i!=-1; i=edge[i].nxt)
            {
                int v=edge[i].to;
                if(belong[u]!=belong[v])
                {
                    outdeg[belong[u]]++;
                    indeg[belong[v]]++;
                }
            }
        long long ans=0,tmp;
        for(long long i=1; i<=atype; i++)
            if(indeg[i]==0 || outdeg[i]==0)     //找出度或者入度为0的点,包含节点数最少的那个点
            {
                //令它为一个部,其它所有点加起来做另一个部,就可以得到最多边数的图了
                tmp=sum[i];
                ans=max(ans,tmp*(tmp-1)+(n-tmp)*(n-tmp-1)+tmp*(n-tmp)-m);
            }
        cout<<"Case "<<(++cases)<<": "<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值