Firefighter
Time limit1sMemory limit128 MB
On a graph with max degree 3, fire spreads one step per hour while one house can be protected each hour; maximize houses kept safe.
- Level
Hard8 of 10
- Topics
- Graph, Tree, Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
Bajtoland is a city that is extremely prone to fire. Its houses are connected by neighbor relations that form an undirected graph. Each house has at most neighboring houses.
One day a fire breaks out in exactly one house . Every hour the fire spreads from every currently burning house to all of its neighbors that are not yet burning and not protected.
Every house that has exactly neighbors is equipped with an automatic early fire-warning system, so a fire never originates in such a house. In other words, the house where the fire starts always has at most neighbors.
There is only a single firefighting aircraft, and it can carry unlimited firefighters. The aircraft moves between houses practically instantly, but dropping one firefighter takes one hour — exactly the time the fire needs to spread one step. Therefore, during each hour you may permanently protect one house that is not yet burning. A protected house never catches fire.
The timeline is as follows. At hour the fire starts in house . Then, during each hour, you may first protect one not-yet-burning house, and afterwards the fire spreads one step. This repeats until the fire can no longer spread.
Determine the maximum number of houses that can be kept safe from the fire.
Input
The first line contains the number of test cases ().
Each test case is given as follows.
- The first line contains the number of houses and the number of neighbor relations (, ). Houses are numbered from to .
- Each of the next lines contains two integers and , meaning that house and house are neighbors (the relation is symmetric).
- The last line contains the number () of the house where the fire starts.
Output
For each test case, print on its own line the maximum number of houses that can be kept safe from the fire.