const int MAXN = 10010 ; int dfn [MAXN]; // i-th order of the points to be dfs int Low [MAXN]; // binary search on the minimum number of trees i dfn where the child still on the stack, low [ i] equal points in a strongly connected component BOOL VIS [MAXN]; Stack < int > S; Vector < int > to [MAXN]; int TOT; int RES [MAXN]; // store strongly connected component, res [ i] denotes the point i belonging RES [i] a strongly connected component int NUM [MAXN]; // i-th points strongly connected components void Tarjan ( int X) { DFN [X] = Low [X] = ++cnt; s.push(x); vis[x] = true; for (int i = 0; i < to[x].size(); i++) { if (!dfn[to[x][i]]) { tarjan(to[x][i]); low[x] = min(low[x], low[to[x][i]]); } else if (vis[to[x][i]]) { low[x] = min(low[x], dfn[to[x][i]]); } } if (low[x] == dfn[x]) { tot++; while (!s.empty() && s.top() != x) { int t = s.top(); res[t] = tot; num[tot]++; vis[t] = false; s.pop(); }; s.pop(); res[x] = tot; num[tot]++; vis[x] = false; } }