强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通

Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1802    Accepted Submission(s): 746


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
 
 分析:首先进行强连通对原图进行缩点,然后对于缩点后的图形,只考虑出度为0和入度为0的联通块,一出度为0说明,该联通块里的所有点都不能和该联通块外面的所有点连单向边,总共有sum[i]*(n-sum[i])条边不能连接,一个连通图两两建边,共有n*(n-1)条边,但是题目已有m条边已经连接,所以现在可以连接的有n*(n-1)-m-sum[i]*(n-sum[i]);入度为0的联通块同理,所有度为0的块计算后取较大值即为答案。
程序:
#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define inf 0x3f3f3f3f
#define M 200009
#define eps 1e-8
#define INT int
#define LL __int64
using namespace std;
struct node
{
    int u,v,next;
}edge[M*2];
int head[M];
int dfn[M];
int belong[M];
int low[M];
int use[M];
int sum[M];
int in[M];
int out[M];
int t,indx,num,cnt,m,n;
stack<int>q;
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++indx;
    q.push(u);
    use[u]=1;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(use[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        int p;
        num++;
        do
        {
            p=q.top();
            q.pop();
            use[p]=0;
            belong[p]=num;
            sum[num]++;

        }while(p!=u);
    }
}
void slove()
{
    num=indx=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(use,0,sizeof(use));
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
    if(num==1)
    {
        printf("-1\n");
        return;
    }
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    for(int i=0;i<t;i++)
    {
        int u=edge[i].u;
        int v=edge[i].v;
        if(belong[u]!=belong[v])
        {
            out[belong[u]]++;
            in[belong[v]]++;
        }
    }
    LL ans=0;
    for(int i=1;i<=num;i++)
    {
        if(!out[i]||!in[i])
        {
            LL s=(LL)n*(n-1)-m-(LL)sum[i]*(n-sum[i]);
            if(ans<s)
                ans=s;
        }
    }
    printf("%I64d\n",ans);
}
int main()
{
    int T,a,b,kk=1;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
        }
        printf("Case %d: ",kk++);
        slove();
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/mypsq/p/4484479.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值