[kuangbin带你飞]专题九 连通图 Strongly connected HDU - 4635

本文探讨了如何在保持图的简单性和非强连通性的前提下,为给定的有向图添加尽可能多的边。通过分析节点数量、已有边的数量及连接方式,文章提出了一种有效的算法来计算可添加的最大边数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
	}	
}
### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了一本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另一个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定一组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单的搜索题目而言,在为期约两周的时间里完成了这一部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进一步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值