洛谷P3388 【模板】割點(割頂)

題目背景

割點html

題目描述

給出一個n個點,m條邊的無向圖,求圖的割點。ios

輸入輸出格式

輸入格式:ide

第一行輸入n,mspa

下面m行每行輸入x,y表示x到y有一條邊code

輸出格式:htm

第一行輸出割點個數blog

第二行按照節點編號從小到大輸出節點,用空格隔開get

輸入輸出樣例

輸入樣例#1: 
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6
輸出樣例#1: 
1 
5

說明

對於所有數據,n20000,m100000string

點的編號均大於0小於等於n。it

tarjan圖不必定聯通。

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 long long read()
 7 {
 8     long long x=0,f=1;
 9     char ch=getchar();
10     while(ch>'9'||ch<'0')
11     {
12         if(ch=='-')
13             f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 const int maxn=100005;
24 int n,m,num,cnt,ans;
25 int fa[maxn],low[maxn],head[maxn],dfn[maxn];
26 struct edge
27 {
28     int u,v,nxt;
29 } e[maxn<<1];
30 bool cut[maxn];
31 inline void add(int u,int v)
32 {
33     e[++num].u=u;
34     e[num].v=v;
35     e[num].nxt=head[u];
36     head[u]=num;
37 }
38 void tarjan(int x)
39 {
40     int in=0;
41     dfn[x]=low[x]=++cnt;
42     for(int v,i=head[x]; i; i=e[i].nxt)
43     {
44         v=e[i].v;
45         if(!dfn[v])
46         {
47             fa[v]=fa[x];
48             tarjan(v);
49             low[x]=fmin(low[x],low[v]);
50             if(low[v]>=dfn[x]&&x!=fa[x])
51                 cut[x]=true;
52             if(x==fa[x])
53                 ++in;
54         }
55         low[x]=fmin(low[x],dfn[v]);
56     }
57     if(x==fa[x]&&in>=2)
58         cut[fa[x]]=true;
59 }
60 int main()
61 {
62     n=read(),m=read();
63     for(int i=1; i<=n; ++i)
64         fa[i]=i;
65     for(int i=1; i<=m; ++i)
66     {
67         int u,v;
68         u=read(),v=read();
69         add(u,v);
70         add(v,u);
71     }
72     for(int i=1; i<=n; ++i)
73         if(!dfn[i])
74             tarjan(i);
75     for(int i=1; i<=n; ++i)
76         if(cut[i])
77             ++ans;
78     printf("%d\n",ans);
79     for(int i=1; i<=n; ++i)
80         if(cut[i])
81             printf("%d ",i);
82     return 0;
83 }
View Code
相關文章
相關標籤/搜索