|
65 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 66 |
|
67 | 67 | ```python
|
68 |
| - |
| 68 | +class Solution: |
| 69 | + def criticalConnections( |
| 70 | + self, n: int, connections: List[List[int]] |
| 71 | + ) -> List[List[int]]: |
| 72 | + def tarjan(a: int, fa: int): |
| 73 | + nonlocal now |
| 74 | + now += 1 |
| 75 | + dfn[a] = low[a] = now |
| 76 | + for b in g[a]: |
| 77 | + if b == fa: |
| 78 | + continue |
| 79 | + if not dfn[b]: |
| 80 | + tarjan(b, a) |
| 81 | + low[a] = min(low[a], low[b]) |
| 82 | + if low[b] > dfn[a]: |
| 83 | + ans.append([a, b]) |
| 84 | + else: |
| 85 | + low[a] = min(low[a], dfn[b]) |
| 86 | + |
| 87 | + g = [[] for _ in range(n)] |
| 88 | + for a, b in connections: |
| 89 | + g[a].append(b) |
| 90 | + g[b].append(a) |
| 91 | + |
| 92 | + dfn = [0] * n |
| 93 | + low = [0] * n |
| 94 | + now = 0 |
| 95 | + ans = [] |
| 96 | + tarjan(0, -1) |
| 97 | + return ans |
69 | 98 | ```
|
70 | 99 |
|
71 | 100 | ### **Java**
|
72 | 101 |
|
73 | 102 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 103 |
|
75 | 104 | ```java
|
| 105 | +class Solution { |
| 106 | + private int now; |
| 107 | + private List<Integer>[] g; |
| 108 | + private List<List<Integer>> ans = new ArrayList<>(); |
| 109 | + private int[] dfn; |
| 110 | + private int[] low; |
| 111 | + |
| 112 | + public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) { |
| 113 | + g = new List[n]; |
| 114 | + Arrays.setAll(g, k -> new ArrayList<>()); |
| 115 | + dfn = new int[n]; |
| 116 | + low = new int[n]; |
| 117 | + for (var e : connections) { |
| 118 | + int a = e.get(0), b = e.get(1); |
| 119 | + g[a].add(b); |
| 120 | + g[b].add(a); |
| 121 | + } |
| 122 | + tarjan(0, -1); |
| 123 | + return ans; |
| 124 | + } |
76 | 125 |
|
| 126 | + private void tarjan(int a, int fa) { |
| 127 | + dfn[a] = low[a] = ++now; |
| 128 | + for (int b : g[a]) { |
| 129 | + if (b == fa) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + if (dfn[b] == 0) { |
| 133 | + tarjan(b, a); |
| 134 | + low[a] = Math.min(low[a], low[b]); |
| 135 | + if (low[b] > dfn[a]) { |
| 136 | + ans.add(List.of(a, b)); |
| 137 | + } |
| 138 | + } else { |
| 139 | + low[a] = Math.min(low[a], dfn[b]); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
77 | 144 | ```
|
78 | 145 |
|
79 | 146 | ### **C++**
|
80 | 147 |
|
81 | 148 | ```cpp
|
82 | 149 | class Solution {
|
83 | 150 | public:
|
84 |
| - int count = 0; |
85 |
| - vector<int> dfn, low; |
86 |
| - vector<vector<int>> graph; |
87 |
| - vector<vector<int>> res; |
88 |
| - void tarjan(int u, int fa) { |
89 |
| - dfn[u] = low[u] = ++count; |
90 |
| - for (auto& v : graph[u]) { |
91 |
| - if (v == fa) |
92 |
| - continue; |
93 |
| - if (!dfn[v]) { |
94 |
| - tarjan(v, u); |
95 |
| - low[u] = min(low[u], low[v]); |
96 |
| - if (dfn[u] < low[v]) |
97 |
| - res.push_back({u, v}); |
98 |
| - } else { |
99 |
| - low[u] = min(dfn[v], low[u]); |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 | 151 | vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
|
105 |
| - dfn.resize(n); |
106 |
| - low.resize(n); |
107 |
| - graph.resize(n); |
108 |
| - for (auto& edge : connections) { |
109 |
| - graph[edge[0]].push_back(edge[1]); |
110 |
| - graph[edge[1]].push_back(edge[0]); |
| 152 | + int now = 0; |
| 153 | + vector<int> dfn(n); |
| 154 | + vector<int> low(n); |
| 155 | + vector<int> g[n]; |
| 156 | + for (auto& e : connections) { |
| 157 | + int a = e[0], b = e[1]; |
| 158 | + g[a].push_back(b); |
| 159 | + g[b].push_back(a); |
111 | 160 | }
|
112 |
| - for (int i = 0; i < n; i++) { |
113 |
| - if (!dfn[i]) |
114 |
| - tarjan(i, -1); |
115 |
| - } |
116 |
| - return res; |
| 161 | + vector<vector<int>> ans; |
| 162 | + function<void(int, int)> tarjan = [&](int a, int fa) -> void { |
| 163 | + dfn[a] = low[a] = ++now; |
| 164 | + for (int b : g[a]) { |
| 165 | + if (b == fa) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + if (!dfn[b]) { |
| 169 | + tarjan(b, a); |
| 170 | + low[a] = min(low[a], low[b]); |
| 171 | + if (low[b] > dfn[a]) { |
| 172 | + ans.push_back({a, b}); |
| 173 | + } |
| 174 | + } else { |
| 175 | + low[a] = min(low[a], dfn[b]); |
| 176 | + } |
| 177 | + } |
| 178 | + }; |
| 179 | + tarjan(0, -1); |
| 180 | + return ans; |
117 | 181 | }
|
118 | 182 | };
|
119 | 183 | ```
|
120 | 184 |
|
| 185 | +### **Go** |
| 186 | +
|
| 187 | +```go |
| 188 | +func criticalConnections(n int, connections [][]int) (ans [][]int) { |
| 189 | + now := 0 |
| 190 | + g := make([][]int, n) |
| 191 | + dfn := make([]int, n) |
| 192 | + low := make([]int, n) |
| 193 | + for _, e := range connections { |
| 194 | + a, b := e[0], e[1] |
| 195 | + g[a] = append(g[a], b) |
| 196 | + g[b] = append(g[b], a) |
| 197 | + } |
| 198 | + var tarjan func(int, int) |
| 199 | + tarjan = func(a, fa int) { |
| 200 | + now++ |
| 201 | + dfn[a], low[a] = now, now |
| 202 | + for _, b := range g[a] { |
| 203 | + if b == fa { |
| 204 | + continue |
| 205 | + } |
| 206 | + if dfn[b] == 0 { |
| 207 | + tarjan(b, a) |
| 208 | + low[a] = min(low[a], low[b]) |
| 209 | + if low[b] > dfn[a] { |
| 210 | + ans = append(ans, []int{a, b}) |
| 211 | + } |
| 212 | + } else { |
| 213 | + low[a] = min(low[a], dfn[b]) |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + tarjan(0, -1) |
| 218 | + return |
| 219 | +} |
| 220 | +
|
| 221 | +func min(a, b int) int { |
| 222 | + if a < b { |
| 223 | + return a |
| 224 | + } |
| 225 | + return b |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | +### **TypeScript** |
| 230 | + |
| 231 | +```ts |
| 232 | +function criticalConnections(n: number, connections: number[][]): number[][] { |
| 233 | + let now: number = 0; |
| 234 | + const g: number[][] = Array(n) |
| 235 | + .fill(0) |
| 236 | + .map(() => []); |
| 237 | + const dfn: number[] = Array(n).fill(0); |
| 238 | + const low: number[] = Array(n).fill(0); |
| 239 | + for (const [a, b] of connections) { |
| 240 | + g[a].push(b); |
| 241 | + g[b].push(a); |
| 242 | + } |
| 243 | + const ans: number[][] = []; |
| 244 | + const tarjan = (a: number, fa: number) => { |
| 245 | + dfn[a] = low[a] = ++now; |
| 246 | + for (const b of g[a]) { |
| 247 | + if (b === fa) { |
| 248 | + continue; |
| 249 | + } |
| 250 | + if (!dfn[b]) { |
| 251 | + tarjan(b, a); |
| 252 | + low[a] = Math.min(low[a], low[b]); |
| 253 | + if (low[b] > dfn[a]) { |
| 254 | + ans.push([a, b]); |
| 255 | + } |
| 256 | + } else { |
| 257 | + low[a] = Math.min(low[a], dfn[b]); |
| 258 | + } |
| 259 | + } |
| 260 | + }; |
| 261 | + tarjan(0, -1); |
| 262 | + return ans; |
| 263 | +} |
| 264 | +``` |
| 265 | + |
121 | 266 | ### **...**
|
122 | 267 |
|
123 | 268 | ```
|
|
0 commit comments