1
0
Fork 0
This commit is contained in:
arielherself 2023-12-02 11:59:44 +08:00
parent 3a00516eff
commit 78b4006392
2 changed files with 39 additions and 0 deletions

20
tarjan/tarjan-cut.cc Normal file
View File

@ -0,0 +1,20 @@
void tarjan(int u, int father) {
vis[u] = true;
low[u] = dfn[u] = ++cnt;
int child = 0;
for (auto v : edge[u]) {
if (!vis[v]) {
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (father != u && low[v] >= dfn[u] && !flag[u]) {
flag[u] = true;
res++;
}
} else if (v != father) low[u] = min(low[u], dfn[v]);
}
if (father == u && child >= 2 && !flag[u]) {
flag[u] = true;
res++;
}
}

19
tarjan/tarjan-scc.cc Normal file
View File

@ -0,0 +1,19 @@
// st = stack, vis = visited, br = branch(result)
constexpr int MAXN=1e5+10;
int dfn[MAXN],low[MAXN],st[MAXN],vis[MAXN],br[MAXN];
int n,cnt,stn,scn;
vector<int>ch[MAXN];
void tarjan(int v){
dfn[v]=low[v]=++cnt;
st[stn++]=v;
vis[v]=1;
for(const auto&u:ch[v])
if(!dfn[u])tarjan(u),low[v]=min(low[v],low[u]);
else if(vis[u])low[v]=min(low[v],dfn[u]);
if(dfn[v]==low[v]){
++scn;
int u;
do u=st[--stn],vis[u]=0,br[u]=scn; while(u!=v);
}
}