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
2013 Multi-University Training Contest 4
 

Recommend
zhuyuanchen520
 


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

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


#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
  
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];  
stackst;  
  
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

你可能感兴趣的:(----连通图,HDU)