/*
http://acm.hdu.edu.cn/showproblem.php?pid=4635
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 477 Accepted Submission(s): 212
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
解析:
题意:
解析:给出一个有向图,问最多可以加多少边使得任意两点无论正反方向皆可到达
思路:最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边,假设X部有x个点,Y部有y个点,有x+y=n,同时边数F=x*y+x*(x-1)+y*(y-1),整理得:F=N*N-N-x*y,当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部的点数的个数差距就要越大,所以首先对于给定的有向图缩点,对于缩点后的每个点,如果它的出度或者入度为0,那么它才有可能成为X部或者Y部,所以只要求缩点之后的出度或者入度为0的点中,包含节点数最少的那个点,令它为一个部,其它所有点加起来做另一个部,就可以得到最多边数的图了
故:1要用tarjan算法进行缩点
2.缩点后重建图
3.找出出度或入度为0且结点最小点,套用公式
46MS 5436K 2350 B C++
*/
#pragma comment(linker, "/STACK:1024000000,1024000000")/
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100000+10;
int pre[maxn],scc[maxn],low[maxn],st[maxn];
int nodenum[maxn],head1[maxn],head2[maxn],vis[maxn];
int dfsn,sccn,top,ans,t1,t2,m,n;
struct Edge
{
int s;
int t;
int next;
}edge[maxn];
struct Node
{
int fn;//出度
int tn;//入度
int num;//缩点后每个“结点”含有的节点数
}node[maxn];
int min(int a,int b)
{
return anode[i].num)
Min=node[i].num;
}
sum+=node[i].num;
}
ans=sum*sum-sum-Min*(sum-Min)-m;
}
int main()
{
int T,i,j,u,v;
int c=0;
scanf("%d",&T);
while(T--)
{ init();
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add1(u,v);
}
work();
printf("Case %d: %d\n",++c,ans);
}
return 0;
}