HDU 4635 Strongly connected

Strongly connected

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


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
 

题目大意:给你一个图,问最多添加多少条边使他仍然不是强连通图,如果已经是强连通图,就输出-1

先设已经有一个图满足题目要求。首先要使它不是一个强连通图,那最多只能有两个强连通分量,而且这两个强连通分量都要是一个完全图,而且有一个强联通分量里的每一个点都要有指向另一个强连通分量的每一个点的边,分别设这两个强联通分量里的点为x,y。首先他们是完全图,所以有x*(x-1)+y*(y-1)条边,每个点指向另一个图的所有点则有x*y条边,所以最后答案就是x*(x-1)+y*(y-1)+x*y-m条边

#include<iostream>
#include<vector>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+7;
vector<int> vec[maxn];
stack<int> sta;
int dfn[maxn];
int low[maxn];
bool vis[maxn];
int in[maxn];
int out[maxn];
int num[maxn];
int sum[maxn];
int index,scc_cnt;
void init()
{
    index=scc_cnt=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,false,sizeof(vis));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    for(int i=0;i<maxn;i++) vec[i].clear();
    while(!sta.empty()) sta.pop();
    return;
}
void tarjan(int node)
{
    dfn[node]=low[node]=++index;
    vis[node]=true;
    sta.push(node);
    int len=vec[node].size();
    for(int i=0;i<len;i++)
    {
        int v=vec[node][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[node]=min(low[node],low[v]);
        }
        else if(vis[v])
        {
            low[node]=min(low[node],dfn[v]);
        }
    }
    if(dfn[node]==low[node])
    {
        ++scc_cnt;
        int v=-1;
        while(node!=v)
        {
            v=sta.top();
            sta.pop();
            vis[v]=false;
            num[v]=scc_cnt;
            sum[scc_cnt]++;
        }
    }
    return;
}
int main()
{
    int test;
    scanf("%d",&test);
    for(int Case=1;Case<=test;Case++)
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                tarjan(i);
            }
        }
        if(scc_cnt==1)
        {
            printf("Case %d: -1\n",Case);
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                int len=vec[i].size();
                for(int j=0;j<len;j++)
                {
                    int v=vec[i][j];
                    if(num[i]!=num[v])
                    {
                        out[num[i]]++;
                        in[num[v]]++;
                    }
                }
            }
            int ans=-1;
            for(int i=1;i<=scc_cnt;i++)
            {
                if(in[i]==0||out[i]==0)
                {
                    int tmp=sum[i];
                    ans=max(ans,tmp*(n-tmp)+(n-tmp)*(n-tmp-1)+tmp*(tmp-1)-m);
                }
            }
            printf("Case %d: %d\n",Case,ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值