Strongly connected HDU - 4635 (有向图强连通分量 DAG)

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

 

Source

2013 Multi-University Training Contest 4

考察有向图的强连通分量。

因为要寻找最大的填边数量,所以图中节点分成两部分,第一部分代表孤立的一个连通分量,除第一部分的其他节点构成第二部分。

此时能添加的边的最大数量为x*y+x*(x-1)+y*(y-1),这个数量代表的就是图中边数最多的情况下整个图形不是强连通的

(也就是从x到y存在边,但是从y到x不存在边,那么能够添加x*y条从x到y的边,x*(x-1)代表当前有向连通分量里面能加边的最大的数量)。

x*y+x*(x-1)+y*(y-1)=...(过程省略)...=n*(n-1)-x*y。

因为n已知,所以只需要求x,y的最小值就行了。

#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e5+100;

struct Edge
{
    int to,next;
} edge[maxn*2];

int head[maxn],tot;
int LOW[maxn],DFN[maxn],Stack[maxn],Belong[maxn];
int Index,top;
int scc;
int Instack[maxn];
int num[maxn];
int in[maxn],out[maxn];

void debug()
{
    cout<<"____I Love Topcoder!____"<<endl;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void Tarjan(int u)
{
    int v;
    LOW[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        v=edge[i].to;
        if(!DFN[v])
        {
            Tarjan(v);
            if(LOW[u]>LOW[v])
                LOW[u]=LOW[v];
        }
        else if(Instack[v]&&LOW[u]>DFN[v])
            LOW[u]=DFN[v];
    }
    if(LOW[u]==DFN[u])
    {
        scc++;
        do
        {
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
            num[scc]++;
        }
        while(u!=v);
    }
}

void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    memset(num,0,sizeof(num));
    Index=scc=top=0;
    for(int i=1; i<=N; ++i)
    {
        if(!DFN[i])
            Tarjan(i);
    }
}

void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}

int main()
{
    int T;
    cin>>T;
    for(int icase=1; icase<=T; ++icase)
    {
        init();
        int n,m,u,v;
        cin>>n>>m;
        for(int i=1; i<=m; ++i){
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        solve(n);
        if(scc==1){
            printf("Case %d: -1\n",icase);
            continue;
        }

        for(int i=1;i<=scc;++i){
            in[i]=0;
            out[i]=0;
        }
        for(int i=1;i<=n;++i){
            for(int u=head[i];u!=-1;u=edge[u].next){
                int v=edge[u].to;
                if(Belong[v]==Belong[i]) continue;
                in[Belong[v]]++;
                out[Belong[i]]++;

            }
        }
        long long puts=(long long)n*(n-1)-m;//去掉桥后的最大边数
        long long x=0;
        for(int i=1;i<=scc;++i)
        {
            if(in[i]==0||out[i]==0)
            {
                x=max(x,puts-(long long)(n-num[i])*num[i]);
            }
        }
        printf("Case %d: %lld\n",icase,x);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值