2013 杭电多校联合赛 第四场D题 4635 Strongly connected

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.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

题意:给你一个图,问你最多能添多少条边使得这个图仍然是无向非强连通图。

 思路:用tarjan缩点后枚举每一个入度或出度为0的点a,设这个点所代表的强连通的点数为x,将其他点通过相互加双向边的方法合成一个强连通的点b,那么这个点所代表的强连通图的点数就是n-x,然后将这个点与原来的点连x*(n-x)条单向边,同时加上两个点内所代表的强连通图的最大边数x*(x-1),(n-x)*(n-x-1),再减去原来已经有的边数m既是通过该点与其他点所能加的最多边的条数。最后取这些点所能加到边数的最大值就是答案了。


比赛的时候出现了一个很坑的情况,我们完全把正确的算法给讨论出来结果确发现没人会写tarjan。也是个教训吧,以前碰到过几次的算法却从来都没有认真去搞懂。

附AC代码:

#include
#include
#include
using namespace std;
#define MAX 100005
int head[MAX],pos;
int cnt,fa[MAX],pre[MAX],low[MAX],cok;
int top,stk[MAX];
struct L{
	int v,nxt;
}e[MAX];
struct LL{
	int sum,in,out;
	void clear(){sum=in=out=0;};
}node[MAX];
templateinline bool Min(A &x,const A &y){return x>y?(x=y):false;}
templateinline bool Max(A &x,const A &y){return x


你可能感兴趣的:(ACM,tarjan,图论)