Test instructions: Seeking strong unicom components
Tarjan algorithm
1#include <iostream>2#include <stdio.h>3#include <stdlib.h>4#include <memory.h>5#include <string.h>6#include <algorithm>7#include <cmath>8#include <map>9 #defineCLC (A, B) memset (A,b,sizeof (a))TentypedefLong Doubleld; OnetypedefLong Longll; A Const intN = -; - Const Doubleeps=1e-9; - Const intinf=-100000; the Const intmaxn=1e5+Ten; - Const intnum=10001; - Const DoublePi=acos (-1); - using namespacestd; + - structGragh + { A intto ; at intNext; - } V[num]; - - BOOLinstack[num]= {false}; - intlow[num]= {0},dfn[num]= {0},stap[num]= {0},belong[num]= {0}; - intAnswer=0; in intdindex,stop,bcnt; - intHead[num]; to intEdge; + - voidAddintAintb) the { *v[edge].to=b; $v[edge].next=Head[a];Panax Notoginsenghead[a]=edge++; - the } + voidTarjan (intu) A { the intv; +dfn[u]=low[u]=++Dindex; -instack[u]=true; $stap[stop++]=u; $ for(intI=head[u]; i!=-1; I=v[i].next) - { -v=v[i].to; the if(!Dfn[v]) - {Wuyi Tarjan (v); the if(low[v]<Low[u]) -low[u]=Low[v]; Wu } - Else if(instack[v]&&dfn[v]<Low[u]) Aboutlow[u]=Dfn[v]; $ } - if(dfn[u]==Low[u]) - { - intsum=0; Abcnt++; + Do the { -v=stap[--stop]; $instack[v]=false; thebelong[v]=bcnt; thesum++; the } the while(v!=u); -cout<<sum<<Endl; in if(sum!=0) theanswer+= (sum* (sum-1))/2; the } About } the voidSolveintN) the { thestop=bcnt=dindex=0; + //CLC (dfn,0); - for(intI=1; i<=n; i++) the if(!Dfn[i])Bayi Tarjan (i); the } the - intMain () - { the intn,m; the intb; theCin>>n>>m; theCLC (head,-1); -Edge=0; the for(intI=0; i<m; i++) the { theCin>>a>>b;94 Add (A, b); the } the solve (n); thecout<<answer<<Endl;98 return 0; About}
View Code
CCF Certification 4