<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>

class Solution {
public:
    vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
        // Form adj list that contains forward and backward paths 
        vector<vector<int>> adj_list(n); 
        for (vector<int>& edge : connections) {
            adj_list[edge[0]].push_back(edge[1]);
            adj_list[edge[1]].push_back(edge[0]);
        }

        vector<int> entry_time(n); 
        vector<int> min_time_possible(n); 
        vector<int> visited(n, 0); 

        vector<vector<int>> critical_connections; 

        int global_time = 0; 
        
        function<void(int, int)> dfs = [&](int cur, int parent) {
            // Mark cur node as visited 
            visited[cur] = 1; 
            // Mark initial entry time and min time to cur global time 
            entry_time[cur] = min_time_possible[cur] = global_time++; 

            // Visit all edges spanning from cur node 
            for (int& neighbor : adj_list[cur]) {
                // Three cases:
                
                // neighbor is previous parent => don't want to re-explore
                if (neighbor == parent) continue; 

                // edge is backward if have visited node before 
                if (visited[neighbor]) {
                    min_time_possible[cur] = min(min_time_possible[cur], entry_time[neighbor]);
                } 
    
                // edge is forward
                else {
                    // visit all child dependencies first to ensure comparing w/ min low value  
                    dfs(neighbor, cur); 

                    // is a bridge if min_time for neighbor is higher than in time of cur 
                    if (min_time_possible[neighbor] > entry_time[cur]) {
                        critical_connections.push_back({cur, neighbor}); 
                    }

                    // update min_time_possible 
                    min_time_possible[cur] = min(min_time_possible[cur], min_time_possible[neighbor]);
                }
            }
        };

        dfs(0, -1); 
        return critical_connections; 
    }
};
170406