hdu4635Strongly connected 【求最多加多少边仍不是强连通分量】

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
 

为了防止做过的题就饭吃了,我这次要做完题就写博客==

题意:就是问加多少边依旧无环,无scc

做法:开始依旧想到了

hdu2767Proving Equivalences【STL版SCCTarjan+缩点】(有注释)

这个题,自以为是的觉得在这个题的基础上-1就好了,然后一个是最多边数,一个是求最少边数,怎么可能一样==

其实只不过多了一个完全图而已,对于一个有n个节点的完全图而言,他有n*n个边,对于这个图而言,最终的结果必然是只有两个集合(即缩点之后的团)团内是scc无环,彼此之间只有单向的通道,设两个团的节点个数分别为x,y则最终可加的边数是n*n-n-x*y-m

in0 out0 是0还是1 要变一下!!

还有就是判-1,只用团的个数,最终结果可能等于0!

#include 
#include
#include
#include
#include
#include
using namespace std;
#define maxn 200005
vectorG[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stackS;
int in0[maxn],out0[maxn];
void dfs(int u)
{
    pre[u]=lowlink[u]=++dfs_clock;
    S.push(u);////每搜索到一个点,压入栈中
    for(int i=0;i=0) printf("Case %d: %lld\n",cas++,ans);
        else printf("Case %d: -1\n",cas++);
    }
    return 0;
}



你可能感兴趣的:(—图论,———连通性,2-SAT)