Store the closing order: e.g., remove , then , then .
Reverse the order: add , then , then .
Start with no barns open. Initialize DSU.
Add barns in reverse order. After each addition, check if all open barns are in one component.
Store answers, then reverse the answer array to get the original order. Time: per barn addition. Space: for DSU and the answer array.