Tarjan

//求强连通分量 
void uni(int x,int y){ if (rank[x]<rank[y]){ fa[x]=y; size[y]+=size[x]; }else{ rank[x]+=(rank[x]==rank[y]); fa[y]=x; size[x]+=size[y]; } } int getfa(int po){ int t=po; while (t!=fa[t]) t=fa[t]; int root=t; while (po!=fa[po]){ t=fa[po]; fa[po]=root; po=t; } return(root); } void tarjan(int po){ low[po]=dfn[po]=++cnt; bt[po]=1; ins[po]=1; sta[++top]=po; if (bt[des[po]]==0){ tarjan(des[po]); low[po]=min(low[po],low[des[po]]); }else{ if (ins[des[po]]) low[po]=min(low[po],dfn[des[po]]); } if (low[po]==dfn[po]){ while (sta[top]!=po){ ins[sta[top]]=0; int t1=sta[top--]; if (getfa(t1)!=getfa(po)) uni(getfa(t1),getfa(po)); } ins[sta[top--]]=0; } }

 

posted @ 2016-07-25 16:40  z1j1n1  阅读(184)  评论(0编辑  收藏  举报