Skip to content
Snippets Groups Projects
Commit 4f29608f authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix articulations and bridges

parent c50abcfa
No related branches found
No related tags found
No related merge requests found
/** /**
* Articulations and Bridges * Articulations and Bridges (Tarjan)
* *
* Complexity (Time): O(n + m) * Complexity (Time): O(n + m)
* Complexity (Space): O(n + m) * Complexity (Space): O(n + m)
...@@ -9,8 +9,10 @@ vector<int> graph[MAX]; ...@@ -9,8 +9,10 @@ vector<int> graph[MAX];
int cont[MAX], parent[MAX]; int cont[MAX], parent[MAX];
int low[MAX], L[MAX]; int low[MAX], L[MAX];
vector<ii> bridges;
vector<int> articulations;
// Find all articulations and bridges in the graph // Finds all articulations and bridges in the graph
void dfs(int x) { void dfs(int x) {
int child = 0; int child = 0;
cont[x] = 1; cont[x] = 1;
...@@ -25,11 +27,10 @@ void dfs(int x) { ...@@ -25,11 +27,10 @@ void dfs(int x) {
low[x] = min(low[x], low[i]); low[x] = min(low[x], low[i]);
if ((parent[x] == -1 && child > 1) || (parent[x] != -1 && low[i] >= L[x])) if ((parent[x] == -1 && child > 1) || (parent[x] != -1 && low[i] >= L[x]))
// CAUTION: may be executed more than once for the same vertex articulations.pb(x);
// ==== x is an articulation point ====
if (low[i] > L[x]) if (low[i] > L[x])
// ==== (x,i) is a bridge ==== bridges.pb(ii(x, i));
} else if (parent[x] != i) { } else if (parent[x] != i) {
low[x] = min(low[x], L[i]); low[x] = min(low[x], L[i]);
...@@ -41,4 +42,8 @@ void dfs(int x) { ...@@ -41,4 +42,8 @@ void dfs(int x) {
void tarjan(int v) { void tarjan(int v) {
memset(parent, -1, sizeof parent); memset(parent, -1, sizeof parent);
dfs(v); dfs(v);
// Remove duplicates for articulations
sort(all(articulations));
articulations.erase(unique(all(articulations)), articulations.end());
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment