洛谷 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
#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\n",tot); for(int i=1;i<=n;++i){ if(cut[i]) printf("%d ",i); } puts(""); return 0; }
posted @   yu__xuan  阅读(267)  评论(5编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开