hdu——3836 Equivalent Sets

                Equivalent Sets

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

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

2011 Multi-University Training Contest 1 - Host by HNU

Recommend

xubiao   |   We have carefully selected several similar problems for you:  3835 3828 3834 3830 3829

题目:

为了证明两组A和B是等价的,我们可以先证明A是B的一个子集,然后证明B是A的一个子集,所以我们得到这两个集合是等价的。
你要证明N组是等效的,使用上面的方法:在每个步骤中,你可以证明一个集合X是另一个集合Y的一个子集,并且还有一些集合被证明是一些其他集合的子集。
现在你想知道解决问题所需的最小步骤。

题目大意为给定一张有向图,求加多少条边之后整个图是一张强连通分量。

思路:

如果一个图是一个强连通分量的话,那么他缩完点以后一定没有入读与出度为零的点。这样的话,我们知道,我们加上边以后只要让他满足出度跟入度都不为0的话,那么他就是一个强连通了。所以,我们·加边的数量就等于入度与出度为零的点的最大值。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 500010
using namespace std;
int p,q,n,m,x,y,sum,tot,top,tim;
int in[N],out[N],dfn[N],low[N],vis[N],stack[N],head[N],belong[N];
int read()
{
     int x=0,f=1; char ch=getchar();
     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();}
     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();}
     return x*f;
}
struct Edge
{
     int to,from,next;
}edge[N];
int add(int x,int y)
{
     tot++;
     edge[tot].to=y;
     edge[tot].next=head[x];
     head[x]=tot;
}
void tarjan(int now)
{
     dfn[now]=low[now]=++tim;
     stack[++top]=now,vis[now]=true;
     for(int i=head[now];i;i=edge[i].next)
     {
         int x=edge[i].to;
         if(vis[x]) low[now]=min(low[now],dfn[x]);
         else if(!dfn[x]) tarjan(x),low[now]=min(low[now],low[x]);
     }
     if(low[now]==dfn[now])
     {
         sum++;belong[now]=sum;
         for(;stack[top]!=now;top--)
         {
              int x=stack[top];
              belong[x]=sum;vis[x]=false;
          }
          vis[now]=false; top--;
     }
}
void shrink_point()
{
    for(int i=1;i<=n;i++)
      for(int j=head[i];j;j=edge[j].next)
       if(belong[i]!=belong[edge[j].to])
         out[belong[i]]++,in[belong[edge[j].to]]++;
}
int main()
{
     while(~scanf("%d",&n))
     {
          m=read();p=0,q=0;
          sum=0,tot=0,tim=0,top=0;
          memset(in,0,sizeof(in));
          memset(dfn,0,sizeof(dfn));
          memset(low,0,sizeof(low));
          memset(vis,0,sizeof(vis));
          memset(out,0,sizeof(out));
          memset(head,0,sizeof(head));
          memset(stack,0,sizeof(stack));
          memset(belong,0,sizeof(belong));
          for(int i=1;i<=m;i++)
             x=read(),y=read(),add(x,y);
          for(int i=1;i<=n;i++)
             if(!dfn[i]) tarjan(i);
           if(sum==1) printf("0\n");
           else
           {
                shrink_point();
             for(int i=1;i<=sum;i++)
             {
                 if(out[i]==0) p++;
                 if(in[i]==0)  q++;
              }
              printf("%d\n",max(p,q));
           }
     }
     return 0;
}
时间: 2024-10-25 18:56:52

hdu——3836 Equivalent Sets的相关文章

hdu 3836 Equivalent Sets

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets 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.Y

[tarjan] hdu 3836 Equivalent Sets

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others) Total Submission(s): 2890    Accepted Submission(s): 1006 Problem Description To prove two set

hdu 3836 Equivalent Sets【强联通】

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 s

hdu 3836 Equivalent Sets(强连通分量--加边)

Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 962 Problem Description To prove two sets A and B are equivalent, we can first prove A is a su

HDU 3836 Equivalent Sets(强连通缩点)

Equivalent Sets 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

hdu 3836 Equivalent Sets trajan缩点

Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others) 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

HDU 3836 Equivalent Sets(Tarjan+缩点)

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 abo

hdu - 3836 Equivalent Sets(强连通)

http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是需要连的边, 例如,假设入度为0的点多,那么每次把出度为0的点连一条边指向入度为0的点,就构成了一个环, 所以构成了一个强连通分量,同理可得出度为0点多的情况. 这代码用g++ re了,c++才能ac. 1 #include <iostream> 2 #include <cstdio>

HDU - 3836 Equivalent Sets (强连通分量+DAG)

题目大意:给出N个点,M条边,要求你添加最少的边,使得这个图变成强连通分量 解题思路:先找出所有的强连通分量和桥,将强连通分量缩点,桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直RE,用C++一发就过了... #include <cstdio> #include <cstring> #define N 20010 #define M 100010 #define min(a,b) ((a) > (b)? (b): (a)) #define max(a,b)