#pragma warning(disable:4996)
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
#define N 20001
using namespace std;
vector<int>to[N];
int dfn[N], low[N], dfs_clock;
bool vis[N], inStack[N];
stack<int>s;
void init(){
dfs_clock = 0;
while (!s.empty())s.pop();
for (int i = 0; i < N; i++){
dfn[i] = low[i] = 0;
to[i].clear();
vis[i] = inStack[i] = false;
}
}
void add(int u, int v){
to[u].push_back(v);
}
void dfs(int u){
dfn[u] = low[u] = ++dfs_clock;
s.push(u);
vis[u] = inStack[u] = true;
for (int i = 0; i < to[u].size(); i++){
int v = to[u][i];
if (!vis[v]){
dfs(v);
low[u] = min(low[u], low[v]);
}
//注意这里是判断是否在栈里
else if (inStack[v]){
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]){
//这里是对强连通分量的操作
while (!s.empty()){
int now = s.top();
s.pop();
//记得把inStack标志去掉!!!
inStack[now] = false;
if (now == u)break;
}
}
}
tarjan算法之 强连通分量
最新推荐文章于 2023-06-05 21:27:11 发布