【模板】割点判定

引理:若图中一个点是割点,则满足:

  • 若该点不是搜索树的根节点:以该点为根的搜索子树中任意节点的 low[] 值均大于等于该点的时间戳 dfn[]。
  • 若该点是搜索树的根节点,则必须有两个或两个以上的搜索子树上的节点满足第一条性质。

割点的定义:将该点删去后,新生成的图(不包括删去的顶点)会分为两个或两个以上的不连通子图。

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxv=2e4+10;
const int maxe=1e5+10;

inline int read(){
	int x=0,f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
	return f*x;
}

struct node{
	int nxt,to;
}e[maxe<<1];
int tot=1,head[maxv];
inline void add_edge(int from,int to){
	e[++tot]=node{head[from],to},head[from]=tot;
}

int n,m;
int dfs_clk,root,sum,dfn[maxv],low[maxv];
bool cut[maxv];

void read_and_parse(){
	n=read(),m=read();
	for(int i=1,x,y;i<=m;i++){
		x=read(),y=read();
		if(x==y)continue;
		add_edge(x,y),add_edge(y,x);
	}
}

void dfs(int u,int fe){
	int flag=0;
	dfn[u]=low[u]=++dfs_clk;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(!dfn[v]){
			dfs(v,i);
			low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u]){
				++flag;
				if(!cut[u]&&(u!=root||flag>1))cut[u]=1,++sum;
			}
		}
		else if(i!=(fe^1))low[u]=min(low[u],dfn[v]);
	}
}

void solve(){
	for(int i=1;i<=n;i++)if(!dfn[i])root=i,dfs(i,0);
	printf("%d
",sum);
	for(int i=1;i<=n;i++)if(cut[i])printf("%d ",i);
}

int main(){
	read_and_parse();
	solve();
	return 0;	
}
原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10511151.html