tarjan无向图缩点

int root=1;
int belong[MAXN],tot;
int dfn[MAXN],low[MAXN],num;
int stack[MAXN],top;
bool instack[MAXN];
void tarjan(int u,int fa){
	dfn[u]=low[u]=++num;
	stack[++top]=u;
	instack[u]=true;
	int tto;
	for(int i=head[u];i;i=bl[i].inext){
		if(bl[i].to==fa)continue;
		if(!dfn[tto=bl[i].to]){
			tarjan(tto,u);
			low[u]=minn(low[u],low[tto]);
		}
		else low[u]=minn(dfn[tto],low[u]);
	}
	if(low[u]==dfn[u]){
		int tan=stack[top];
		++tot;
		while(tan!=u){
			belong[tan]=tot;
			instack[tan]=false;
			tan=stack[--top];
		}
		--top;
		belong[u]=tot;
		instack[u]=false;
	}
	return ;
}

 

转载于:https://www.cnblogs.com/2018hzoicyf/p/11182977.html

你可能感兴趣的:(tarjan无向图缩点)