当前位置:网站首页>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 .
代码如下:
边栏推荐
- 【图像分类】2022-MaxViT ECCV
- Offensive and defensive world - web2
- 你真的了解IP地址吗?
- hdu4635 Strongly connected(tarjan计算强连通通分量+缩点+思想)
- Flink Record has Long.MIN_VALUE timestamp (= no timestamp marker). Is the time characteristic
- 蔚来杯2022牛客暑期多校训练营6 ABGJM
- C# - var 关键字
- [ 深度学习 ] 课程学习(Curriculum Learning)
- Implementation principle of priority queue
- 攻防世界——fakebook
猜你喜欢
随机推荐
使用分类权重,轻松解决数据不平衡的问题
BOSS直聘回应女大学生被性骚扰:高度重视求职者安全 可在App举报
选择适合投稿的英文期刊或会议的方法
SeeOD应用:He-Ne激光束聚焦物镜设计
多态案例2 制作饮品
蔚来杯2022牛客暑期多校训练营6 ABGJM
蔚来杯2022牛客暑期多校训练营6 ABGJM
oracle sql语法 更改为mysql sql语法 或者代码实现
要写脚本,编程不好不要紧--浅谈CTF中脚本的编写方法
我的MySQL安装这样了怎么解决也
Go 函数与方法
Pinia(一)初体验快速安装与上手
C# - var 关键字
67:第五章:开发admin管理服务:20:开发【解冻/冻结用户,接口】;(用户状态变更后,需要刷新用户状态,即变更用户会话信息:我们一般通过“删除redis中会话信息,强制用户重新登录“来做的;)
Django+MySQL+HarmonyOS------------笔记二
MySQL中的锁机制详解
docker部署redis容器问题
oracle如何删除表并且释放表空间
交换两个整型变量的三种方法
FRED应用:TMT MOBIE成像光谱仪的概念设计阶段杂散光分析







![[Raspberry Pi] vim editor](/img/a8/6cfdeefa044dfa44b603654ea11a98.png)

