Run the log backwards
transcript
A network loses links, one at a time. After each cut, count the pieces. Union find cannot split a group.
There can be a hundred thousand cuts, one by one. Rebuilding costs about a hundred and fifty thousand steps. That is fifteen billion in all. Far too slow.
Read the whole log first, before any join. Join the links that are never cut. Then put the cuts back, last to first. A removal becomes an addition.
Link two is never cut. Four pieces remain. Before link zero returns, there are four pieces. Before link three returns, there are three. Before link one returns, there are two. Read the answers in cut order.
First join what is never cut. Then walk the cuts backwards. Record the count, then join the link.
Union find only joins. So turn removals into additions. Record before you restore. Then every answer is correct. Full breakdown in the lecture.