Closing the Farm
InterviewTime limit2sMemory limit512 MB
After each barn closure in the given order, report whether the remaining open barns stay connected by open paths.
- Level
Medium6 of 10
- Topics
- Union-find, Graph
- Solved
- No attempts yet
Problem
Farmer John and his cows are leaving for a long vacation, so John wants to close the farm for a while and save money.
The farm has barns and two-way paths, and each path joins a pair of barns (). John closes the farm one barn at a time. When a barn closes, every path touching that barn closes with it and can no longer be used.
John wants to know, at the start and after each closing, whether the farm is fully connected. The farm is fully connected when you can travel from any open barn to any other open barn along a series of open paths. The farm is in poor repair, so it may not be fully connected even at the start. A farm with only one open barn counts as fully connected.
Input
The first line contains and . Each of the next lines contains the numbers of the two barns that one path joins. Barns are numbered through . More than one path may join the same pair of barns. The last lines give a permutation of through , the order in which the barns close.
Output
Print lines, each holding YES or NO. The first line tells whether the farm is fully connected at the start, and line tells whether the farm is fully connected after the th closing.