【模板】Tarjan强联通分量

C++版本:

Tarjan强联通分量

void tarjan(int u) {
	int v;
	dfn[u] = low[u] = ++in;
	instack[u] = true;
	Stap[++stop] = u;
	for (int i = head[u]; i; i = eg[i].nex) {
		v = eg[i].to;
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[v], low[u]);
		}
		else if (instack[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (dfn[u] == low[u]) {
		++Bcnt;
		while (true) {
			v = Stap[stop--];
			instack[v] = false;
			belong[v] = Bcnt;
			all[Bcnt]++;
			if (v == u)break;
		}
	}
}
posted @ 2020-09-13 18:35  pjhui  阅读(102)  评论(0编辑  收藏  举报