Tarjian Strong Unicom Component (template)

Source: Internet
Author: User

Come on, water template bar .....

#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineN 10000using namespacestd;intX,y,n,m,t,tot,sum,top,time;intHead[n],col[n],stack[n],dfn[n],low[n],a[n][n];BOOLVis[n];structedge{int  from, Next,to;} Edge[n];intAddintXinty) {Tot++; Edge[tot].to=y; Edge[tot].next=Head[x]; HEAD[X]=tot;}intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnf*x;}intTarjian (intNow ) {T=0; Dfn[now]=low[now]=++time;//the low value of each point at the beginning DFN equals its timestampStack[++top]=now; vis[now]=true;//put the point into the stack and mark it as on the stack     for(intI=head[now];i;i=edge[i].next)//Update to the low value of the point to which he is connected{x=edge[i].to; if(Vis[x]) low[i]=min (Dfn[x],low[i]);//If the point is already in the stack        Else if(!Dfn[x])            {Tarjian (x); Low[i]=min (Low[x],low[i]);//continue to expand from this point        }    }    if(Low[now]==dfn[now])//indicates that the strong connected component ends at this point{sum++;//number of strongly connected components plus oneCol[now]=sum;//put that point in the strong connectivity component she belongs to.         for(; stack[top]!=now;top--) {Col[stack[top]]=sum; Vis[stack[top]]=false; } Vis[now]=false; Top--; } }intMain () {n=read (), m=read ();  for(intI=1; i<=m;i++) {x=read (); y=read ();     Add (x, y); }     for(intI=1; i<=n;i++)      if(!Dfn[i]) Tarjian (i); printf ("%d", sum); return 0;}

Tarjian Strong Unicom Component (template)

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.