HDU 3836 Equivalent Sets(Tarjan+縮點),hdutarjan

來源:互聯網
上載者:User

HDU 3836 Equivalent Sets(Tarjan+縮點),hdutarjan
Problem DescriptionTo 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. 
InputThe 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. 
OutputFor each case, output a single integer: the minimum steps needed. 
Sample Input

4 03 21 21 3
 
Sample Output
42HintCase 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1. 
 強聯通縮點:添加幾條邊成強聯通分量:設縮點後所有點中出度為0的點為d_1,入度為0點為d_2,則答案為max(d_1,d_2);
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=20000+100;const int maxm=100000;struct node{    int u,v;    int next;}e[maxm];int head[maxn],cntE;int DFN[maxn],low[maxn];int s[maxm],top,index,cnt;int belong[maxn],instack[maxn];int in[maxn],out[maxn];int n,m;void init(){    top=cntE=0;    index=cnt=0;    CLEAR(DFN,0);    CLEAR(head,-1);    CLEAR(instack,0);//    CLEAR(belong,0);}void addedge(int u,int v){    e[cntE].u=u;e[cntE].v=v;    e[cntE].next=head[u];    head[u]=cntE++;}void Tarjan(int u){    DFN[u]=low[u]=++index;    instack[u]=1;    s[top++]=u;    for(int i=head[u];i!=-1;i=e[i].next)    {        int v=e[i].v;        if(!DFN[v])        {            Tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(instack[v])            low[u]=min(low[u],DFN[v]);    }    int v;    if(DFN[u]==low[u])    {        cnt++;        do{            v=s[--top];            belong[v]=cnt;            instack[v]=0;        }while(u!=v);    }}void work(){    REPF(i,1,n)      if(!DFN[i])  Tarjan(i);    if(cnt<=1)    {        puts("0");        return ;    }    CLEAR(in,0);    CLEAR(out,0);    for(int i=0;i<cntE;i++)    {        int u=e[i].u,v=e[i].v;        if(belong[u]!=belong[v])            in[belong[v]]++,out[belong[u]]++;    }    int d_1=0,d_2=0;    REPF(i,1,cnt)    {        if(!in[i])            d_1++;        if(!out[i])            d_2++;    }    printf("%d\n",max(d_1,d_2));}int main(){    int u,v;    while(~scanf("%d%d",&n,&m))    {        init();        for(int i=0;i<m;i++)        {            scanf("%d%d",&u,&v);            addedge(u,v);        }        work();    }    return 0;}



hdu 1269 tarjan WA 了 解釋

沒有迴圈調用
 
hdu1269 前向星+tarjan 不知道錯在什地方更改

dfs()沒有迴圈調用
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.