//-----------Tarjan-------------
int dfn[MAXNODE],low[MAXNODE],index;
int stack[MAXNODE],top;
int id_scc[MAXNODE],cnt_scc;
void Tarjan(int i)
{
int j;
dfn[i]=low[i]=++index;
stack[++top]=i;
for(Node *p=G[i];p;p=p->next)
{
j=p->num;
if(!dfn[j])
{
Tarjan(j);
if(low[j]<low[i])
low[i]=low[j];
}
else if(!id_scc[j] && dfn[j]<low[i])
low[i]=dfn[j];
}
if(dfn[i]==low[i])
{
cnt_scc++;
do
{
j=stack[top--];
id_scc[j]=cnt_scc;
} while (i!=j);
}
}
void solve(int nodenum)
{
index=top=cnt_scc=0;
memset(dfn,0,sizeof(dfn));
memset(id_scc,0,sizeof(id_scc));
for(int i=1;i<=nodenum;i++)
{
if(!dfn[i])
Tarjan(i);
}
}