Cake delivery
Time limit1sMemory limit64 MB
Given a directed graph where every village must be reachable from village 1, find the minimum number of walks starting at village 1 that together cover all vertices.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Greedy, Shortest path
- Solved
- No attempts yet
Problem
Mirko makes the best cake in his village, a grapefruit cheesecake. To advertise the recipe he decided to give at least one cake to each of the villages in his county. He hires several traveling salesmen for the job. A salesman carries as many cakes as he wants and hands them out while moving along the one way roads that connect the villages.
Every salesman starts in Mirko's village, which is village 1. Mirko picks the route of every salesman he hires. A route is any sequence of roads joined end to end, and it may pass through the same village more than once. A salesman is allowed to stay put, and such a salesman delivers only to village 1.
What is the smallest number of salesmen Mirko has to hire so that every village gets a cake?
Input
The first line contains the number of villages and the number of roads (, ).
Each of the next lines contains two integers and (). This means there is a one way road from village to village , so a salesman can go directly from village to village .
The same road may be given more than once, and and may be equal.
Output
Print the smallest number of salesmen Mirko has to hire. The input is always such that a way to give every village a cake exists.