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)