Let's make this one less mysterious. For 1192. Critical Connections in a Network, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- Tarjan's algorithm, find bridge
- https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/
- https://www.geeksforgeeks.org/bridge-in-a-graph/
- https://www.quora.com/q/competitiveprogramming2/Cut-Vertex-Articulation-point
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are bridgeUtil, visited, disc, low.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Tarjan's algorithm, find bridge
02//https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/
03//https://www.geeksforgeeks.org/bridge-in-a-graph/
04//https://www.quora.com/q/competitiveprogramming2/Cut-Vertex-Articulation-point
05//Runtime: 1356 ms, faster than 35.79% of C++ online submissions for Critical Connections in a Network.
06//Memory Usage: 167.3 MB, less than 79.16% of C++ online submissions for Critical Connections in a Network.
07class Solution {
08public:
09 void bridgeUtil(int cur, vector<vector<int>>& adj, vector<bool>& visited, vector<int>& disc, vector<int>& low, vector<int>& parent, int& time, vector<vector<int>>& edges){
10 visited[cur] = true;
11 disc[cur] = low[cur] = time++;
12
13 // cout << "discover " << cur << " at " << disc[cur] << endl;
14 // cout << cur << " has " << adj[cur].size() << " neighbors." << endl;
15
16 for(int nei : adj[cur]){
17 if(!visited[nei]){
18 parent[nei] = cur;
19 bridgeUtil(nei, adj, visited, disc, low, parent, time, edges);
20 /*
21 low[cur]: earliest visited vertex reachable
22 from subtree rooted with cur
23
24 because nei is a subtree of cur,
25 so we can update low[cur] with low[nei]
26 if low[nei] is smaller
27 */
28 low[cur] = min(low[cur], low[nei]);
29 // if(low[cur] == low[nei]) cout << "through low[" << nei << "], low[" << cur << "]: " << low[cur] << endl;
30
31 /*
32 cur has a subtree rooted at nei,
33 and nei cannot go to cur or
34 its ancestor through back edge
35 */
36 if(low[nei] > disc[cur]){
37 edges.push_back({cur, nei});
38 }
39 }else if(nei != parent[cur]){
40 //not understand?
41 low[cur] = min(low[cur], disc[nei]);
42 // if(low[cur] == disc[nei]) cout << "through disc[" << nei << "], low[" << cur << "]: " << low[cur] << endl;
43 }
44 }
45 };
46
47 vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
48 //adjacency list
49 vector<vector<int>> adj(n);
50
51 for(vector<int>& conn : connections){
52 //undirected graph
53 adj[conn[0]].push_back(conn[1]);
54 adj[conn[1]].push_back(conn[0]);
55 }
56
57 vector<bool> visited(n, false);
58 vector<int> disc(n);
59 //earliest visited vertex reachable from subtree rooted with v
60 vector<int> low(n);
61 vector<int> parent(n, -1);
62
63 vector<vector<int>> edges;
64
65 for(int i = 0; i < n; ++i){
66 if(!visited[i]){
67 // cout << "visit " << i << endl;
68 int time = 0;
69 bridgeUtil(i, adj, visited, disc, low, parent, time, edges);
70 }
71 }
72
73 return edges;
74 }
75};
Cost