Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1802    Accepted Submission(s): 746

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
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
 
 分析:首先进行强连通对原图进行缩点,然后对于缩点后的图形,只考虑出度为0和入度为0的联通块,一出度为0说明,该联通块里的所有点都不能和该联通块外面的所有点连单向边,总共有sum[i]*(n-sum[i])条边不能连接,一个连通图两两建边,共有n*(n-1)条边,但是题目已有m条边已经连接,所以现在可以连接的有n*(n-1)-m-sum[i]*(n-sum[i]);入度为0的联通块同理,所有度为0的块计算后取较大值即为答案。
程序:
  1. #include"cstdio"
  2. #include"cstring"
  3. #include"cstdlib"
  4. #include"cmath"
  5. #include"string"
  6. #include"map"
  7. #include"cstring"
  8. #include"iostream"
  9. #include"algorithm"
  10. #include"queue"
  11. #include"stack"
  12. #define inf 0x3f3f3f3f
  13. #define M 200009
  14. #define eps 1e-8
  15. #define INT int
  16. #define LL __int64
  17. using namespace std;
  18. struct node
  19. {
  20. int u,v,next;
  21. }edge[M*];
  22. int head[M];
  23. int dfn[M];
  24. int belong[M];
  25. int low[M];
  26. int use[M];
  27. int sum[M];
  28. int in[M];
  29. int out[M];
  30. int t,indx,num,cnt,m,n;
  31. stack<int>q;
  32. void init()
  33. {
  34. t=;
  35. memset(head,-,sizeof(head));
  36. }
  37. void add(int u,int v)
  38. {
  39. edge[t].u=u;
  40. edge[t].v=v;
  41. edge[t].next=head[u];
  42. head[u]=t++;
  43. }
  44. void tarjan(int u)
  45. {
  46. dfn[u]=low[u]=++indx;
  47. q.push(u);
  48. use[u]=;
  49. for(int i=head[u];~i;i=edge[i].next)
  50. {
  51. int v=edge[i].v;
  52. if(!dfn[v])
  53. {
  54. tarjan(v);
  55. low[u]=min(low[u],low[v]);
  56. }
  57. else if(use[v])
  58. low[u]=min(low[u],dfn[v]);
  59. }
  60. if(low[u]==dfn[u])
  61. {
  62. int p;
  63. num++;
  64. do
  65. {
  66. p=q.top();
  67. q.pop();
  68. use[p]=;
  69. belong[p]=num;
  70. sum[num]++;
  71.  
  72. }while(p!=u);
  73. }
  74. }
  75. void slove()
  76. {
  77. num=indx=;
  78. memset(dfn,,sizeof(dfn));
  79. memset(low,,sizeof(low));
  80. memset(use,,sizeof(use));
  81. memset(sum,,sizeof(sum));
  82. for(int i=;i<=n;i++)
  83. {
  84. if(!dfn[i])
  85. tarjan(i);
  86. }
  87. if(num==)
  88. {
  89. printf("-1\n");
  90. return;
  91. }
  92. memset(in,,sizeof(in));
  93. memset(out,,sizeof(out));
  94. for(int i=;i<t;i++)
  95. {
  96. int u=edge[i].u;
  97. int v=edge[i].v;
  98. if(belong[u]!=belong[v])
  99. {
  100. out[belong[u]]++;
  101. in[belong[v]]++;
  102. }
  103. }
  104. LL ans=;
  105. for(int i=;i<=num;i++)
  106. {
  107. if(!out[i]||!in[i])
  108. {
  109. LL s=(LL)n*(n-)-m-(LL)sum[i]*(n-sum[i]);
  110. if(ans<s)
  111. ans=s;
  112. }
  113. }
  114. printf("%I64d\n",ans);
  115. }
  116. int main()
  117. {
  118. int T,a,b,kk=;
  119. cin>>T;
  120. while(T--)
  121. {
  122. scanf("%d%d",&n,&m);
  123. init();
  124. for(int i=;i<m;i++)
  125. {
  126. scanf("%d%d",&a,&b);
  127. add(a,b);
  128. }
  129. printf("Case %d: ",kk++);
  130. slove();
  131. }
  132. return ;
  133. }

