題目描述: 給定一個加權無向圖,找出最小生成樹(MST)中的「關鍵邊」和「偽關鍵邊」。
解題思路:
mstWeight。mstWeight,則該邊可以出現在某個 MST 中,為偽關鍵邊。使用 Union-Find(DSU)實現 Kruskal 演算法,每次排除/包含一條邊重新跑。
時間複雜度:O(m^2 * alpha(n)) — 對每條邊執行一次 Kruskal(O(m * alpha(n))),共 m 條邊。alpha 為反阿克曼函數,幾乎為常數。
空間複雜度:O(n + m) — Union-Find 陣列 O(n),邊陣列 O(m)。
1. Append original index to each edge, sort edges by weight
2. Compute mstWeight using standard Kruskal
3. For each edge i:
a. Exclude edge i, run Kruskal:
- If result > mstWeight or graph disconnected: edge is CRITICAL
b. Else force-include edge i, run Kruskal:
- If result == mstWeight: edge is PSEUDO-CRITICAL
4. Return [critical edges, pseudo-critical edges]Tarjan's Bridge + Cycle 分析:先建 MST,用 Tarjan 演算法找出橋邊(即關鍵邊)。對非關鍵邊,檢查替換後是否仍得到相同權重的 MST。時間 O(m * alpha(n)),但實作更複雜。
邊權分組 + 連通分量:將相同權重的邊分組,在每組內分析哪些邊是可互換的(偽關鍵)、哪些是必要的(關鍵)。時間可達 O(m * alpha(n)),是理論最優解但實作難度高。