hdu 3836 Equivalent Sets【强联通】

本文深入解析了信息技术领域中的多个子领域,包括前端开发、后端开发、移动开发、游戏开发、大数据开发等,并针对每个领域进行了详细的剖析。通过实际案例,展示了在这些领域中遇到的问题及其解决方案,旨在帮助开发者更全面地理解信息技术的复杂性并提供有效的应对策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值