Problem Description
  1. 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.
  2. 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.
  3. Now you want to know the minimum steps needed to get the problem proved.
Input
  1. The input file contains multiple test cases, in each case, the first line contains two integers N <= and M <= .
  2. Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
 
Output
  1. For each case, output a single integer: the minimum steps needed.
 
Sample Input
  1.  
 
Sample Output
  1.  
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
 

把tarjan复习了一遍

  1. 题目描述:将题目中的集合转换为顶点,A集合是B集合的子集,转换为一条有向边<A,B>,即题目给我们一个有向图,问最少需要添加多少条边使之成为强连通图。
  2. 解题思路:通过tarjan算法找出图中的所有强连通分支,并将每一个强连通分支缩成一个点(因为强连通分量本身已经满足两两互相可达)。
  3. 要使缩点后的图成为强连通图,每个顶点最少要有一个入度和一个出度,一条边又提供一个出度和一个入度。
  4. 所以可以通过统计没有入度的顶点数 noInDegree 没有出度的顶点数 noOutDegree
  5. 所需要添加的边数就是noInDegreenoOutDegree中的最大值。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<stack>
  5. #include<vector>
  6. using namespace std;
  7. #define N 20006
  8. #define inf 1<<26
  9. int n,m;
  10. struct Node
  11. {
  12. int from;
  13. int to;
  14. int next;
  15. }edge[N<<];
  16. int tot;//表示边数,边的编号
  17. int head[N];
  18. int vis[N];
  19. int tt;
  20. int scc;
  21. stack<int>s;
  22. int dfn[N],low[N];
  23. int col[N];
  24.  
  25. void init()//初始化
  26. {
  27. tt=;
  28. tot=;
  29. memset(head,-,sizeof(head));
  30. memset(dfn,-,sizeof(dfn));
  31. memset(low,,sizeof(low));
  32. memset(vis,,sizeof(vis));
  33. memset(col,,sizeof(col));
  34. }
  35.  
  36. void add(int s,int u)//邻接矩阵函数
  37. {
  38. edge[tot].from=s;
  39. edge[tot].to=u;
  40. edge[tot].next=head[s];
  41. head[s]=tot++;
  42. }
  43.  
  44. void tarjan(int u)//tarjan算法找出图中的所有强连通分支
  45. {
  46. dfn[u] = low[u]= ++tt;
  47. vis[u]=;
  48. s.push(u);
  49. int cnt=;
  50. for(int i=head[u];i!=-;i=edge[i].next)
  51. {
  52. int v=edge[i].to;
  53. if(dfn[v]==-)
  54. {
  55. // sum++;
  56. tarjan(v);
  57. low[u]=min(low[u],low[v]);
  58. }
  59. else if(vis[v]==)
  60. low[u]=min(low[u],dfn[v]);
  61. }
  62. if(dfn[u]==low[u])
  63. {
  64. int x;
  65. scc++;
  66. do{
  67. x=s.top();
  68. s.pop();
  69. col[x]=scc;
  70. vis[x]=;
  71. }while(x!=u);
  72. }
  73. }
  74.  
  75. int main()
  76. {
  77. while(scanf("%d%d",&n,&m)== && n+m)
  78. {
  79.  
  80. init();
  81.  
  82. scc=;//scc表示强连通分量的个数
  83. while(m--)
  84. {
  85. int a,b;
  86. scanf("%d%d",&a,&b);
  87. add(a,b);//构造邻接矩阵
  88. }
  89. for(int i=;i<=n;i++)
  90. {
  91. if(dfn[i]==-)
  92. {
  93. tarjan(i);
  94. }
  95. }
  96. //printf("%d\n",scc);
  97.  
  98. int inde[N];
  99. int outde[N];
  100. memset(inde,,sizeof(inde));
  101. memset(outde,,sizeof(outde));
  102. for(int i=;i<tot;i++)
  103. {
  104. int a=edge[i].from;
  105. int b=edge[i].to;
  106. if(col[a]!=col[b])
  107. {
  108. inde[col[b]]++;
  109. outde[col[a]]++;
  110. }
  111. }
  112.  
  113. int ans1=;
  114. int ans2=;
  115. for(int i=;i<=scc;i++)
  116. {
  117. if(inde[i]==)
  118. {
  119. ans1++;
  120. }
  121. if(outde[i]==)
  122. {
  123. ans2++;
  124. }
  125. }
  126. if(scc==)
  127. {
  128. printf("0\n");
  129. continue;
  130. }
  131. printf("%d\n",max(ans1,ans2));
  132.  
  133. }
  134. return ;
  135. }

hdu 3836 Equivalent Sets(tarjan+缩点)的更多相关文章

  1. hdu 3836 Equivalent Sets trajan缩点

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

  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

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

  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

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

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

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

  7. HDU3836 Equivalent Sets (Tarjan缩点+贪心)

    题意: 给你一张有向图,问你最少加多少条边这张图强连通 思路: 缩点之后,如果不为1个点,答案为出度为0与入度为0的点的数量的最大值 代码: #include<iostream> #inc ...

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

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

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

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

随机推荐

  1. Weka算法Clusterers-DBSCAN源代码分析

    假设说世界上仅仅能存在一种基于密度的聚类算法的话.那么它必须是DBSCAN(Density-based spatial clustering of applications with noise).D ...

  2. redis 源代码分析(一) 内存管理

    一,redis内存管理介绍 redis是一个基于内存的key-value的数据库,其内存管理是很重要的,为了屏蔽不同平台之间的差异,以及统计内存占用量等,redis对内存分配函数进行了一层封装,程序中 ...

  3. [Redux] Implementing combineReducers() from Scratch

    The combineReducers function we used in previous post: const todoApp = combineReducers({ todos, visi ...

  4. Handler Looper 原理 详解

    演示代码 public class MainActivity extends ListActivity {     private TextView tv_info;     private CalT ...

  5. Windows 右键快速运行命令行

    原文见:右键命令行 - yacper - 博客园 方法一:配置文件夹选项 1 打开人任意文件夹,[工具] --> [文件夹选项] --> [文件类型] --> [(无)资料夹] -- ...

  6. java开发软件的安装

    jdk+eclipse+svn+maven+mysql+tomcat7.0+sublime安装包和jar插件 配置管理工具-SVN http://download.csdn.net/detail/u0 ...

  7. window程序设计1

    int WINAPI WinMain(HINSTANCE HInstance,HINSTANCE HPreInstance,LPSTR szCmdLine,int CmdShown) { Massag ...

  8. 在TCP协议下的数据传送

    本人小白菜逼一枚,,,,刚建立博客,也写不了太深入的,就写点上课的笔记什么的.有错误希望广大博友指出,我一定虚心学习接收改正. 我的新浪邮箱:liudaohui0805@sina.com 我的QQ邮箱 ...

  9. 12个用得着的JQuery代码片段

    1. 导航菜单背景切换效果 在项目的前端页面里,相对于其它的导航菜单,激活的导航菜单需要设置不同的背景.这种效果实现的方式有很多种,下面是使用JQuery实现的一种方式: <ul id='nav ...

  10. Zepto源码笔记(一)

    最近在研究Zepto的源码,这是第一篇分析,欢迎大家继续关注,第一次写源码笔记,希望大家多指点指点,第一篇文章由于首次分析原因不会有太多干货,希望后面的文章能成为各位大大心目中的干货. Zepto是一 ...