Earthquake Damage
Time limit1sMemory limit128 MB
Given a graph and reports that certain damaged pastures cannot reach the barn, find the minimum total number of pastures that cannot return to the barn.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, DFS, Implementation
- Solved
- No attempts yet
Problem
An earthquake has struck Farmer John's farm in Wisconsin! The quake damaged some pastures so that they can no longer be crossed. Remarkably, none of the cowpaths was damaged.
The farm is a set of pastures numbered (), connected by undirected cowpaths numbered (). Cowpath joins pastures and (; ). A cowpath may join a pasture to itself, and two pastures may be joined by more than one cowpath. The barn is in pasture .
cows, each in a different pasture (), phone Farmer John one after another, each reporting a single integer (). The report means that pasture is undamaged, yet the calling cow cannot get back to the barn from it because no path avoiding damaged pastures exists.
After every cow has reported in, determine the minimum number of pastures from which it is impossible to return to the barn (counting the impassable damaged pastures themselves).
Input
- Line 1: Three space-separated integers: , , and
- Lines : Line describes cowpath with two integers and .
- Lines : Line contains a single integer .
Output
- Line 1: A single integer, the minimum number of pastures from which a cow cannot return to the barn (including the damaged pastures themselves).
Hint
For example, along the path , damaging pasture leaves the cows in pastures , , and unable to return to the barn.