Concrete ideas See detailed website: Https://www.byvoid.com/blog/scc-tarjan;
Then the Tarjan template is sorted out, the function code of the specific array is commented.
const int n=100010;struct data{int to,next;} tu[n*2];int head[n];int ip;int dfn[n], low[n];///dfn[] indicates the number of steps in deep search, low[u] means u or u The subtree can be traced back to the earliest stack of nodes in the sequence number of the int sccno[n];///indent array, which represents a point corresponding to the indentation value of int step;int scc_cnt;///strong connected component number void init () {ip=0; memset (head,-1,sizeof (Head));} void Add (int u,int v) {tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++;} Vector<int> scc[n];///to get out of the contraction point, scc[i] inside the indentation I this indent specifically which points stack<int> s;void dfs (int u) {dfn[u] = low[u] = ++step; S.push (U); for (int i = head[u]; i!=-1; i=tu[i].next) {int v = tu[i].to; if (!dfn[v]) {DFS (v); Low[u] = min (Low[u], low[v]); } else if (!sccno[v]) low[u] = min (Low[u], dfn[v]); } if (low[u] = = Dfn[u]) {scc_cnt + = 1; Scc[scc_cnt].clear (); while (1) {int x = S.top (); S.pop (); if (sccno[x]! = scc_cnt) scc[scc_cnt].push_back (x); SCCNO[X] = scc_cnt; if (x = = u) break; } }}void Tarjan (int n) {memset (sccno, 0, sizeof (SCCNO)); memset (DFN, 0, sizeof (DFN)); Step = scc_cnt = 0; for (int i = 1; I <=n; i++) if (!dfn[i]) DFS (i);}
Tarjan template (indent, to find strongly connected components of the graph)