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

3 篇文章 0 订阅
3 篇文章 0 订阅

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<iostream>
#include<cstdio>
#include<cstring>
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];
template<class A>inline bool Min(A &x,const A &y){return x>y?(x=y):false;}
template<class A>inline bool Max(A &x,const A &y){return x<y?(x=y):false;}
inline void add(int u,int v){e[pos].v=v;e[pos].nxt=head[u];head[u]=pos++;}
void dfs(int u){
	low[u]=pre[u]=++cok;//节点编号不能ck++!!!!
	stk[top++]=u;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].v;
		if(!pre[v]){
			dfs(v);
			Min(low[u],low[v]);//如果该点的子节点能达到更高的节点
		}
		else if(!fa[v])//只能通过没有确定编号的其他点
		Min(low[u],pre[v]);
	}
	if (low[u]==pre[u]){//如果u只能连回自己,那么u就是第一个被发现的节点
		cnt++;
		do{fa[stk[top-1]]=cnt;node[cnt].sum++;}
		while(stk[--top]!=u);
	}
}
void tarjan(int n){
	top=cok=cnt=0;
	memset(pre,0,sizeof(pre));
	memset(fa,0,sizeof(fa));
	for(int i=1;i<=n;i++)
	if(!pre[i])dfs(i);
}
int main(){
	int T;
	scanf("%d",&T);
	int id=1;
	while(T--){
		int n,m,x,y;
		scanf("%d%d",&n,&m);
		memset(head,-1,sizeof(head));
		pos=0;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&x,&y);
			add(x,y);
		}
		for(int i=0;i<=MAX;i++)node[i].clear();
		tarjan(n);
		cout<<"Case "<<(id++)<<": ";
		if(cnt==1){
			cout<<-1<<endl;
			continue;
		}
		for(int u=1;u<=n;u++){
			for(int i=head[u];i!=-1;i=e[i].nxt){
					int v=e[i].v;
					if(fa[u]!=fa[v]){
						node[fa[u]].out++;
						node[fa[v]].in++;
					}
			}
		}
		long long  res=0;
		for(int i=1;i<=cnt;i++){
			if(node[i].in==0||node[i].out==0){
				long long a=node[i].sum;
				long long b=n-a;
				long long ans=a*(a-1)+b*(b-1)+a*b-m;
				Max(res,ans);
			}
		}
		cout<<res<<endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值