1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn= 100005 ; 4 int cnt= 0 ; // Number of strongly connected components 5 int stk[maxn]; // Temporarily store the traversed Point, throw out element 6 when encountering low[x]==dfn[x] int dfn[maxn]; // Timestamp [Because each point belongs to at most one strongly connected component, it is also used to distinguish whether It already exists in a strongly connected component. If it exists, there is no need to continue to search down.] 7 int low[maxn] // And the time of the earliest ancestor 8 int vis[maxn]; // Mark this traversal process into the stack dot, also clear marked sigil when stk[] throws dot 9 intsz[maxn]; // Number of elements in each strongly connected component 10 int tim= 1 ; 11 void tar( int x) 12 { 13 low[x]=dfn[x]=tim++ ; 14 vis[x] = 1 ; 15 stk[tot++]= x; 16 if (! dfn[next[x]]) 17 { 18 tar(next[x]); 19 low[x]= min(low[x],low[next [x]]); 20 } 21 else if (vis[next[x]]) 22 { 23 low[x]= dfn[next[x]]; 24 } 25 if (dfn[x]== low[x]) 26 { 27 int xx; 28 cnt++ ; 29 do { 30 xx=stk[--tot] ; // Throw element 31 col[xx]=cnt; // Color block 32 sz[cnt]++; // Number of elements in the strongly connected component where the element is located 33 vis[xx]= 0 ; // clear flag 34 } while (x!= xx); 35 } 36 } 37 int main() 38 { 39 for(int i = 1 ; i <= n ; i++) 40 { 41 tar(i); 42 } 43 return 0; 44 }