HDU4635-Strongly connected

Strongly connected

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


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
 

Recommend
zhuyuanchen520
 


题意:给出N个顶点,M条边的有向图,问最多加入多少条边之后,这个图仍旧是一个简单图(简单图:无重边,无自环),并且不是强联通的。如果原始的图就是强联通的话就输出 -1.

解题思路:找出强联通块,计算每个连通块内的点数。将点数最少的那个连通块单独拿出来,其余的连通块合并成一个连通分量。 假设第一个连通块的点数是x第二个连通块的点数是 y, 已经有的边数是m,则ans =  x*(x-1) + y*(y-1) + x*y - m;(强联通块内边数最多是 n*(n-1) ),  
最少点数的强联通分量要满足出度或者入度为 0才行,不然是不满足的。


#include <iostream>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <vector>  
#include <map>  
#include <set>  
#include <queue>  
#include <stack>  
#include <functional>  
#include <climits>  
  
using namespace std;  
  
#define LL long long  
const LL INF=0x3f3f3f3f3f3f3f3f;  
  
const int N=101000;  
int n, m;  
struct Node  
{  
    int v,nt;  
}edge[N];  
int s[N],cnt;  
int dfn[N],low[N],id[N],dep;  
bool vis[N],instack[N]; 
int in[N],out[N];  
int res;
LL sum[N];  
stack<int>st;  
  
void AddEdge(int u,int v)  
{  
    edge[cnt].v=v;  
    edge[cnt].nt=s[u];  
    s[u]=cnt++;  
}  
  
void tarjan(int u)  
{  
    st.push(u);  
    instack[u]=true;  
    vis[u]=true;  
    dfn[u]=low[u]=++dep;  
    for(int i=s[u]; ~i; i=edge[i].nt)  
    {  
        int v=edge[i].v;  
        if(!vis[v])  // 生成树的边.  
        {  
            tarjan(v);  
            low[u]=min(low[u],low[v]);  
        }  
        else if(instack[v])//在栈中,回边.  
            low[u]=min(low[u],dfn[v]);  
    }  
    if(dfn[u]==low[u])//顶点u为根的子树是一个强连同块  
    {  
        int t;  
        do  
        {  
            id[t=st.top()]=res;
			sum[res]++;
            st.pop();  
            instack[t]=false; //low[t] = n;  
        }  
        while(t!=u);  
        res++;//强连通分量增加  
    }  
}  
  
void solve()  
{  
    res=0,dep=0;
	memset(sum,0,sizeof sum);
    while(!st.empty()) st.pop();  
    memset(vis,0,sizeof vis);  
    memset(instack,0,sizeof instack);  
    for(int i=1; i<=n; i++)  
        if(!vis[i]) tarjan(i);  
    // Debug  
        /* for(int i = 1; i <= n; i++) 
             printf("dfn[%d] = %d, low[%d] = %d\n", i,dfn[i], i,low[i]); 
         for(int i = 1; i <= n; i++) 
             printf("id[%d] = %d\n", i, id[i] );*/  
    memset(in,0,sizeof in);
	memset(out,0,sizeof out);
    for(int u=1; u<=n; u++)  
    {  
        for(int i=s[u]; ~i; i=edge[i].nt)  
        {  
            int v=edge[i].v;
			if(id[u]!=id[v])
			{
				in[id[v]]++;
				out[id[u]]++;
			}
        }  
    }  
    LL x=INF;
	for(int i=0;i<res; i++)
    {
        if(!out[i]||!in[i])
            x=min(x,sum[i]);
    }
    LL y=1LL*n-x;
	LL ans=x*(x-1)+y*(y-1)+x*y-m;
	if(res==1) printf("-1\n");
	else printf("%lld\n",ans);
}  
  
int main()  
{
	int t,cas=1;
	scanf("%d",&t);
    while(t--)  
    {
		scanf("%d%d",&n,&m);
        memset(s,-1,sizeof s);  
        cnt=0;  
        for(int i=0; i<m; i++)  
        {  
            int u,v;  
            scanf("%d%d",&u,&v);  
            AddEdge(u,v);  
        }
		printf("Case %d: ",cas++);
        solve();  
    }  
    return 0;  
}  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值