Tarjan algorithm + indent: Finding strong connected Components POJ 2186

Source: Internet
Author: User

Strongly connected components: 1309. [HAOI2006] Popular Cattle

★ Import File: cow.in output file: cow.out Simple comparison
Time limit: 1 s memory limit: MB

"Title description"

The desire of every cow is to become one of the most popular cows. Now there are n cows, give you m-pairs of integers (a, B), indicating that cow A

B is popular. This relationship is transitive, and if a thinks B is popular, B thinks C is popular, then bull a also thinks that Ox C is popular. Your task is to find out how many cows are considered popular by all cows.

"Input Format"

Line 1th two integers n,m;

Next m line, two numbers per line, A, B, meaning that A is considered to be welcome (the information given may be repeated, that is, there may be multiple A, b)

"Output Format"

A number, that is, how many cows are considered popular by all cows.

"Sample Input"

3 3
0 S
2 1
2 3

"Sample Output"
1
"Data Range"

10% of Data n<=20,m<=50

30% of Data n<=1000,m<=20000

70% of Data n<=5000,m<=50000

100% of Data n<=10000,m<=50000

1 /*judge can have "cattle by all the cows think is welcome", that is, after the contraction point of the figure of the 0 point is only one, if the figure out of 0 points have multiple, certainly does not meet the situation, find out the point, and then output the strong connected components can be*/2#include <iostream>3 using namespacestd;4#include <cstdio>5#include <cstring>6#include <stack>7 #defineN 100108 #defineM 500109 BOOLIn_stack[n];Ten intLow[n],dfn[n],x,y,n,m,father[n]; One intchudu[n],t=0, topt=0, head[n]; A structedge{ -     intV,last,u; -}edge[m*2]; thestack<int>STA; - intAns[n],begi,en; - intRead () - { +     intsum=0, ff=1; -     Chars; +s=GetChar (); A      while(s<'0'|| S>'9') at     { -         if(s=='-') ff=-1; -s=GetChar (); -     } -      while(s>='0'&&s<='9') -     { insum=sum*Ten+s-'0'; -s=GetChar (); to     } +     returnsum*ff; - } the voidAdd_edge (intUintVintk) * { $edge[k].u=u;Panax Notoginsengedge[k].v=v; -edge[k].last=Head[u]; thehead[u]=K; + } A voidinput () the { +N=read (); m=read (); -      for(intI=1; i<=m;++i) $     { $X=read (); y=read (); - Add_edge (x,y,i); -     } the } - voidTarjan (intu)Wuyi { thelow[u]=dfn[u]=++topt; - sta.push (u); Wuin_stack[u]=true; -      for(intL=head[u];l;l=edge[l].last) About     { $         intv=edge[l].v; -         if(!Dfn[v]) -         { - Tarjan (v); Alow[u]=min (low[u],low[v]); +         } the         Else if(In_stack[v]) -low[u]=min (low[u],dfn[v]); $     } the     if(low[u]==Dfn[u]) the     { theans[++ans[0]]=u; the         intx; -          Do in         { thex=sta.top (); the Sta.pop (); Aboutin_stack[x]=false; thefather[x]=u; the} while(x!=u); the     } + } - voidSuo_dian () the {Bayi      for(intL=1; l<=m;++l) the     { the         if(father[edge[l].v]!=father[edge[l].u]) -         { -chudu[father[edge[l].u]]++; the         } the     } the } the intMain () - { theFreopen ("cow.in","R", stdin); theFreopen ("Cow.out","W", stdout); the input ();94      for(intI=1; i<=n;++i) the     { the         if(!Dfn[i]) the         {98 Tarjan (i); About         } -     }101 Suo_dian ();102     intsum=0, L;103      for(intI=1; i<=ans[0];++i)104       if(chudu[ans[i]]==0) the       {106sum++;107L=Ans[i];108       }109     if(sum>1) printf ("0"); the     Else if(sum==1)111     { thesum=0;113          for(intI=1; i<=n;++i) the           if(father[i]==l) thesum++; theprintf"%d\n", sum);117     }118 fclose (stdin); fclose (stdout);119     return 0; -}

Tarjan algorithm + indent: Finding strong connected Components POJ 2186

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.