Tarjan模板
Tarjan模板
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<cmath>
#include<limits.h>
#include<climits>
#include<fstream>
#include<set>
typedef long long ll;
using namespace std;
const int N = 1e5 + 5;
int cnt;//强连通分量的个数
int low[N], num[N], dfn;
int sccno[N], top;//sccno::SCC Number,就是第几个SCC的编号
int stack[N];
vector<int>G[N];
void dfs(int u)//u:父节点,v:子节点
{
stack[top++] = u;
low[u] = num[u] = ++dfn;//num[u]同时担负了判断是否进入的职责
for (int i = 0; i < G[u].size(); ++i)
{
int v = G[u][i];
if (!num[v])
{
dfs(v);
low[u] = min(low[v], low[u]);
}
else if (!sccno[v])//如果没有scc编号,但是又跳到跳过的位置
low[u] = min(low[u], num[v]);
}
if (low[u] == num[u])
{
cnt++;
while (1)//该点以上都是一个scc
{
int v = stack[--top];
sccno[v] = cnt;
if (u == v)break;
}
}
}
void Tarjan(int n)
{
cnt = top = dfn = 0;
memset(sccno, 0, sizeof(sccno));
memset(num, 0, sizeof(num));
memset(low, 0, sizeof(low));
for (int i = 1; i <= n; i++)
if (!num[i])
dfs(i);//防止不连通
}
int main()
{
int n, m, u, v;
cin >> n >> m;//n:点,m:边
for (int i = 0; i < m; i++)
{
cin >> u >> v;
G[u].push_back(v);
}
Tarjan(n);
return 0;
}