-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNecessary Roads.cpp
81 lines (72 loc) · 2.14 KB
/
Necessary Roads.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
81
// say Alhamdulillah
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
ll timee = 0;
vector<ll> par, vis, low, explr;
vector<pair<ll, ll>> ans;
vector<vector<ll>> adj_list;
/**
* @brief Depth-First Search (DFS) to find bridges in a graph.
*
* This function performs a DFS traversal to find all the bridges in an undirected graph.
* A bridge is an edge which, when removed, makes the graph disconnected.
*
* @param curr The current node being explored.
* @param parnt The parent node of the current node.
*
* @details
* - `vis` is a vector that keeps track of visited nodes.
* - `low` is a vector that stores the lowest discovery time reachable from the subtree rooted with
* the node.
* - `explr` is a vector that stores the discovery times of visited nodes.
* - `timee` is a global variable to keep track of the discovery time.
* - `par` is a vector that stores the parent nodes in the DFS tree.
* - `adj_list` is the adjacency list representing the graph.
* - `ans` is a vector that stores the bridges found in the graph.
*
* The function updates the `low` and `explr` values for each node and checks if the current edge is
* a bridge.
*/
void dfs(ll curr, ll parnt) {
if (vis[curr] == 1) return;
low[curr] = explr[curr] = timee++;
par[curr] = parnt;
vis[curr] = 1;
for (ll neigh : adj_list[curr]) {
if (neigh == par[curr]) continue;
if (!vis[neigh]) {
dfs(neigh, curr);
low[curr] = min(low[curr], low[neigh]);
if (explr[curr] < low[neigh]) {
ans.push_back({neigh, curr});
}
} else {
low[curr] = min(low[curr], explr[neigh]);
}
}
}
int main() {
fast;
ll n, m, x, y, i;
cin >> n >> m;
low.resize(n + 1, LONG_LONG_MAX);
par.resize(n + 1, -1);
vis.resize(n + 1, 0);
explr.resize(n + 1, 0);
adj_list.resize(n + 1);
for (i = 1; i <= m; i++) {
cin >> x >> y;
adj_list[x].emplace_back(y);
adj_list[y].emplace_back(x);
}
dfs(1, 1);
cout << ans.size() << endl;
for (auto x : ans) {
cout << x.first << " " << x.second << endl;
}
}