HDU - 4635 Strongly connected

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

题意:给定一个图,求在不成为强连通的条件下最多能添加多少条边。如果本来就是连通图,输出-1.

思路:先用tarjan缩点。不成为强连通,只需要一个点被孤立(不能形成回路),其他点全部连接。这个被孤立的点需要缩点中的点尽量少,且需要入度或者出度为零。假设被最少的缩点中有p个点,则其他可以任意连接的点为di=n-p。di个点一共可以连接di*(di-1)条边,最少缩点中要只能进或则只能出(入度为零或出度为零)。所以最少缩点里面的点也可以任意连接,总数为p*(p-1)。被孤立的点连接其他点(只进或只出),一共有p*n条,则总数为:di*(di-1)+p*(p-1)+p*n。然后减去题目给出的边数m就是答案。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stack>
#define M 100010
using namespace std;
struct path
{
    int to,nextt;
}A[M];
stack<int>q;
int head[M],DFN[M],LOW[M],book[M],re[M],in[M],out[M],sum[M];
int t,ph=0,n,m,x,y,tot,carry,indox;
void init()
{
    tot=carry=indox=0;
    memset(in,0,sizeof(in));
    memset(head,-1,sizeof(head));
    memset(book,0,sizeof(book));
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    memset(out,0,sizeof(out));
    memset(sum,0,sizeof(sum));
    return ;
}
void add(int u,int v)
{
    A[tot].to=v;
    A[tot].nextt=head[u];
    head[u]=tot++;
    return ;
}
void tarjan(int u)
{
    int tem;
    DFN[u]=LOW[u]=++indox;
    book[u]=1;
    q.push(u);
    for(int i=head[u];i!=-1;i=A[i].nextt)
    {
        tem=A[i].to;
        if(DFN[tem]==-1)
        {
            tarjan(tem);
            LOW[u]=min(LOW[u],LOW[tem]);
        }
        else if(book[tem])
        {
            LOW[u]=min(LOW[u],DFN[tem]);
        }
    }
    if(DFN[u]==LOW[u])
    {
        ++carry;
        do
        {
            tem=q.top();
            q.pop();
            book[tem]=0;
            re[tem]=carry;
        }while(tem!=u);
    }
    return ;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(DFN[i]==-1)
                tarjan(i);
        }
        printf("Case %d: ",++ph);
        if(carry==1)
        {
            printf("-1\n");
            continue;
        }
        int v;
        for(int i=1;i<=n;i++)
        {
            sum[re[i]]++;
            for(int j=head[i];j!=-1;j=A[j].nextt)
            {
                v=A[j].to;
                if(re[i]!=re[v])
                {
                    in[re[v]]++;
                    out[re[i]]++;
                }
            }
        }
        int pan=0x3f3f3f3f,k;
        for(int i=1;i<=carry;i++)
        {
            if(pan>sum[i]&&(in[i]==0||out[i]==0))
            {
                pan=sum[i];
                k=i;
            }
        }
        int di=n-pan;
        int ans=di*(di-1)+di*pan-m+pan*(pan-1);
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/84074031