当前位置:网站首页>hdu4635 Strongly connected (tarjan calculates strongly connected components + shrink points + ideas)
hdu4635 Strongly connected (tarjan calculates strongly connected components + shrink points + ideas)
2022-08-08 09:02:00 【51CTO】
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2714 Accepted Submission(s): 1128
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
Sample Output
Source
2013 Multi-University Training Contest 4
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you: 6022 6021 6020 6019 6018
Statistic |
Submit |
Discuss |
Note
题意大概就是:给你一个DAG图,After asking how many edges to add at mostDAGThe graph is still not strongly connected.
对于这道题而言.We can think in reverse.If the graph is a strongly connected graph,So how many edges can there be at most.
Assuming the last requestedDAGThe figure has two partsX图和Y图.此时X图和YEach graph must be a strongly connected graph,并且X图和YThere are only one-way edges between graphs.
这样才能使DAGThe maximum number of graph edges
假设X图的顶点数为x,Y图的顶点数为y,则DAG图的顶点数为x+y=n
则有:
- 如果使XGraph Strongly Connected,边数最多为x*(x-1)(即Xto each vertex of the graphXEvery other vertex of the graph has a one-way edge)
- 如果使YGraph Strongly Connected,边数最多为y*(y-1)
- X图和YThere are only single-item edges between graphs,The maximum number of sides is x*y(即Xto each vertex of the graphYEach vertex of the graph has a one-way edge)
The number of sides can be obtained from the aboveF(DAG)=x*y+x*(x-1)+y*(y-1)=n*n-n-x*y.即DAGThe maximum number of edges in the graph is n*n-n-x*y.
由于n一定,So this question can be transformed into a requestx*y的最小值,又因为x+y=n.所以x和yThe larger the difference, the smaller the result.
Then this question turns into a searchDAGThe smallest strongly connected component of a vertex in a graph.
F(DAG)Subtract the number of existing edgesmThat is the desired result.
Compute strongly connected components can be usedtarjan算法,In the statistical process, the strongly connected components can be shortened~由于我们要求X图和YThere are only one-way edges between graphs,所以
我们要找的Y图 Must be in-degree0或者出度为0的强连通分量,否则 此DAGThe graph is a strongly connected graph .
代码如下:
边栏推荐
- 让百姓消费更安全更放心更满意 江苏出台放心消费创建示范认定管理办法
- Do you really know IP addresses?
- 推荐下载软件
- 67:第五章:开发admin管理服务:20:开发【解冻/冻结用户,接口】;(用户状态变更后,需要刷新用户状态,即变更用户会话信息:我们一般通过“删除redis中会话信息,强制用户重新登录“来做的;)
- 【回归预测】基于GPML工具箱的高斯过程回归附matlab代码
- 浏览器中输入URL后到底发生了什么?
- 数据治理(三):数据质量管理
- [Raspberry Pi] vim editor
- FRED应用:TMT MOBIE成像光谱仪的概念设计阶段杂散光分析
- LabVIEW前面板和程序框图的最大尺寸
猜你喜欢

idea big data tools submit flink tasks

67:第五章:开发admin管理服务:20:开发【解冻/冻结用户,接口】;(用户状态变更后,需要刷新用户状态,即变更用户会话信息:我们一般通过“删除redis中会话信息,强制用户重新登录“来做的;)

22-08-06 Xi'an EasyExcel implements dictionary table import and export

浏览器中输入URL后到底发生了什么?

Kotlin Compose MiUI13.0.4 版本 Livedata不生效

方案 | 医疗单据OCR识别,为医保零星报销打造安全屏障

Literature Learning (part33)--Clustering by fast search and find of density peaks

BLOB, TEXT, GEOMETRY or JSON column 'xxxx' can't have a default value

My MySQL installation that is how to solve

COMSOL Multiphysics 6.0软件安装包和安装教程
随机推荐
LVS负载均衡群集及NAT模式群集
Golang实现sha256或sha512加密
[ 深度学习 ] 课程学习(Curriculum Learning)
My MySQL installation that is how to solve
Recommended download software
2022/8/7
sed命令
深度解析网易严选和京东的会员体系,建议收藏
docker部署redis容器问题
推荐100首好听英文歌
COMSOL Multiphysics 6.0软件安装包和安装教程
Why is HTTS safer?
DBeaver 22.1.4 发布,可视化数据库管理平台
Techwiz OLED:偏振片的发射特性
Pinia(一)初体验快速安装与上手
STL underlying implementation principle
LVS负载均衡群集
To make people's consumption safer, more assured and more satisfied
简单理解MVVM模型
jupyter lab安装、配置教程