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
题意:最多添加几条边使得图还不是强连通图
1.若本来就是连通的,printf(“-1”);
2. 有向连通图 n(n-1)条边 最后应该是两个强连通的点,一个点有向另一个点的边
long long sss=(long long)n*(n-1)-m;
long long ans=0;
for(int i=1;i<=block;i++)
{
if(ru[i]==0||chu[i]==0)
ans=max(ans,sss-(long long)belong_num[i]*(n-belong_num[i]));
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200000+100;
struct node
{
int v,next;
}edge[maxn*2+100];
struct A
{
int u,v;
}ab[maxn*2+100];
int head[maxn],cnt,low[maxn],dfn[maxn],stack[maxn],instack[maxn],ru[maxn],chu[maxn],belong[maxn];
int index,top,block,bridge,vis[maxn*2+100],belong_num[maxn];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(stack,0,sizeof(stack));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
memset(ru,0,sizeof(ru));
memset(chu,0,sizeof(chu));
memset(belong_num,0,sizeof(belong_num));
index=top=bridge=block=0;
}
void add_edge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
stack[top++]=u;
instack[u]=1;
int v;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
block++;
int mm=0;
do
{
mm++;
v=stack[--top];
instack[v]=0;
belong[v]=block;
}while(u!=v);
belong_num[block]=mm;
}
}
int main ()
{
int t;
scanf("%d",&t);
for(int j=1;j<=t;j++)
{
int n,m;
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int xx,yy;
scanf("%d%d",&xx,&yy);
ab[i].u=xx;
ab[i].v=yy;
add_edge(xx,yy);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
if(block==1)
{
printf("Case %d: -1\n",j);
continue;
}
for(int i=1;i<=m;i++)
{
int xx=ab[i].u;
int yy=ab[i].v;
if(belong[xx]!=belong[yy])
{
chu[belong[xx]]++;
ru[belong[yy]]++;
}
}
long long sss=(long long)n*(n-1)-m;
long long ans=0;
for(int i=1;i<=block;i++)
{
if(ru[i]==0||chu[i]==0)
ans=max(ans,sss-(long long)belong_num[i]*(n-belong_num[i]));
}
printf("Case %d: ",j);
printf("%lld\n",ans);
}
}