-
Notifications
You must be signed in to change notification settings - Fork 166
/
Necessary Cities.cpp
55 lines (47 loc) · 1.19 KB
/
Necessary Cities.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxN = 1e5+1;
bool vis[maxN], is_artic[maxN];
int N, M, timer, tin[maxN], low[maxN];
vector<int> ans, G[maxN];
void dfs(int u = 1, int p = -1){
vis[u] = true;
tin[u] = low[u] = ++timer;
int children = 0;
for(int v : G[u]){
if(v != p){
if(vis[v]) low[u] = min(low[u], tin[v]);
else {
dfs(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= tin[u] && p != -1){
if(!is_artic[u]){
ans.push_back(u);
is_artic[u] = true;
}
}
children++;
}
}
}
if(p == -1 && children > 1){
if(!is_artic[u]){
ans.push_back(u);
is_artic[u] = true;
}
}
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs();
int K = (int) ans.size();
printf("%d\n", K);
for(int i = 0; i < K; i++)
printf("%d%c", ans[i], (" \n")[i==K-1]);
}