-
Notifications
You must be signed in to change notification settings - Fork 112
/
1192-CriticalConnectionsInANetwork.cs
63 lines (54 loc) · 2.23 KB
/
1192-CriticalConnectionsInANetwork.cs
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
//-----------------------------------------------------------------------------
// Runtime: 992ms
// Memory Usage: 83.4 MB
// Link: https://leetcode.com/submissions/detail/365305888/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1192_CriticalConnectionsInANetwork
{
public IList<IList<int>> CriticalConnections(int n, IList<IList<int>> connections)
{
var graph = new Dictionary<int, IList<int>>();
var edges = new HashSet<(int, int)>();
foreach (var connection in connections)
{
if (!graph.ContainsKey(connection[0]))
graph[connection[0]] = new List<int>();
if (!graph.ContainsKey(connection[1]))
graph[connection[1]] = new List<int>();
graph[connection[0]].Add(connection[1]);
graph[connection[1]].Add(connection[0]);
edges.Add((connection[0], connection[1]));
}
var ranks = new int[n];
for (int i = 0; i < n; i++)
ranks[i] = -2;
DFS(0, 0, graph, edges, ranks, n);
return edges.Select(a => new int[2] { a.Item1, a.Item2 }).ToList<IList<int>>();
}
private int DFS(int node, int depth, Dictionary<int, IList<int>> graph, HashSet<(int, int)> edges, int[] ranks, int n)
{
if (ranks[node] >= 0) return ranks[node];
ranks[node] = depth;
var minStartPoint = n;
foreach (var neighbor in graph[node])
{
if (ranks[neighbor] == depth - 1) continue;
var startPoint = DFS(neighbor, depth + 1, graph, edges, ranks, n);
if (startPoint <= depth)
{
if (edges.Contains((node, neighbor)))
edges.Remove((node, neighbor));
else if (edges.Contains((neighbor, node)))
edges.Remove((neighbor, node));
}
minStartPoint = Math.Min(minStartPoint, startPoint);
}
return minStartPoint;
}
}
}