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

分析:

1.首先要把这个图变成一个完全图,然后减去最初的m条边。
2.因为要求加入的边最大化,你需要强联通缩点,把入度为0或者出度为0的内含节点最少的联通块找出来,然后再减去最小联通块内的点与其他点的连接边就可以了。(减去的最少,剩余的也越多)
3.考虑当只有一个连通图时,不满足,输出-1;

AC代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=1e5+10;
int t,n,m,u,v,tot,k,mi,Case;
vector<int>ve[M];
int dfn[M],low[M],co[M],out[M],in[M],num[M];
stack<int>st;
void tarjan(int x)
{
    dfn[x]=low[x]=++tot;
    st.push(x);//入栈
    for(int i=0; i<ve[x].size(); i++)
    {
        int y=ve[x][i];
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(!co[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x])///某个节点回溯之后的low【u】值还是==dfn【u】的值,那么这个节点无疑就是一个关键节点(为强连通分量的一个顶点。)
    {
        k++;/**缩点,将连通分量缩成一个点,建入新树中*/
        while(1)
        {
            int a=st.top();
            st.pop();
            co[a]=k;/*看做建了一个新树,只有用强连通分量的顶点建入树中*/
            num[k]++;/*记录某连通分量内有多少个点*/
            if(a==x)//(遍历该连通分量内有多少个点【在a前的点,均为一个连通分量】)
                break;
        }
    }
}
int main()
{
    scanf("%d",&t);
    Case=0;
    while(t--)
    {
        tot=k=0;/**初始化*/
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
       memset(co,0,sizeof(co));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            ve[i].clear();
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            ve[u].push_back(v);
        }
        for(int i=1; i<=n; i++)
            if(!dfn[i])
                tarjan(i);
        mi=inf;
        for(int i=1; i<=n; i++)
            for(int j=0; j<ve[i].size(); j++)
                if(co[i]!=co[ve[i][j]])
                    out[co[i]]++,in[co[ve[i][j]]]++;
        for(int i=1; i<=k; i++)
            if(!in[i]||!out[i])
                mi=min(mi,num[i]);
        printf("Case %d: ",++Case);
        if(k==1)
            printf("-1\n");
        else
            printf("%d\n",n*(n-1)-m-mi*(n-mi));
    }
    return 0;
}
/**想法一:
找出强联通块,计算每个连通块内的点数。将点数最少的那个连通块单独拿出来,
其余的连通块合并成一个连通分量。 那么假设第一个连通块的 点数是 x
第二个连通块的点数是 y
一个【强】连通图最多(每两个点之间,至少存在一条课互相到达的路径)的边数为n*(n-1)
一个连通图的边数至少为n*(n-1)- x*y + 1
则非连通图最多的边数为n*(n-1)- x*y 即 x*(x-1)+ y*(y-1)+ x*y
因为原图中已经有m条边 所以最多加 x*(x-1)+ y*(y-1)+ x*y - m 条边
这里最少点数的强联通分量要满足一个条件,就是出度或者入度为 0才行,不然是不满足的。
二:
缩点后
这其实就相当于一个完全图至少减去多少条边,使之变成非强连通图
肯定减去连通分量里点最少的那个了*/

备战ing,题目分析简略,见谅,转载请注明出处。。。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值