hdu 3836 Equivalent Sets(强连通,tarjan,4级)

来源:互联网 发布:德国程序员工资水平 编辑:程序博客网 时间:2024/05/16 08:28

Equivalent Sets

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


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 03 21 21 3
 

Sample Output
42
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

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

思路大致是:先将图中的强连通分量缩点,使得图变成一张无环图,这个时候图上剩余k1个出度为0的点和k2个入度为0的点,将出度为0的点连一条有向边到入度为0的点,此时形成一个环,这个环是一个强连通分量,可以再缩成一个点,当图中只剩一个点的时候,就完成了任务。显而易见的是最大只要连接max{k1,k2}条边,就能将所有的点缩成一个点。

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int mm=2e4+9;const int mn=5e4+9;int n,m;int head[mm],ver[mn],next[mn];int dfn[mm],e_to[mm],low[mm],dex,edge,cnt,stack[mm],top;bool instack[mm];int ideg[mm],odeg[mm];void add(int u,int v){    ver[edge]=v;    next[edge]=head[u];    head[u]=edge++;}void data(){    edge=dex=cnt=top=0;    memset(head,-1,sizeof(head));    memset(dfn,0,sizeof(dfn));    memset(ideg,0,sizeof(ideg));    memset(odeg,0,sizeof(odeg));    memset(instack,0,sizeof(instack));}void tarjan(int u){    int v;    dfn[u]=low[u]=++dex;    stack[top++]=u;    instack[u]=1;    for(int i=head[u]; ~i; i=next[i])    {        v=ver[i];        if(instack[v])        {            low[u]=low[u]<dfn[v]?low[u]:dfn[v];        }        else if(!dfn[v])        {            tarjan(v);            low[u]=low[u]<low[v]?low[u]:low[v];        }    }    if(low[u]==dfn[u])    {        ++cnt;        do        {            v=stack[--top];            instack[v]=0;            ///if(cnt==3)cout<<"v="<<v<<endl;            e_to[v]=cnt;        }        while(u^v);    }}int solve(){   int u,v;    for(int i=1; i<=n; ++i)        if(!dfn[i])        tarjan(i);    for(int i=1; i<=n; ++i)    {     for(int j=head[i];~j;j=next[j])     { u=i;       v=ver[j];       if(e_to[v]^e_to[u])       ++odeg[e_to[u]],++ideg[e_to[v]];     }    }    int in=0,out=0;   /// for(int i=1;i<=n;++i)   /// cout<<i<<" "<<e_to[i]<<" ";puts("");    if(cnt==1)return 0;    for(int i=1;i<=cnt;++i)///缩点后的点数    {      if(!ideg[i])++in;      if(!odeg[i])++out;    }    return in>out?in:out;}int main(){    int a,b;    while(~scanf("%d%d",&n,&m))    {   data();        for(int i=0; i<m; ++i)        {            scanf("%d%d",&a,&b);            add(a,b);        }        int ans=solve();        printf("%d\n",ans);    }}




原创粉丝点击