Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5742    Accepted Submission(s): 1973

Problem Description
Consider the following exercise, found in a generic linear algebra textbook.

Let A be an n × n matrix. Prove that the following statements are equivalent:

1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. Ax = 0 has only the trivial solution x = 0.

The
typical way to solve such an exercise is to show a series of
implications. For instance, one can proceed by showing that (a) implies
(b), that (b) implies (c), that (c) implies (d), and finally that (d)
implies (a). These four implications show that the four statements are
equivalent.

Another way would be to show that (a) is equivalent
to (b) (by proving that (a) implies (b) and that (b) implies (a)), that
(b) is equivalent to (c), and that (c) is equivalent to (d). However,
this way requires proving six implications, which is clearly a lot more
work than just proving four implications!

I have been given some
similar tasks, and have already started proving some implications. Now I
wonder, how many more implications do I have to prove? Can you help me
determine this?

 
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤
50000): the number of statements and the number of implications that
have already been proved.
* m lines with two integers s1 and s2
(1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved
that statement s1 implies statement s2.

 
Output
Per testcase:

* One line with the minimum number of additional implications that
need to be proved in order to prove that all statements are equivalent.

 
Sample Input
2
4 0
3 2
1 2
1 3
 
Sample Output
4
2
 
Source
 
Recommend
lcy
 
理解一下题意,经过奇奇怪怪的转化以后,得出核心题意:给一个有向图,问最少加几条边可使其成为强连通图。
tarjan缩点以后,统计入度为0和出度为0的点个数,取最大值就是答案。
 
这题总感觉以前做过好多次?
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<cstring>
  6. using namespace std;
  7. const int mxn=;
  8. int top,stack[mxn];
  9. bool inst[mxn];
  10. int cnt,dnow;
  11. int dfn[mxn],low[mxn];
  12. int belone[mxn],in[mxn],out[mxn];
  13. vector<int> e[mxn];
  14. void clear(){
  15. cnt=;dnow=;top=;
  16. memset(dfn,-,sizeof(dfn));
  17. memset(inst,false,sizeof(inst));
  18. memset(in,,sizeof in);
  19. memset(out,,sizeof out);
  20. for(int i=;i<mxn;i++) e[i].clear();
  21. }
  22. int n,m;
  23. void tarjan(int s){
  24. int v=,i;
  25. dfn[s]=++dnow;
  26. low[s]=dfn[s];
  27. inst[s]=true;
  28. stack[++top]=s;
  29. int si=e[s].size();
  30. for(i=;i<si;i++){
  31. v=e[s][i];
  32. if(dfn[v]==-){
  33. tarjan(v);
  34. low[s]=min(low[v],low[s]);
  35. }
  36. else if(inst[v]){
  37. low[s]=min(dfn[v],low[s]);
  38. }
  39. }
  40. if(dfn[s]==low[s]){
  41. cnt++;
  42. do{
  43. v=stack[top--];
  44. belone[v]=cnt;
  45. inst[v]=false;
  46. }while(s!=v);
  47. }
  48. return;
  49. }
  50. void calc(){
  51. if(cnt==){
  52. printf("0\n");return;
  53. }
  54. int i,j;
  55. for(i=;i<=n;i++){
  56. for(j=;j<e[i].size();j++){
  57. int v=e[i][j];
  58. if(belone[i]!=belone[v]){
  59. in[belone[v]]++;
  60. out[belone[i]]++;
  61. }
  62. }
  63. }
  64. int idg=,odg=;
  65. for(i=;i<=cnt;i++){
  66. if(!in[i])idg++;
  67. if(!out[i])odg++;
  68. }
  69. printf("%d\n",max(idg,odg));
  70. return;
  71. }
  72. int main(){
  73. int T;
  74. scanf("%d",&T);
  75. while(T--){
  76. scanf("%d%d",&n,&m);
  77. if(!m){
  78. if(n==)printf("0\n");
  79. else printf("%d\n",n);
  80. continue;
  81. }
  82. clear();
  83. int i,j;
  84. int u,v;
  85. for(i=;i<=m;i++){
  86. scanf("%d%d",&u,&v);
  87. e[u].push_back(v);
  88. }
  89. for(i=;i<=n;i++){
  90. if(dfn[i]==-)tarjan(i);
  91. }
  92. calc();
  93. }
  94. return ;
  95. }

HDU2767 Proving Equivalences的更多相关文章

  1. HDU2767 Proving Equivalences(加边变为强联通图)

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

  2. hdu2767 Proving Equivalences Tarjan缩点

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  3. hdu2767 Proving Equivalences --- 强连通

    给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...

  4. hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法

    点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...

  5. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  6. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

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

  7. Proving Equivalences(加多少边使其强联通)

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

  8. UVALive - 4287 - Proving Equivalences(强连通分量)

    Problem   UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...

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

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

随机推荐

  1. ATM-lib-common

    import logging.configfrom conf import settingsfrom core import src def get_logger(name): logging.con ...

  2. 【函数应用】PHP中关于URL的函数处理

    一,函数介绍 1.解析HTTP头信息:get_header() array get_headers ( string 目标URL [, int $format = 0 [如果将可选的 format 参 ...

  3. 15.VUE学习之-表单中使用key唯一令牌解决表单值混乱问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. pycharm配置Git托管

    利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程  转载https ...

  5. DFS:BZOJ1085-骑士精神

    题目: 1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1461  Solved: 796 [Submit][ ...

  6. Python文件与异常处理

    文件读写 使用python的BIF(build in function)open()进行文件读写操作 # 1.打开文件 data = open(file_name,'w') # 读取模式有很多种,主要 ...

  7. How to check if Visual Studio 2005 SP1 is installed

    How to check if Visual Studio 2005 SP1 is installed Check the following registry key. HKEY_LOCAL_MAC ...

  8. 分分钟教你做出自己的新闻阅读APP

    分分钟教你做出自己的新闻阅读APP 引子 曾经不小心发现了一些好的看新闻的网站,但是电脑又不是随身携带,因此想要下载一个这个网站的手机APP来看新闻,但是问题来了,这个网站根本没有做Android端! ...

  9. JWT实现token的生成和认证demo

    上篇写到对JWT的理解,这篇写一个小的demo来实践下 Github:https://github.com/wuhen152033/token/tree/dev 简介 本次的demo是基于Spring ...

  10. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...