Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)

Problem Description
To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.
 
Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
 
Output
For each case, output a single integer: the minimum steps needed.
 
Sample Input
4 0
3 2
1 2
1 3
 
Sample Output
4
2

Hint

Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.

 
Source

题意:给你n个点,m条边的有向图,最少加几条边使得改图为强连通;

思路:对于一个缩完点的图,要使得其强连通,入度和出度都至少为1;

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<cstring>
  10. #include<vector>
  11. #include<list>
  12. #include<set>
  13. #include<map>
  14. #include<stdlib.h>
  15. #include<time.h>
  16. using namespace std;
  17. #define LL long long
  18. #define pi (4*atan(1.0))
  19. #define eps 1e-6
  20. #define bug(x) cout<<"bug"<<x<<endl;
  21. const int N=1e5+,M=1e6+,inf=1e9+;
  22. const LL INF=5e17+,mod=1e9+;
  23.  
  24. struct is
  25. {
  26. int u,v;
  27. int next;
  28. }edge[];
  29. int head[];
  30. int belong[];
  31. int dfn[];
  32. int low[];
  33. int stackk[];
  34. int instack[];
  35. int number[];
  36. int in[N],out[N];
  37. int n,m,jiedge,lu,bel,top;
  38. void update(int u,int v)
  39. {
  40. jiedge++;
  41. edge[jiedge].u=u;
  42. edge[jiedge].v=v;
  43. edge[jiedge].next=head[u];
  44. head[u]=jiedge;
  45. }
  46. void dfs(int x)
  47. {
  48. dfn[x]=low[x]=++lu;
  49. stackk[++top]=x;
  50. instack[x]=;
  51. for(int i=head[x];i;i=edge[i].next)
  52. {
  53. if(!dfn[edge[i].v])
  54. {
  55. dfs(edge[i].v);
  56. low[x]=min(low[x],low[edge[i].v]);
  57. }
  58. else if(instack[edge[i].v])
  59. low[x]=min(low[x],dfn[edge[i].v]);
  60. }
  61. if(low[x]==dfn[x])
  62. {
  63. int sum=;
  64. bel++;
  65. int ne;
  66. do
  67. {
  68. sum++;
  69. ne=stackk[top--];
  70. belong[ne]=bel;
  71. instack[ne]=;
  72. }while(x!=ne);
  73. number[bel]=sum;
  74. }
  75. }
  76. void tarjan()
  77. {
  78. memset(dfn,,sizeof(dfn));
  79. bel=lu=top=;
  80. for(int i=;i<=n;i++)
  81. if(!dfn[i])
  82. dfs(i);
  83. }
  84. int main()
  85. {
  86. int i,t;
  87. while(~scanf("%d%d",&n,&m))
  88. {
  89. memset(in,,sizeof(in));
  90. memset(out,,sizeof(out));
  91. memset(head,,sizeof(head));
  92. jiedge=;
  93. for(i=;i<=m;i++)
  94. {
  95. int u,v;
  96. scanf("%d%d",&u,&v);
  97. update(u,v);
  98. }
  99. tarjan();
  100. int x=;
  101. int z=;
  102. for(i=;i<=jiedge;i++)
  103. if(belong[edge[i].v]!=belong[edge[i].u])
  104. {
  105. if(!out[belong[edge[i].u]])x++;
  106. if(!in[belong[edge[i].v]])z++;
  107. out[belong[edge[i].u]]++;
  108. in[belong[edge[i].v]]++;
  109. }
  110. x=bel-x;
  111. z=bel-z;
  112. if(bel==)
  113. printf("0\n");
  114. else
  115. printf("%d\n",max(x,z));
  116. }
  117. return ;
  118. }

hdu 3836 Equivalent Sets trajan缩点的更多相关文章

  1. hdu 3836 Equivalent Sets

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...

  2. [tarjan] hdu 3836 Equivalent Sets

    主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...

  3. hdu 3836 Equivalent Sets(强连通分量--加边)

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

  4. hdu——3836 Equivalent Sets

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

  5. hdu 3836 Equivalent Sets(tarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

  6. hdu - 3836 Equivalent Sets(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...

  7. HDU - 3836 Equivalent Sets (强连通分量+DAG)

    题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...

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

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

  9. HDU 3836 Equivalent SetsTarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

随机推荐

  1. PHP json_encode函数中需要注意的地方

    在php中使用 json_encode() 内置函数可以使用得php中的数据更好的与其它语言传递与使用. 这个函数的功能是将数组转换成json数据存储格式: 1 <?php 2 $arr=arr ...

  2. Icarscan VCI is definitely the update variation of Start iDiag

    Start iCarScan is alternative of Super X431 iDiag, it’ll make your Android smartphone or tablet righ ...

  3. 配置方案:Redis持久化RDB和AOF

    Redis持久化方案 Redis是内存数据库,数据都是存储在内存中,为了避免进程退出导致数据的永久丢失,需要定期将Redis中的数据以某种形式(数据或命令)从内存保存到硬盘.当下次Redis重启时,利 ...

  4. DNS缓存中毒的知识

    网络上出现互联网漏洞——DNS缓存漏洞,此漏洞直指我们应用中互联网脆弱的安全系统,而安全性差的根源在于设计缺陷.利用该漏洞轻则可以让用户无法打开网页,重则是网络钓鱼和金融诈骗,给受害者造成巨大损失. ...

  5. 现代汽车加入Linux 基金会和 AGL协作平台

    1月4日,现代汽车宣布已加入 Linux 基金会和其旗下的非营利协作平台 Automotive Grade Linux(AGL),现代汽车公司副总裁兼信息娱乐技术中心负责人 Paul Choo 表示: ...

  6. 在Linux 中如何从进程相关的文件描述中恢复数据

    在Linux中误删除了某个文件,但是 ps-ef|grep 文件名 发现某个进程还在使用该文件,那么可以通 过以下方式恢复文件. 例如:创建一个简单文件/tmp/test.txt, 随便向里面写点内容 ...

  7. Spring Boot 中使用 @ConfigurationProperties 注解

    @ConfigurationProperties 主要作用:绑定 application.properties 中的属性 例如: @Configuration public class DataSou ...

  8. jquery的$post方法不发送空数组的解决办法

    问题:jquery里的ajax在提交post请求时,如果数据里有一个空数组,则这个空数组不会提交上去 技术上的解决办法如下: 源代码: var params = { type : , ids:[] } ...

  9. Centos6环境下CI(CodeIgniter)框架创建定时任务

    在我们项目开发过程中,经常遇到定时类需求,如果是仅仅一个PHP文件,那么很轻松的知道该怎么配置,但是在框架中,mvc设计思想访问对应控制器下的对应方法,那么就无从下手了.我这里参考网上的例子在自己的服 ...

  10. P2590 [ZJOI2008]树的统计(树链剖分)

    P2590 [ZJOI2008]树的统计 虽然是入门树剖模板 但是我终于1A了(大哭) 懒得写啥了(逃 #include<iostream> #include<cstdio> ...