Practice
[(0,1,1), (1,2,2), (0,2,3)] and n=3, what is the value of mst_weight after processing all edges?Solution
Step 1: Sort edges by weight
Edges sorted: (0,1,1), (1,2,2), (0,2,3)Step 2: Process edges and union sets
Union(0,1) succeeds, mst_weight=1; Union(1,2) succeeds, mst_weight=3; Union(0,2) fails (already connected).Final Answer:
Option A -> Option AQuick Check:
Sum of edges in MST is 1+2=3 [OK]
- Adding all edges regardless of cycle
- Off-by-one in edges_used count
- Misordering edges before union
Solution
Step 1: Understand problem connectivity
Stones can be removed if they share a row or column, which forms connected components of stones.Step 2: Recognize union-find suitability
Union-Find efficiently groups stones by connecting those sharing rows or columns, enabling counting of connected components.Final Answer:
Option B -> Option BQuick Check:
Union-Find finds connected components in near-linear time [OK]
- Assuming greedy removal always works
- Trying DP without connectivity insight
- Sorting stones doesn't solve connectivity
Solution
Step 1: Understand DFS visit marking
Visited nodes must be marked before recursive calls to avoid revisiting and infinite loops.Step 2: Identify bug in visit marking
Marking visited after recursion causes repeated visits and possible infinite recursion.Final Answer:
Option A -> Option AQuick Check:
Visited marking must be before recursion to prevent cycles [OK]
- Marking visited too late
- Confusing visited marking with neighbor iteration
Solution
Step 1: Understand the new constraint
Emails can belong to multiple users, so union-find's single parent per node model breaks down.Step 2: Choose correct data structure
A bipartite graph connecting accounts and emails with connected components allows shared emails without forcing merges.Step 3: Evaluate other options
Union-find cannot represent multiple parents; greedy assignment loses correctness.Final Answer:
Option A -> Option AQuick Check:
Graph approach models shared emails correctly [OK]
- Trying to modify union-find for multiple parents
- Ignoring shared emails and merging incorrectly
Solution
Step 1: Understand reuse impact
Reusing cables means the only constraint is having enough cables to connect all nodes, i.e., at least n-1 connections.Step 2: Apply union-find with check
We still count connected components and need at least n-1 cables; if so, minimum operations = components - 1; else -1.Step 3: Reject naive assumptions
No change needed; the original union-find solution still applies directly. ignores cable count; Use a BFS or DFS to find the minimum spanning tree since reuse allows cycles. is unnecessary; Count connected components and return zero if at least one connection exists, since reuse removes cable limits. incorrectly assumes any connection suffices.Final Answer:
Option A -> Option AQuick Check:
Minimum cables needed remains n-1 even with reuse [OK]
- Assuming reuse means zero operations
- Ignoring minimum cable count
- Switching to BFS unnecessarily
