// Tarjan algorithm for communicating the condensing point and the component Tuqiang #include <the iostream> #include <cstdio> #include <CString> #include <algorithm> #include <Vector> #include <Queue> the using namespace STD; const int = N 100010 , M = 1000010 ; // int Ver [M], the Next [M], head [N], DFN [N], Low [N]; int Stack [N], INS [N], C [N ]; int VC [m], NC [m], HC [N], TC; // strongly connected components Vector < int > SCC [N]; int n-, m, TOT, NUM, Top, CNT; voidthe Add ( int X, int Y) { Ver [ ++ TOT] = Y, the Next [TOT] = head [X], head [X] = TOT; } // after condensing point construction of FIG void add_c ( int X, int Y) { VC [ ++ TC] = Y, NC [TC] = HC [X], HC [X] = TC; } void Tarjan ( int X) { DFN [X] = Low [X] ++ = NUM ; Stack [ ++ Top] x =, INS [x] = . 1 ; // tag point x for ( int I = head [x]; I; I = the Next [I]) // not passed if(! DFN [Ver [I]]) { Tarjan (Ver [I]); // recursive update Low [X] = min (Low [X], Low [Ver [I]]); } the else IF (INS [ Ver [I]]) // updated directly Low [X] = min (Low [X], DFN [Ver [I]]); IF (DFN [X] == Low [X]) { // a strongly connected ++ CNT; int Y; do { Y = Stack [top--], INS [Y] = 0 ; // belongs to which strongly connected C [Y] = CNT, SCC [CNT] .push_back (Y); } the while (x != y); } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); for (int x = 1; x <= n; x++) for (int i = head[x]; i; i = Next[i]) { int y = ver[i]; if (c[x] == c[y]) continue; add_c(c[x], c[y]); } }