Evacuation
Time limit1sMemory limit128 MB
Find the maximum number of queens that can travel from checkpoint 1 to checkpoint N when only roads touching either end carry at most one ant.
- Level
Medium7 of 10
- Topics
- Graph
- Solved
- No attempts yet
Problem
Most of the ants working in the CC garden died in a sudden disaster, and only 1000 queen ants and a few worker ants survived. To keep the colony alive, the survivors decided to send some of the queens to a shelter. The shelter is very safe, and in a few months the colony will grow there again.
The shelter is laid out like this.
- The shelter has checkpoints and roads that join checkpoints.
- If , checkpoint sits at a higher altitude than checkpoint .
- A road always runs from the higher checkpoint to the lower one.
- An ant cannot move from a lower checkpoint to a higher one.
- Each road that touches checkpoint carries only one ant.
- Each road that touches checkpoint carries only one ant.
- At most one road directly joins two checkpoints and .
A road that touches neither checkpoint nor checkpoint has no limit on how many ants use it, and a checkpoint has no limit on how many ants stay there.
Every queen ant starts at checkpoint . A queen that never reaches checkpoint does not survive, so the ants want to send as many queens as possible to checkpoint . The remaining queens join the defense line.
Find the largest number of queen ants that can reach the shelter.
Input
The first line has the number of test cases .
The first line of each test case has the number of checkpoints () and the number of roads ().
Each of the next lines has two integers , (), meaning a road runs from checkpoint to checkpoint .
Output
For each test case, print on one line the largest number of queen ants that can reach checkpoint .