[Tarjan] 学习笔记

发布时间 2023-08-18 10:15:42作者: Zwjay

原理

强连通分量

讲得超级屌,这次比董晓好得多

void tarjan(int x)
{
	dfn[x] = low[x] = t ++;
	s.push(x);
	in[x] = true;
	for (int i = h[x]; i ; i = e[i].next)
	{
		int y = e[i].to;
		if (!dfn[y])
		{
			tarjan(y);
			low[x] = min(low[x], low[y]);
		}
		else if (in[y])
			low[x] = min(low[x], dfn[y]);
	}
	if (dfn[x] == low[x])
	{
		while (s.top() != x)
		{
			in[s.top()] = false;
			cout << s.top() << ' ';
			s.pop();
		}
		in[s.top()] = false;
		cout << s.top() << '\n';
		s.pop();
	}
}