Proving Equivalences

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

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
题意:n个点m条边的有向图,问最少增加多少边使图强连通。
题解:求每个scc的入度和出度,然后分别求出入度中0的个数in和出度out,取in和out中较大的一个; 
因为入度或出度为0证明这个scc和别的scc未相连,需要用一条边相连,这条边就是要加入的边,又因为一个scc可能连接多个scc,即只考虑入度或者只考虑出度都不准确
 
和昨天做的那道题一模一样,今天再做一遍 就当练练手吧
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<stack>
  6. #define MAX 50010
  7. #define INF 0x3f3f3f
  8. using namespace std;
  9. struct node
  10. {
  11. int beg,end,next;
  12. }edge[MAX];
  13. int low[MAX],dfn[MAX];
  14. int n,m,ans;
  15. int sccno[MAX],instack[MAX];
  16. int dfsclock,scccnt;
  17. vector<int>newmap[MAX];
  18. vector<int>scc[MAX];
  19. int head[MAX];
  20. int in[MAX],out[MAX];
  21. stack<int>s;
  22. void init()
  23. {
  24. ans=0;
  25. memset(head,-1,sizeof(head));
  26. }
  27. void add(int u,int v)
  28. {
  29. edge[ans].beg=u;
  30. edge[ans].end=v;
  31. edge[ans].next=head[u];
  32. head[u]=ans++;
  33. }
  34. void getmap()
  35. {
  36. int a,b,i;
  37. while(m--)
  38. {
  39. scanf("%d%d",&a,&b);
  40. add(a,b);
  41. }
  42. }
  43. void tarjan(int u)
  44. {
  45. int v,i,j;
  46. s.push(u);
  47. instack[u]=1;
  48. dfn[u]=low[u]=++dfsclock;
  49. for(i=head[u];i!=-1;i=edge[i].next)
  50. {
  51. v=edge[i].end;
  52. if(!dfn[v])
  53. {
  54. tarjan(v);
  55. low[u]=min(low[u],low[v]);
  56. }
  57. else if(instack[v])
  58. low[u]=min(low[u],dfn[v]);
  59. }
  60. if(dfn[u]==low[u])
  61. {
  62. scccnt++;
  63. while(1)
  64. {
  65. v=s.top();
  66. s.pop();
  67. instack[v]=0;
  68. sccno[v]=scccnt;
  69. if(v==u)
  70. break;
  71. }
  72. }
  73. }
  74. void find(int l,int r)
  75. {
  76. memset(low,0,sizeof(low));
  77. memset(dfn,0,sizeof(dfn));
  78. memset(instack,0,sizeof(instack));
  79. memset(sccno,0,sizeof(sccno));
  80. dfsclock=scccnt=0;
  81. for(int i=l;i<=r;i++)
  82. {
  83. if(!dfn[i])
  84. tarjan(i);
  85. }
  86. }
  87. void suodian()
  88. {
  89. int i;
  90. for(i=1;i<=scccnt;i++)
  91. {
  92. newmap[i].clear();
  93. in[i]=0;out[i]=0;
  94. }
  95. for(i=0;i<ans;i++)
  96. {
  97. int u=sccno[edge[i].beg];
  98. int v=sccno[edge[i].end];
  99. if(u!=v)
  100. {
  101. newmap[u].push_back(v);
  102. in[v]++;
  103. out[u]++;
  104. }
  105. }
  106. }
  107. void solve()
  108. {
  109. int i;
  110. if(scccnt==1)
  111. {
  112. printf("0\n");
  113. return ;
  114. }
  115. else
  116. {
  117. int inn=0;
  118. int outt=0;
  119. for(i=1;i<=scccnt;i++)
  120. {
  121. if(!in[i]) inn++;
  122. if(!out[i]) outt++;
  123. }
  124. printf("%d\n",max(inn,outt));
  125. }
  126. }
  127. int main()
  128. {
  129. int t;
  130. scanf("%d",&t);
  131. while(t--)
  132. {
  133. scanf("%d%d",&n,&m);
  134. init();
  135. getmap();
  136. find(1,n);
  137. suodian();
  138. solve();
  139. }
  140. return 0;
  141. }

  

 

hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】的更多相关文章

  1. HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  2. hdu 2767 Proving Equivalences(tarjan缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...

  3. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  4. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  5. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

  6. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  7. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  8. hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

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

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

随机推荐

  1. python【第二十一篇】Django模板继承、分页、cookie验证

    1.模板继承 母版master.html {% block title %}{% endblock %}2 {% block table-cont %}{% endblock %} 子板 {% ext ...

  2. 跨线程操作UI控件

    写程序的时候经常会遇到跨线程访问控件的问题,看到不少人去设置Control.CheckForIllegalCrossThreadCalls = false;这句话是告诉编译器不要对跨线程访问作检查,可 ...

  3. std::copy的使用

    看到有人在用std::copy这个东西,很简洁和爽啊,,所以找些帖子学习学习 http://blog.sina.com.cn/s/blog_8655aeca0100t6qe.html https:// ...

  4. libiconv2.dll

    一.问题描述 在我使用MinGW的mingw32-make工具的时候,提示错误“libiconv-2.dll找不到”. 二.问题解决 1.从脚本之家下载“libiconv-2.dll”,下载地址“ht ...

  5. 数据库 - FMDB

    FMDB 是基于 SQLite 封装的 面向对对象(OC) 的API. FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API FMDB 需要libsqli ...

  6. Swift开发之 ---- Swift宏定义

    swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...

  7. bzoj 1209: [HNOI2004]最佳包裹 三维凸包

    1209: [HNOI2004]最佳包裹 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 160  Solved: 58[Submit][Status] ...

  8. 在游戏中使用keybd_event的问题

    转自在游戏中使用keybd_event的问题 今天发现在游戏中,keybd_event不能使用,结果发现游戏是使用directinput实现读取键盘的,关键还是扫描码的问题,我抄了一段老外的代码,经测 ...

  9. 【网络流24题】No.19 负载平衡问题 (费用流)

    [题意] G 公司有 n 个沿铁路运输线环形排列的仓库, 每个仓库存储的货物数量不等. 如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入文件示例input ...

  10. 了解php的session_start的工作原理

    一.php使用session_start开启SESSION 二.浏览器访问该php脚本时,将产生两个可能: 1.(客户端的提交的cookie没有找到PHPSESSID的键) 或 (在服务器端没有找到P ...