洛谷 P3388 【模板】割点(割顶)
题目
思路
tarjan求割点
Code
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
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\n",tot);
for(int i=1;i<=n;++i){
if(cut[i]) printf("%d ",i);
}
puts("");
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步