整理了下模板。。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,maxm=+;
int low[maxn],dfn[maxn],s[maxn],beg[maxn],top,scc,cz;bool ins[maxn];
struct ted{int x,y;ted*nxt;}adj[maxm],*fch[maxn],*ms=adj;
void add(int x,int y){*ms=(ted){x,y,fch[x]};fch[x]=ms++;return;}
void tarjan(int u){
low[u]=dfn[u]=++cz;ins[u]=true;s[++top]=u;
for(ted*e=fch[u];e;e=e->nxt){
int v=e->y;if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v])low[u]=min(low[u],dfn[v]);
}if(low[u]==dfn[u]){
scc++;int t=-;while(t!=u)beg[t=s[top--]]=scc,ins[t]=false;
}return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
int n,m;
void init(){
n=read();m=read();int x,y;
while(m--){x=read();y=read();add(x,y);}
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++){
printf("%d:%d\n",i,beg[i]);
}
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
/*
5 5
1 2
2 3
3 4
4 5
5 1
*/