-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6799097
commit 842975f
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution | ||
{ | ||
public: | ||
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) | ||
{ | ||
for (auto& it : connections) g[it[0]].emplace_back(it[1]), g[it[1]].emplace_back(it[0]); | ||
low = vector<int>(n, 0); | ||
dfs(1); | ||
return res; | ||
} | ||
|
||
void dfs(int cur, int v=0, int p=-1) | ||
{ | ||
int dfn = cur; | ||
low[v] = cur; | ||
for (auto i : g[v]) | ||
{ | ||
if (i != p) | ||
{ | ||
if (low[i] == 0) | ||
{ | ||
dfs(++cur, i, v); | ||
if (low[i] > dfn) res.push_back({v, i}); | ||
} | ||
low[v] = min(low[v], low[i]); | ||
} | ||
} | ||
} | ||
private: | ||
vector<vector<int>> res; | ||
vector<int> low; | ||
unordered_map<int, vector<int>> g; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var g [][]int | ||
var low []int | ||
var res [][]int | ||
|
||
func criticalConnections(n int, connections [][]int) [][]int { | ||
g = make([][]int, n) | ||
low = make([]int, n) | ||
res = make([][]int, 0) | ||
for _, e := range connections { | ||
g[e[0]] = append(g[e[0]], e[1]) | ||
g[e[1]] = append(g[e[1]], e[0]) | ||
} | ||
dfs(1, 0, -1) | ||
return res | ||
} | ||
|
||
func dfs(cur, v, p int) { | ||
dfn := cur | ||
low[v] = cur | ||
for _, i := range g[v] { | ||
if i != p { | ||
if low[i] == 0 { | ||
cur++ | ||
dfs(cur, i, v) | ||
if low[i] > dfn { | ||
res = append(res, []int{i, v}) | ||
} | ||
} | ||
low[v] = min(low[v], low[i]) | ||
} | ||
} | ||
} | ||
|
||
func min(x, y int) int { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var criticalConnections = function(n, connections) { | ||
var g = [], low = Array(n), res = []; | ||
low.fill(0); | ||
for (let con of connections) { | ||
g[con[0]] = g[con[0]] || []; | ||
g[con[1]] = g[con[1]] || []; | ||
g[con[0]].push(con[1]); | ||
g[con[1]].push(con[0]); | ||
} | ||
|
||
var dfs=function(cur, v, p) { | ||
var dfn = cur; | ||
low[v] = cur; | ||
for (let i of g[v]) { | ||
if (i != p) { | ||
if (low[i] == 0) { | ||
cur++; | ||
dfs(cur, i, v); | ||
if (low[i] > dfn) { | ||
res.push([i, v]); | ||
} | ||
} | ||
low[v] = Math.min(low[v], low[i]); | ||
} | ||
} | ||
} | ||
dfs(1, 0, -1); | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def criticalConnections(self, n, connections): | ||
res, low, g = [], [None] * n, collections.defaultdict(list) | ||
for i, j in connections: | ||
g[i].append(j) | ||
g[j].append(i) | ||
|
||
def dfs(cur, v=0, p=None): | ||
dfn = low[v] = cur | ||
for i in g[v]: | ||
if i != p: | ||
if low[i] == None: | ||
cur += 1 | ||
dfs(cur, i, v) | ||
low[i] > dfn and res.append([v, i]) | ||
low[v] = min(low[v], low[i]) | ||
dfs(0) | ||
return res |