Re: [閒聊] 每日leetcode

作者: Rushia (みけねこ的鼻屎)   2024-06-30 16:22:07
※ 引述《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
作者: sustainer123 (caster)   2024-06-30 16:23:00
大師
作者: oin1104 (是oin的說)   2024-06-30 16:23:00
我今天放棄 我去看解答 看那個什麼union find 有夠靠北誰想得到
作者: DJYOMIYAHINA (通通打死)   2024-06-30 16:23:00
看題目直接放棄 晚點再來看一次==我看了答案說union find就回去想 還是沒想到 我就這樣了
作者: sustainer123 (caster)   2024-06-30 16:26:00
我圖學得超爛 對不起

Links booklink

Contact Us: admin [ a t ] ucptt.com