struct node{
int u,to,next,w;
}e[maxn*maxn];
int id[maxn],q[maxn],dfn[maxn],low[maxn],num[maxn];
int n,m,tsp,qe,cnt;
void init()
{
memset(head,-1,sizeof(head));
edge=0;
}
void addedge(int u,int v)
{
e[edge].u=u;
e[edge].to=v;
e[edge].next=head[u];
head[u]=edge++;
}
void tarjan(int u)
{
int i,v;
dfn[u]=low[q[qe++]=u]=++tsp;
for(i=head[u];i>=0;i=e[i].next)
if(!dfn[v=e[i].to])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if(id[v]<0)
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
++cnt;
do{
v=q[--qe];
++num[id[v]=cnt];
}while(v!=u);
}
}
void solve()
{
int i;
tsp=qe=cnt=0;
for(i=0;i<=n;++i)
id[i]=-1,dfn[i]=0;
for(i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
}