割点与割边
low值
对于一个无向连通图
- 如果
v 点没有遍历过,那么我们认为这条边是有效边,继续向v 前进 - 如果
v 点已经遍历过了,那么我们认为这条边是无效边,忽略这条边
显然所有的有效边构成一棵以出发点为根的树
我们规定对于有效边,只允许从父亲走向儿子,对于无效边,我们至多经过一条,并且定义
我们讨论
- 显然
low_u<=dfn_u - 对于
u 点连接的任意一条有效边对应的子节点v ,有low_u<=low_v - 对于
u 点连接的任意一条无效边对应的点v ,有low_u<=dfn_v
-
u$ 不为出发点,且 $dfs$ 树中 $u$ 点存在至少一个子节点 $v$ ,满足 $dfn_u<=low_v
割边的判定:
-
u-v$ 为有效边,在 $dfs$ 树中 $u$ 为 $v$ 的父节点,且 $dfn_u<low_v
时间复杂度为
参考代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10;
struct edge{
int to,val,nxt;
}e[maxn<<1];
int head[maxn],tot=2;
void addedge(ll u,ll v,ll w){
e[tot]=edge{v,w,head[u]};head[u]=tot++;
e[tot]=edge{u,w,head[v]};head[v]=tot++;
}
int root,dfn[maxn],low[maxn],dfc,n,m;
bool p_cut[maxn],e_cut[maxn<<1];//p_cut[i]=1则第i个点为割点,e_cut[2*i]=e_cut[2*i+1]=1则第i条边为割边
void tarjan(int u,int e_in){
dfn[u]=low[u]=++dfc;
int child=0;
for(int i=head[u];~i;i=e[i].nxt){
if(i==(e_in^1)) continue;//^的优先级非常小,一定记得加括号
int v=e[i].to;
if(!dfn[v]){
child++;
tarjan(v,i);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]&&u!=root) p_cut[u]=1;//割点(根要特判)
if(dfn[u]<low[v]) e_cut[i]=e_cut[i^1]=1;//割边
}
else low[u]=min(low[u],dfn[v]);
}
if(u==root&&child>=2) p_cut[u]=1;//特判根
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(root=i,-1);
}