HDU 3836 equivalent sets (strongly connected component-Edge Addition)

Source: Internet
Author: User

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 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, 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. 
Compress the strongly connected component of the source image to obtain a directed acyclic graph. How many sides can be added to the graph to make it into a strongly connected graph, max (point with an inbound degree of 0, A point with an exit of 0) is the answer.


#include"stdio.h"#include"string.h"#include"queue"#include"vector"#include"algorithm"using namespace std;#define N 20005#define M 50005#define min(a,b) (a<b?a:b)const int inf=1000000;struct node{    int u,v,next;}e[M];int t,bcnt,index,stop,ans;int head[N],dfn[N],low[N],stap[N],mark[N],be[N];int indeg[N],out[N];void add(int u,int v){    e[t].u=u;    e[t].v=v;    e[t].next=head[u];    head[u]=t++;}void tarjan(int u){    int i,v;    dfn[u]=low[u]=++index;    stap[++stop]=u;    mark[u]=1;    for(i=head[u];i!=-1;i=e[i].next)    {        v=e[i].v;        if(!dfn[v])        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(mark[v])            low[u]=min(low[u],dfn[v]);    }    if(low[u]==dfn[u])    {        bcnt++;        do        {            v=stap[stop--];            mark[v]=0;            be[v]=bcnt;        }        while(u!=v);        ans++;    }}void solve(int n){    int i;    memset(dfn,0,sizeof(dfn));    index=stop=bcnt=0;    for(i=1;i<=n;i++)    {        if(!dfn[i])            tarjan(i);    }}void work(int n){    int i,j,u,v,t1,t2;    memset(indeg,0,sizeof(indeg));    memset(out,0,sizeof(out));    for(i=1;i<=n;i++)    {        u=be[i];        for(j=head[i];j!=-1;j=e[j].next)        {            v=be[e[j].v];            if(u!=v)            {                indeg[v]++;                out[u]++;            }        }    }    t1=t2=0;    for(i=1;i<=bcnt;i++)    {        if(indeg[i]==0)            t1++;        if(out[i]==0)            t2++;    }    printf("%d\n",t1>t2?t1:t2);}int main(){    int n,m,u,v;    while(scanf("%d%d",&n,&m)!=-1)    {        t=0;        memset(head,-1,sizeof(head));        while(m--)        {            scanf("%d%d",&u,&v);            add(u,v);        }        ans=0;        solve(n);       // printf("%d\n",ans);        if(ans==1)            printf("0\n");        else            work(n);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.