标签:
struct node{ int v,next; }e[M]; int head[N],cnt; int p[N],st[N],id,top,scc; int dfn[N],low[N],belong[N]; void add(int u,int v){ e[cnt].v=v,e[cnt].next=head[u]; head[u]=cnt++; } void init(){ memset(head,-1,sizeof(head)); memset(p,0,sizeof(p)); memset(dfn,0,sizeof(dfn)); id=top=cnt=0; } void dfs(int u){ dfn[u]=low[u]=++id; st[++top]=u;p[u]=1; int v; for(int i=head[u];i!=-1;i=e[i].next){ v=e[i].v; if(!dfn[v]){ dfs(v); if(low[v]<low[u])low[u]=low[v]; }else if(p[v]&&dfn[v]<low[u]){ low[u]=dfn[v]; } } if(dfn[u]==low[u]){ ++scc; do{ v=st[top--]; p[v]=0; belong[v]=scc; }while(v!=u); } } void Tarjian(int n){ for(int i=1;i<=n;i++){ if(!dfn[i]) dfs(i); } printf("%d\n",scc); for(int i=1;i<=n;i++){ printf("%d %d\n",i,belong[i]); } } int main(){ int n,m,u,v; scanf("%d%d",&n,&m); init(); while(m--){ scanf("%d%d",&u,&v); add(u,v); } Tarjian(n); return 0; }
标签:
原文地址:http://www.cnblogs.com/L-King/p/5659313.html