洛谷 P3388 【模板】割点(割顶)

题目

思路

tarjan求割点
不会的戳(不知道好不好自己康康吧)

(Code)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define min_(a,b) a>b?b:a;
#define MAXN 100010
int n,m,cnt,id;
int head[MAXN];
int dfn[MAXN],low[MAXN];
bool cut[MAXN];
struct Edge{
	int next,to;
}edge[200001];//无向图
inline int read(){
	int x=0;bool f=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return f?-x:x;
}
void add_edge(int from,int to){
	edge[++cnt].to=to,edge[cnt].next=head[from],head[from]=cnt;
}
void tarjan(int u,int father){
	dfn[u]=low[u]=++id;
	int ch=0;
	for(int i=head[u];i;i=edge[i].next){
		int x=edge[i].to;
		if(!dfn[x]){
			tarjan(x,father);
			low[u]=min_(low[u],low[x]);
			if(low[x]>=dfn[u]&&u!=father) cut[u]=1;
			if(u==father) ch++;
		}
		low[u]=min_(low[u],dfn[x]);
	}
	if(ch>=2&&u==father) cut[u]=1;
}

int main(){
	n=read(),m=read();
	int u,v;
	for(int i=1;i<=m;++i){
		u=read(),v=read();
		add_edge(u,v),add_edge(v,u);
	}
	for(int i=1;i<=n;++i){
		if(!dfn[i]) tarjan(i,i);
	}
	int tot=0;
	for(int i=1;i<=n;++i){
		if(cut[i]) tot++;
	}
	printf("%d
",tot);
	for(int i=1;i<=n;++i){
		if(cut[i]) printf("%d ",i);
	}
	puts("");
	return 0;
}
原文地址:https://www.cnblogs.com/poi-bolg-poi/p/11215857.html