Link
Time limit2sMemory limit64 MB
Given a functional graph where each node has exactly one outgoing edge, find the minimum number of edges to add so every node is reachable from node 1 within K hops.
Problem
A school website is being reorganized. It has pages, numbered from to , and the homepage is page . The content is fine, but the pages are linked poorly: every page contains exactly one link, and that link points to some other (different) page. Because of this, a visitor who starts at the homepage often has to follow many links to reach a given page, and some pages may not be reachable from the homepage at all.
To improve this, new links may be added anywhere: a new link may be placed on any page and may point to any page. For an integer , the website is called -reachable if every page other than the homepage can be reached from the homepage by following at most links.
Given the website and the integer , compute the minimum number of links that must be added to make the website -reachable.
Input
The first line contains two integers and (, ): the number of pages and the maximum number of links a visitor may follow.
Each of the next lines contains two different integers and (), meaning that the single link on page points to page .
Output
Print a single integer: the minimum number of additional links needed to make the website -reachable.