为了能到远方,脚下的每一步都不能少.|

流氓兔LMT

园龄:6个月粉丝:4关注:5

📂图论
🔖图论
2025-01-20 19:36阅读: 4评论: 0推荐: 0

强连通分量tarjan

如果节点×是某个强连通分量在搜索树中遇到的第一个节点,那么这个强连通分量的其余节点肯定是在搜索树中以×为根的子树中。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;

int head[N], to[N], nxt[N], w[N];
int tot;
void add(int a, int b, int c)
{
	nxt[++tot] = head[a];
	head[a] = tot;
	to[tot] = b;
	w[tot] = c;
}

int dfn[N], low[N], tim;
stack<int> sta;
int in[N];
int scc[N],siz[N], cnt;
void tarjan(int u)
{
	dfn[u] = low[u] = ++tim;
	sta.push(u);
	in[u] = 1;
	for (int i = head[u]; i; i = nxt[i])
	{
		int v = to[i];
		if (!dfn[v])
		{
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if (in[v])//说明v为深度小于u,v已经在栈中了
		{
			low[u] = min(low[u], dfn[v]);
		}
	}
	if (dfn[u] == low[u])
	{
		cnt++;
		while (1)//现在栈顶到u为u的子树,它们是一组强连通分量
		{
			int v=sta.top();
			sta.pop();
			in[v] = 0;
			scc[v] = cnt; // SCC编号
			++siz[cnt];	  // SCC大小
			if(u==v)
				break;
		}
	}
}
int main()
{

	return 0;
}
posted @   流氓兔LMT  阅读(4)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起