码迷,mamicode.com
首页 > 移动开发 > 详细

hdu 3836 Equivalent Sets【强联通】

时间:2015-02-03 23:07:59      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

Equivalent Sets

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


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
分析:题意:有1~N代表N个结论,首先已给出m个关系,a b 代表a能证明b,为至少需要添加几条边
能是N条结论相互两两能够证明,很明显就是至少添加几条边使构成一个强联通。
先缩点,在求出入度为0,和出度为0的个数,取其最大这便为答案。
  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. }
  98.         

hdu 3836 Equivalent Sets【强联通】

标签:

原文地址:http://blog.csdn.net/letterwuyu/article/details/43453297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!