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

题意:给你n个点,m条边的有向图,最少加几条边使得改图为强连通;

思路:对于一个缩完点的图,要使得其强连通,入度和出度都至少为1;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-6
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=1e9+10;
const LL INF=5e17+10,mod=1e9+7;

struct is
{
    int u,v;
    int next;
}edge[50010];
int head[50010];
int belong[50010];
int dfn[50010];
int low[50010];
int stackk[50010];
int instack[50010];
int number[50010];
int in[N],out[N];
int n,m,jiedge,lu,bel,top;
void update(int u,int v)
{
    jiedge++;
    edge[jiedge].u=u;
    edge[jiedge].v=v;
    edge[jiedge].next=head[u];
    head[u]=jiedge;
}
void dfs(int x)
{
    dfn[x]=low[x]=++lu;
    stackk[++top]=x;
    instack[x]=1;
    for(int i=head[x];i;i=edge[i].next)
    {
        if(!dfn[edge[i].v])
        {
            dfs(edge[i].v);
            low[x]=min(low[x],low[edge[i].v]);
        }
        else if(instack[edge[i].v])
        low[x]=min(low[x],dfn[edge[i].v]);
    }
    if(low[x]==dfn[x])
    {
        int sum=0;
        bel++;
        int ne;
        do
        {
            sum++;
            ne=stackk[top--];
            belong[ne]=bel;
            instack[ne]=0;
        }while(x!=ne);
        number[bel]=sum;
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    bel=lu=top=0;
    for(int i=1;i<=n;i++)
    if(!dfn[i])
    dfs(i);
}
int main()
{
    int i,t;
    while(~scanf("%d%d",&n,&m))
    {
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(head,0,sizeof(head));
        jiedge=0;
        for(i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            update(u,v);
        }
        tarjan();
        int x=0;
        int z=0;
        for(i=1;i<=jiedge;i++)
        if(belong[edge[i].v]!=belong[edge[i].u])
        {
            if(!out[belong[edge[i].u]])x++;
            if(!in[belong[edge[i].v]])z++;
            out[belong[edge[i].u]]++;
            in[belong[edge[i].v]]++;
        }
        x=bel-x;
        z=bel-z;
        if(bel==1)
            printf("0\n");
        else
            printf("%d\n",max(x,z));
    }
    return 0;
}
时间: 2024-11-17 09:50:56

hdu 3836 Equivalent Sets trajan缩点的相关文章

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

题目连接 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 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 s

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)