※ 引述《smart0eddie (smart0eddie)》之銘言:
: 2024-06-30
: 1579. Remove Max Number of Edges to Keep Graph Fully Traversable
: Alice and Bob have an undirected graph of n nodes and three types of edges:
: Type 1: Can be traversed by Alice only.
: Type 2: Can be traversed by Bob only.
: Type 3: Can be traversed by both Alice and Bob.
: Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.
: Return the maximum number of edges you can remove, or return -1 if Alice and Bob cannot fully traverse the graph.
思路:
1.照著提示做,連通檢查用併查集,先把type3的邊分別給alice和bob,如果兩邊的點沒有
連通,用過的邊數+1。
2.處理剩下的兩種類型邊,如果兩點已經連通就忽略,否則用過的邊數+1。
3.檢查alice和bob是否是連通圖,不是返回-1。
4.因為我們先用type3的邊再用type2和type1,所以只要圖形連通就會自動刪除兩點之間
存在type3和其他兩種的邊,也就是會使用最少且連通的邊數,假定為 n,最多可以刪
除的邊數為: 總邊數-n(用過的邊)
java code