1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5#include <stack>6 using namespacestd;7 intN,m,ntime;//ntime Log DFS access time8 intinstack[11000];//whether the marker point is in the stack9 intdfn[11000];//The number of access to the node during the DFS process (also called the start time)Ten intlow[11000];//The start time of the first node in the DFS process from which the node below the node (the start time is greater than the start time of the node, and the node reachable by the node) can reach the beginning of the initial low array equal to the DFN array Onevector<vector<int> >G; Astack<int>St; - voidTarjan (intu) - { the inti,v; -dfn[u]=low[u]=ntime++; - st.push (u); -instack[u]=1; + for(i=0; I<g[u].size (); i++)//each side starting from U - { +v=G[u][i]; A if(dfn[v]==0)//v hasn't been visited yet. at { - Tarjan (v); -low[u]=min (low[u],low[v]); - } - Else if(Instack[v])//v in the stack - { inlow[u]=min (low[u],dfn[v]); - } to } + if(Dfn[u]==low[u])//U is the root of a strongly connected component - { the Do * { $v=st.top ();Panax Notoginseng St.pop (); -instack[v]=0; theprintf"%d", v);//outputs the number of all points that belong to this strongly connected component +} while(u!=v); Aprintf"\ n"); the } + } - intMain () $ { $ inti; - while(SCANF ("%d%d", &n,&m)! =EOF) - { the g.clear (); -G.resize (11000);Wuyi while(!st.empty ()) St.pop (); theNtime=1; -memset (Instack,0,sizeof(Instack)); Wumemset (DFN,0,sizeof(DFN)); -memset (Low,0,sizeof(Low)); About intu,v; $ for(i=0; i<m;i++)//point numbering starting from 1 - { -scanf"%d%d",&u,&v); - G[u].push_back (v); A } +Tarjan (1); the } - return 0; $}
Strong connected components of a forward connected graph without heavy edges