HDU 3836 equivalent Sets "Strong Unicom"

Source: Internet
Author: User

Equivalent setsTime limit:12000/4000 MS (java/others) Memory limit:104857/104857 K (java/others)
Total submission (s): 3065 Accepted Submission (s): 1077


Problem Descriptionto prove-sets A and B are equivalent, we can first prove a was a subset of B, and then prove B is a Subset of A, so-finally we got that these and sets are equivalent.
You be to prove N sets be equivalent, using the method above:in each step can prove a set X is a subset of another Set Y, and there is also some sets that is already proven to being subsets of some other sets.
Now, want to know the minimum steps needed to get the problem proved.
Inputthe input file contains multiple test cases, in each case, the first line contains both integers N <= 20000 and M & lt;= 50000.
Next M lines, each line contains the integers x, Y, means set X in a subset of set Y.
Outputfor, output a single integer:the minimum steps needed.
Sample Input
4 03 21) 21 3

Sample Output
42 Analysis: Test instructions: There are 1~n to represent n conclusions, first has given m relations, a B for a can prove B, for at least a few side can be added to be N conclusion 22 can prove, it is obvious that at least add a few sides to make a strong unicom. The first indentation, in the degree of access is 0, and the number of degrees of 0, take its maximum this is the answer.
  
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <stack>
  6. #define MAX (A, b) a>b?a:b
  7. #define MAXH 220000+10
  8. #define MAXN 550000+10
  9. using namespace Std;
  10. typedef struct{
  11. int to,next;
  12. }node;
  13. stack<int>s;
  14. Node E1[MAXN],E2[MAXN];
  15. int head1[maxh],head2[maxh],mark[maxh],cnt1,cnt2;
  16. int belong[maxh],num[maxh][2],in[maxh],out[maxh],count;
  17. void init (int n) {
  18. memset (head1,-1,sizeof (HEAD1));
  19. memset (head2,-1,sizeof (head2));
  20. cnt1=0;
  21. cnt2=0;
  22. }
  23. void Add (int a,int b) {
  24. e1[cnt1].to=b,e1[cnt1].next=head1[a],head1[a]=cnt1++;
  25. e2[cnt2].to=a,e2[cnt2].next=head2[b],head2[b]=cnt2++;
  26. }
  27. void dfs1 (int x) {
  28. Mark[x]=1;
  29. for (int i=head1[x];i+1;i=e1[i].next) {
  30. int to=e1[i].to;
  31. if (Mark[to])
  32. Continue
  33. DFS1 (to);
  34. }
  35. S.push (x);
  36. }
  37. void dfs2 (int x) {
  38. Mark[x]=1;
  39. Belong[x]=count;
  40. for (int i=head2[x];i+1;i=e2[i].next) {
  41. int to=e2[i].to;
  42. if (Mark[to])
  43. Continue
  44. DFS2 (to);
  45. }
  46. }
  47. int main () {
  48. int n,m,a,b,sumin,sumout;
  49. while (~SCANF ("%d%d", &n,&m)) {
  50. Init (n);
  51. for (int i=0;i<m;i++) {
  52. scanf ("%d%d", &a,&b);
  53. Num[i][0]=a;
  54. Num[i][1]=b;
  55. Add (A, b);
  56. }
  57. memset (Mark,0,sizeof (Mark));
  58. while (! S.empty ())
  59. S.pop ();
  60. for (int i=1;i<=n;i++) {
  61. if (!mark[i]) {
  62. DFS1 (i);
  63. }
  64. }
  65. memset (Mark,0,sizeof (Mark));
  66. count=0;
  67. while (! S.empty ()) {
  68. int S=s.top ();
  69. S.pop ();
  70. if (!mark[s]) {
  71. count++;
  72. DFS2 (s);
  73. }
  74. }
  75. if (count==1) {
  76. printf ("0\n");
  77. Continue
  78. }
  79. memset (In,0,sizeof (in));
  80. Memset (out,0,sizeof (out));
  81. sumin=sumout=0;
  82. for (int i=0;i<m;i++) {
  83. if (Belong[num[i][0]]!=belong[num[i][1]])
  84. In[belong[num[i][0]]]=out[belong[num[i][1]]]=1;
  85. }
  86. for (int i=1;i<=count;i++) {
  87. if (!in[i]) {
  88. sumin++;
  89. }
  90. if (!out[i]) {
  91. sumout++;
  92. }
  93. }
  94. printf ("%d\n", Max (Sumin,sumout));
  95. }
  96. return 0;
  97. }

HDU 3836 equivalent Sets "Strong Unicom"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.