<p>There are <code>n</code> servers numbered from <code>0</code> to <code>n - 1</code> connected by undirected server-to-server <code>connections</code> forming a network where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a connection between servers <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. Any server can reach other servers directly or indirectly through the network.</p>

<p>A <em>critical connection</em> is a connection that, if removed, will make some servers unable to reach some other server.</p>

<p>Return all critical connections in the network in any order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/1537_ex1_2.png" style="width: 198px; height: 248px;" />
<pre>
<strong>Input:</strong> n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
<strong>Output:</strong> [[1,3]]
<strong>Explanation:</strong> [[3,1]] is also accepted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2, connections = [[0,1]]
<strong>Output:</strong> [[0,1]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>n - 1 &lt;= connections.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>There are no repeated connections.</li>
</ul>

def to_connection_list(n: int, connections: List[List[int]]) -> List[List[int]]:
    result = [[] for i in range(n)]
    for (i, j) in connections:
        result[i].append(j)
        result[j].append(i)
    return result

def is_connected(n: int, connections: List[List[int]]) -> bool:
    connection_list = to_connection_list(n, connections)
    visited = set()
    to_visit = [0]
    while len(to_visit) > 0:
        current = to_visit.pop()
        if current in visited:
            continue
        visited.add(current)
        to_visit.extend(connection_list[current])
    return len(visited) == n

class SolutionOld:
    def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
        output = []
        for i in range(len(connections)):
            removed_connections = [connections[j] for j in range(len(connections)) if j != i]
            if not is_connected(n, removed_connections):
                output += [connections[i]]
        return output

def get_path_to_root(parents: Dict[int, int | None], i: int):
    path = []
    current = i
    while current is not None:
        path.append(current)
        current = parents[current]
    return path

def get_path(parents: Dict[int, int | None], i: int, j: int):
    path_i = get_path_to_root(parents, i)
    path_j = get_path_to_root(parents, j)
    subroot = None
    while len(path_i) > 0 and len(path_j) > 0 and path_i[-1] == path_j[-1]:
        path_i.pop()
        subroot = path_j.pop()
    return path_i + [subroot] + list(reversed(path_j))

class Solution:
    def criticalConnectionsOld2(self, n: int, connections: List[List[int]]) -> List[List[int]]:
        connection_list = to_connection_list(n, connections)
        parents = {0: None}
        cycle_edges = set()
        to_visit = [(0, i) for i in connection_list[0]]
        while len(to_visit) > 0:
            current_i, current_j = to_visit.pop()
            if current_j not in parents:
                parents[current_j] = current_i
                for next_node in connection_list[current_j]:
                    to_visit.append((current_j, next_node))
            else:
                path_i_to_j = get_path(parents, current_i, current_j)
                if len(path_i_to_j) == 2:
                    continue
                path_i_to_j.append(current_i)
                for k in range(len(path_i_to_j) - 1):
                    i, j = path_i_to_j[k], path_i_to_j[k + 1]
                    if j < i:
                        i, j = j, i
                    cycle_edges.add((i, j))

        result = []
        for i, j in connections:
            if (i, j) not in cycle_edges and (j, i) not in cycle_edges:
                result.append([i, j])
        return result

class Solution:
    def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
        connection_list = to_connection_list(n, connections)
    
        stack = [0]
        stack_to_level = {0: 0}
        visited = {0}
        to_visit = [[(0, i) for i in connection_list[0]]]
        level = 0
        smallest_level = {}
        cycle_edges = set()

        while len(to_visit) > 0:
            #print(stack, to_visit, smallest_level)
            current_level = to_visit[-1]
            if len(current_level) == 0:
                current = stack[-1]
                previous = None if len(stack) == 1 else stack[-2]
                stack.pop()
                to_visit.pop()
                level -= 1
                #print(previous, current, smallest_level, level)
                if current in smallest_level and smallest_level[current] <= level:
                    if previous not in smallest_level:
                        smallest_level[previous] = smallest_level[current]
                    else:
                        smallest_level[previous] = min(smallest_level[previous], smallest_level[current])
                    cycle_edges.add((previous, current))
            else:
                parent, current = current_level.pop()
                if current in visited:
                    if parent in smallest_level:
                        smallest_level[parent] = min(smallest_level[parent], stack_to_level[current])
                    else:
                        smallest_level[parent] = stack_to_level[current]
                    cycle_edges.add((parent, current))
                else:
                    stack.append(current)
                    level += 1
                    stack_to_level[current] = level
                    visited.add(current)
                    to_visit.append([(current, next_node) for next_node in connection_list[current] if next_node != parent])

        result = []
        for i, j in connections:
            if (i, j) not in cycle_edges and (j, i) not in cycle_edges:
                result.append([i, j])
        return result



73031