int tol,head[maxn];
struct edge
{
int to,next;
}es[maxm];
void addedge( int u , int v )
{
es[tol].to = v;
es[tol].next = head[u];
head[u] = tol++;
}
int Low[maxn],Dfn[maxn],Stack[maxn],Belong[maxn],num[maxn];
int index,top,scc;
bool Instack[maxn];
void dfs( int u )
{
int v;
Low[u] = Dfn[u] = ++index;
Stack[top++] = u;
Instack[u] = true;
for ( int i=head[u] ; i!=-1 ; i=es[i].next )
{
v = es[i].to;
if ( !Dfn[v] )
{
dfs( v );
if ( Low[u]>Low[v] ) Low[u] = Low[v];
}
else if ( Instack[v]&&Low[u]>Dfn[v] )
Low[u] = Dfn[v];
}
if ( Low[u]==Dfn[u] )
{
scc++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
}while ( v!=u );
}
}
void Tarjan( int n )
{
memset( Dfn , 0 , sizeof(Dfn) );
memset( Instack , false , sizeof(Instack) );
memset( num , 0 , sizeof(num) );
index = scc = top = 0;
for ( int i=1 ; i<=n ; i++ )
if ( !Dfn[i] ) dfs( i );
}