HUD——T 3836 Equivalent Sets

本文介绍了一种算法,用于解决如何用最少步骤证明多个集合间的等价性问题。通过构建图模型并进行缩点处理,统计出入度为0的节点数量来确定所需最小步骤。

http://acm.hdu.edu.cn/showproblem.php?pid=3836

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


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.
 

 

Source
 

 

Recommend
xubiao   |   We have carefully selected several similar problems for you:   3835  3828  3834  3830  3829 
 
 
题意:求最少连几条边可以使整个图成为强连通图
可以先将图缩点,然后统计新图中入读==0,和出度==0 的点的个数,因为使加边最少,
所以应该是先给出度==0的点连一条向入读==0的点得边,然后再加上多余的(入读==0||出度==0)的点
ans=max(入读==0的点数,出读==0的点数)
 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 const int N(20000+5);
 8 const int M(50000+5);
 9 int n,m;
10 
11 int head[N],sumedge;
12 struct Edge
13 {
14     int v,next;
15     Edge(int v=0,int next=0):v(v),next(next){} 
16 }edge[M];
17 inline void ins(int u,int v)
18 {
19     edge[++sumedge]=Edge(v,head[u]);
20     head[u]=sumedge;
21 }
22 
23 int tim,dfn[N],low[N];
24 int top,Stack[N],instack[N];
25 int sumcol,col[N];
26 void DFS(int now)
27 {
28     dfn[now]=low[now]=++tim;
29     Stack[++top]=now; instack[now]=1;
30     for(int i=head[now];i;i=edge[i].next)
31     {
32         int v=edge[i].v;
33         if(!dfn[v])  DFS(v),low[now]=min(low[now],low[v]);
34         else if(instack[v]) low[now]=min(low[now],dfn[v]);
35     }
36     if(dfn[now]==low[now])
37     {
38         col[now]=++sumcol;
39         for(;Stack[top]!=now;top--)
40         {
41             col[Stack[top]]=sumcol;
42             instack[Stack[top]]=0;
43         }
44         instack[now]=0; top--;
45     }
46 }
47 
48 int ans,ans1,ans2,rd[N],cd[N];
49 inline void init()
50 {
51     top=ans=ans1=ans2=tim=sumcol=sumedge=0;
52     memset(rd,0,sizeof(rd));
53     memset(cd,0,sizeof(cd));
54     memset(low,0,sizeof(low));
55     memset(dfn,0,sizeof(dfn));
56     memset(head,0,sizeof(head));
57     memset(Stack,0,sizeof(Stack));
58     memset(instack,0,sizeof(instack));
59 }
60 
61 int main()
62 {
63     for(;~scanf("%d%d",&n,&m);init())
64     {
65         for(int u,v,i=1;i<=m;i++)
66             scanf("%d%d",&u,&v),ins(u,v);
67         for(int i=1;i<=n;i++)
68             if(!dfn[i]) DFS(i);
69         for(int u=1;u<=n;u++)
70             for(int i=head[u];i;i=edge[i].next)
71             {
72                 int v=edge[i].v;
73                 if(col[u]==col[v]) continue;
74                 rd[col[v]]++; cd[col[u]]++;
75             }
76         for(int i=1;i<=sumcol;i++)
77         {
78             if(!cd[i]) ans1++;
79             if(!rd[i]) ans2++;
80         }
81         ans=max(ans1,ans2);
82         if(sumcol==1) ans=0;
83         printf("%d\n",ans);
84     }
85     return 0;
86 }

 

转载于:https://www.cnblogs.com/Shy-key/p/7380720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值