forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaveexploration.cpp
80 lines (69 loc) · 1.8 KB
/
caveexploration.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> graph;
typedef vector<pair<int,int>> vpii;
typedef vector<bool> vb;
typedef vector<int> vi;
int dfstime = 0;
void find(graph& adj, vpii& bridges, vb& vis, vi& par, vi& hi, vi& lo, int curr) {
vis[curr] = true;
hi[curr] = lo[curr] = ++dfstime;
for(auto next : adj[curr]) {
if(!vis[next]) {
par[next] = curr;
find(adj, bridges, vis, par, hi, lo, next);
lo[curr] = min(lo[curr], lo[next]);
if(lo[next] > hi[curr]) {
bridges.push_back({next,curr});
}
}
else if(next != par[curr]) {
lo[curr] = min(lo[curr], hi[next]);
}
}
}
int dfs(graph& adj, vb& vis, set<pair<int,int>>& skip, int curr) {
vis[curr] = true;
int total = 1;
for(auto next : adj[curr]) {
if(vis[next]) {
continue;
}
if(skip.count({curr,next}) || skip.count({next,curr})) {
continue;
}
total += dfs(adj, vis, skip, next);
}
return total;
}
int main() {
int n, m;
cin >> n >> m;
// Read in graph
graph adj(n);
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
adj[n1].push_back(n2);
adj[n2].push_back(n1);
}
// Allocate memory
vpii bridges;
vb vis(n,false);
vi par(n,-1);
vi hi(n,-1);
vi lo(n,-1);
// Find bridges
find(adj, bridges, vis, par, hi, lo, 0);
// Put bridges in a better data structure
set<pair<int,int>> skip;
for(auto i : bridges) {
skip.insert(i);
}
// Reset memory
for(int i = 0; i < n; i++) {
vis[i] = false;
}
// See how many nodes can be reaced without using bridges
cout << dfs(adj, vis, skip, 0) << endl;
}