Graph Theory37 sections · 1633 units
Open in Course

USACO 644 Closing the Farm - Solution

Adding Barns

1.1. Store the closing order: e.g., remove 11, then 33, then 22.

2.2. Reverse the order: add 22, then 33, then 11.

3.3. Start with no barns open. Initialize DSU.

4.4. Add barns in reverse order. After each addition, check if all open barns are in one component.

5.5. Store answers, then reverse the answer array to get the original order. Time: O(nα(n))O(n \cdot \alpha(n)) per barn addition. Space: O(n)O(n) for DSU and the answer array.