diff --git a/graph/articulations_bridges.cpp b/graph/articulations_bridges.cpp index 5065ed82442ae8b9e365dd6a02c1dadbe502ff5e..7d2d4041df9a2e14816cf09a3952ec447c4244a8 100644 --- a/graph/articulations_bridges.cpp +++ b/graph/articulations_bridges.cpp @@ -1,5 +1,5 @@ /** - * Articulations and Bridges + * Articulations and Bridges (Tarjan) * * Complexity (Time): O(n + m) * Complexity (Space): O(n + m) @@ -9,8 +9,10 @@ vector<int> graph[MAX]; int cont[MAX], parent[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) { int child = 0; cont[x] = 1; @@ -25,11 +27,10 @@ void dfs(int x) { low[x] = min(low[x], low[i]); if ((parent[x] == -1 && child > 1) || (parent[x] != -1 && low[i] >= L[x])) - // CAUTION: may be executed more than once for the same vertex - // ==== x is an articulation point ==== + articulations.pb(x); if (low[i] > L[x]) - // ==== (x,i) is a bridge ==== + bridges.pb(ii(x, i)); } else if (parent[x] != i) { low[x] = min(low[x], L[i]); @@ -41,4 +42,8 @@ void dfs(int x) { void tarjan(int v) { memset(parent, -1, sizeof parent); dfs(v); + + // Remove duplicates for articulations + sort(all(articulations)); + articulations.erase(unique(all(articulations)), articulations.end()); }