|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + // Helper function to perform DFS traversal and find critical connections |
| 4 | + void DFS(int server, int timer, int parentServer, vector<int>& low, vector<int>& disc, vector<int>& visited, vector<vector<int>>& criticalEdge, vector<vector<int>>& connections){ |
| 5 | + |
| 6 | + // Mark the current server as visited |
| 7 | + visited[server] = true; |
| 8 | + |
| 9 | + // Set discovery time and lowest reachable time for the current server |
| 10 | + disc[server] = low[server] = timer++; // timer is incremented after setting the discovery time |
| 11 | + |
| 12 | + // Explore all neighboring servers (connections) |
| 13 | + for(int & neighbourServer : connections[server]){ |
| 14 | + |
| 15 | + // Skip the parent server to avoid going back to the parent in the DFS tree |
| 16 | + if(neighbourServer == parentServer) continue; |
| 17 | + |
| 18 | + // If the neighboring server is not visited, explore it |
| 19 | + if(!visited[neighbourServer]){ |
| 20 | + DFS(neighbourServer, timer, server, low, disc, visited, criticalEdge, connections); |
| 21 | + |
| 22 | + // After visiting the neighbor, update the low time for the current server |
| 23 | + low[server] = min(low[server], low[neighbourServer]); |
| 24 | + |
| 25 | + // If the lowest time of the neighbor is greater than the discovery time of the current server, |
| 26 | + // it indicates that the edge between current server and the neighbor is a critical connection. |
| 27 | + if(low[neighbourServer] > disc[server]) |
| 28 | + criticalEdge.push_back({server, neighbourServer}); |
| 29 | + }else { |
| 30 | + // If the neighboring server was already visited, update the low time |
| 31 | + low[server] = min(low[server], low[neighbourServer]); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Main function to find all critical connections in the network |
| 37 | + vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) { |
| 38 | + // Arrays to store the lowest time reachable from a server and the discovery time of each server |
| 39 | + vector<int> low(n, -1); |
| 40 | + vector<int> disc(n, -1); |
| 41 | + |
| 42 | + // Array to track if a server has been visited |
| 43 | + vector<int> visited(n, false); |
| 44 | + |
| 45 | + // To store the critical connections (edges) |
| 46 | + vector<vector<int>> criticalEdge; |
| 47 | + |
| 48 | + // Adjacency list to represent the network of servers |
| 49 | + vector<vector<int>> adjacencyList(n); |
| 50 | + |
| 51 | + // Build the adjacency list from the connections |
| 52 | + for(auto& edge : connections){ |
| 53 | + adjacencyList[edge[0]].push_back(edge[1]); |
| 54 | + adjacencyList[edge[1]].push_back(edge[0]); |
| 55 | + } |
| 56 | + |
| 57 | + // Start DFS from each server that hasn't been visited |
| 58 | + for(int i = 0; i < n; i++) |
| 59 | + if(!visited[i]) |
| 60 | + DFS(i, 0, -1, low, disc, visited, criticalEdge, adjacencyList); |
| 61 | + |
| 62 | + // Return the list of critical connections (edges) |
| 63 | + return criticalEdge; |
| 64 | + } |
| 65 | +}; |
0 commit comments