强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通的更多相关文章

  1. 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。

    /** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...

  2. HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】

    Strongly connected Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. history统计命令最多的20条

    1.1.1 统计使用命令最多的20条 [root@ob1 ~]# history|awk '{ml[$2]++}END{for (i in ml) print i,ml[i]}'|sort -nrk ...

  4. 震惊,当我运行了这条Linux命令后,服务器竟然... (Linux中的删除命令)

    震惊,当我运行了这条Linux命令后,服务器竟然... 0X00 写在前面 大家都听说过删库命令rm -rf /*,但是谁又真正实践过呢?但作为一个程序员,不看看这条命令执行后会发生什么,怎么能甘心呢 ...

  5. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. 增加一条新记录,同时返回其自增id

    方法一.是在Insert或Update触发器中用select来返回需要的字段值.默认情况下,当insert时,触发其insert触发器,它的默认返回值是影响到的行数,语句是:select @@rowc ...

  7. 强连通图(最多加入几条边使得图仍为非强连通图)G - Strongly connected HDU - 4635

    题目链接:https://cn.vjudge.net/contest/67418#problem/G 具体思路:首先用tarjan缩点,这个时候就会有很多个缩点,然后再选取一个含有点数最少,并且当前这 ...

  8. 笔试算法题(19):判断两条单向链表的公共节点 & 字符集删除函数

    出题:给定两个单向链表的头结点,判断其是否有公共节点并确定第一个公共节点的索引: 分析: 由于是单向链表,所以每个节点有且仅有一个后续节点,所以只可能是Y型交叉(每条链表中的某个节点同时指向一个公共节 ...

  9. Strongly connected HDU - 4635 原图中在保证它不是强连通图最多添加几条边

    1 //题意: 2 //给你一个有向图,如果这个图是一个强连通图那就直接输出-1 3 //否则,你就要找出来你最多能添加多少条边,在保证添加边之后的图依然不是一个强连通图的前提下 4 //然后输出你最 ...

随机推荐

  1. Delphi格式化输出函数(1): Format

    vars: string;begin//指令类型 types := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Integer)]);//返回: 最大整数是: ...

  2. Java Blocking Queue

    //Listing 8-1. The Blocking Queue Equivalent of Chapter 3’s PC Application import java.util.concurre ...

  3. Java高级之内存模型分析

    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 下文是博主感悟,请带着怀疑性的态度阅读! 需要了解基本变量所占 ...

  4. 利用快速排序原理找出数组中前n大的数

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...

  5. 怎么清除file控件的文件路径

    还记得上次做一个文件上传,后来测试告诉我说,如果我要是不选择文件了呢?该怎么办?我说:简单啊,做一个取消按钮不就完事了吗!然后我就想一个file空间做一个取消是多么简单的事,用js处理可是想怎么样就怎 ...

  6. SQL Server 2008 R2不支持limit(限制行数)

    SQL Server 2008 R2不支持limit 可用:select top 3 * from Websites2 MySQL 语法 SELECT *FROM PersonsLIMIT 5; Or ...

  7. gulp教程

    1. http://www.tuicool.com/articles/FJVNZf 2.http://www.ydcss.com/archives/18 3.手动创建package.json: 如:c ...

  8. js - 驼峰命名

    1. // 驼峰命名 console.log(hump('border-bottom-color')) function hump( str) { if (typeof str != 'string' ...

  9. linux命令之tee

    功能说明:读取标准输入的数据,并将其内容输出成文件.语 法:tee [-ai][--help][--version][文件...]补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设 ...

  10. 智能生活 科技无限 CTO VOICE 第二期 智能硬件创新创业专场演讲嘉宾招募

    生活不只有诗和远方,还有当下的痛点和需求 当可穿戴设备.虚拟现实.无人机.机器人进入人们视线甚至生活当中 下一个风口就在智能硬件领域上凸显 那么,创业者如何撕掉智能外衣,设计一款有竞争力的智能硬件? ...