Luogu P3388 【模板】割点(割顶)

题目描述

https://www.luogu.com.cn/problem/P3388

Solution

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
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 20010
#define maxm 100010
using namespace std;

int n, m;

struct Edge {
int to, next;
} e[maxm * 2]; int c1, head[maxn];
inline void add_edge(int u, int v) {
e[c1].to = v; e[c1].next = head[u]; head[u] = c1++;
}

int dfn[maxn], low[maxn], c2; bool ans[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++c2; int du = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
if (!dfn[v]) { ++du;
tarjan(v, u); low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u] && fa) ans[u] = 1;
}
else low[u] = min(low[u], dfn[v]);
}
if (!fa && du > 1) ans[u] = 1;
}

int Ans;
int main() { memset(head, -1, sizeof head);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y; scanf("%d%d", &x, &y);
add_edge(x, y); add_edge(y, x);
}
for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= n; ++i) Ans += ans[i]; cout << Ans << endl;
for (int i = 1; i <= n; ++i) if (ans[i]) printf("%d ", i);
return 0;
}