Enter a forward graph to calculate the number of strong connected components of each node, and the number of strong connected components of the output
1#include <iostream>2#include <cstring>3#include <vector>4 using namespacestd;5 Const intmaxn=1024x768;6 structedge{7 intGo,next;8 };9 intVis[maxn],count=0, BOOK[MAXN];TenVector<edge>g,g2; Onevector<int>S; A intEND[MAXN],END2[MAXN]; - voidAddint from,intTo) {Edge e;e.go=to;e.next=end[ from]; G.push_back (e); end[ from]=g.size ()-1;} - voidADD2 (int from,intTo) {Edge e;e.go=to;e.next=end2[ from]; G2.push_back (e); end2[ from]=g2.size ()-1;} the voidDfsintu) - { -vis[u]=1; - for(intI=end[u];i;i=G[i].next) { + intgo=G[i].go; - if(!Vis[go]) DFS (go); + } A s.push_back (u); at } - voidDFS2 (intu) - { -book[u]=count; - for(intI=end2[u];i;i=G2[i].next) { - intgo=G2[i].go; in if(!Book[go]) DFS2 (go); - } to } + voidInit () - { thememset (Vis,0,sizeof(Vis)); *memset (End,0,sizeof(end)); $memset (End2,0,sizeof(End2));Panax Notoginsengmemset (book,0,sizeof(book)); - } the intMain () + { A intn,m,a,b; thescanf"%d%d",&n,&m); + init (); - for(intI=1; i<=m;i++) $ { $scanf"%d%d",&a,&b); - Add (A, b); - add2 (b,a); the } - for(intI=1; i<=n;i++)if(!Vis[i]) DFS (i);Wuyi for(inti=n-1; i>=0; i--)if(!Book[s[i]]) { thecount++; - DFS2 (S[i]); Wu } -cout<<count; About return 0; $}
A strong connected component-kosaraju algorithm for graphs