Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
Total Submission(s): 3568    Accepted Submission(s): 1235

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.

 
题意: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<stack>
  5. #include<vector>
  6. #define MAX 50010
  7. #define INF 0x3f3f3f
  8. using namespace std;
  9. int n,m;
  10. int ans,head[MAX];
  11. int low[MAX],dfn[MAX];
  12. int instack[MAX],sccno[MAX];
  13. vector<int>newmap[MAX];
  14. vector<int>scc[MAX];
  15. int scccnt,dfsclock;
  16. int in[MAX],out[MAX];
  17. stack<int>s;
  18. struct node
  19. {
  20. int beg,end,next;
  21. }edge[MAX];
  22. void init()
  23. {
  24. ans=0;
  25. memset(head,-1,sizeof(head));
  26. }
  27. void add(int beg,int end)
  28. {
  29. edge[ans].beg=beg;
  30. edge[ans].end=end;
  31. edge[ans].next=head[beg];
  32. head[beg]=ans++;
  33. }
  34. void getmap()
  35. {
  36. int i,a,b;
  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. low[u]=dfn[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(low[u]==dfn[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]++;out[u]++;
  103. }
  104. }
  105. }
  106. void solve()
  107. {
  108. int i,j;
  109. if(scccnt==1)
  110. {
  111. printf("0\n");
  112. return ;
  113. }
  114. else
  115. {
  116. int minn=0;
  117. int maxx=0;
  118. for(i=1;i<=scccnt;i++)
  119. {
  120. if(!in[i])
  121. minn++;
  122. if(!out[i])
  123. maxx++;
  124. }
  125. printf("%d\n",max(minn,maxx));
  126. }
  127. }
  128. int main()
  129. {
  130. while(scanf("%d%d",&n,&m)!=EOF)
  131. {
  132. init();
  133. getmap();
  134. find(1,n);
  135. suodian();
  136. solve();
  137. }
  138. return 0;
  139. }

  

hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】的更多相关文章

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

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

  2. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

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

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

  4. hdu 3836 Equivalent Sets trajan缩点

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

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

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

  6. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

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

  7. hdu 3836 Equivalent Sets

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

  8. HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others)    Memory Lim ...

  9. [tarjan] hdu 3836 Equivalent Sets

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

随机推荐

  1. 曾经的10道JAVA面试题

    1.HashMap和Hashtable的区别. 都属于Map接口的类,实现了将惟一键映射到特定的值上.HashMap 类没有分类或者排序.它允许一个null 键和多个null 值.Hashtable ...

  2. 快速理解webStroage

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. PHP trim去空格函数

    trim() 能除去的字符有“ ”空格."\t"水平制表符."\n"换行符."\r"回车符."\0字符串结束符".&qu ...

  4. An easy way to syncTime using C#

    /* * Created by SharpDevelop. * User: Administrator * Date: 2013/10/23 * Time: 8:57 * author zibet * ...

  5. SQLite Helper (C#) z

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp Introduction I have written a small ...

  6. isEqual

    "; NSString *str2 = [NSString stringWithFormat:@"%@", str1]; 大家明白, str1和str2在内存中的地址是不 ...

  7. aspx、ashx以及cs的关系,viewState

    aspx和ashx关系:aspx就是一种特殊的ashx,aspx对应的类是page,它是实现了IHttpHandler接口,所以说aspx是高级的HttpHandler.aspx中帮我们封装了很多操作 ...

  8. Code First 指定外键名称

    指定类外键有注释(DataAnnotation)和FluentAPI两种方式, 目前掌握的知识我们先把DataAnnotation可用的四种方式总结如下 第一种方法: //1-指定导航属性,会自动生成 ...

  9. ASP.NET之HttpModule拦截404异常

    Httpmodule代码: public class Error404Module : IHttpModule { public void Init(HttpApplication context) ...

  10. IntelliJ IDEA 创建web项目后添加Java EE (Tomcat)的依赖包

    本文讲述的是IntelliJ IDEA 12版本 如果在编译器中创建一个web项目后,没有设置tomcat的依赖包,就不能成功的编译,会缺少javax.servlet.*等类. 添加的方法是: 打开p ...