Hobson’s Trains
Mr. Hobson has retired from running a stable and has invested in a more modern form of transport, trains. He has built a rail network with stations. However, he has retained his commitment to free the passenger from the burden of too many choices: from each station, a passenger can catch a train to exactly one other station. Such a journey is referred to as a leg. Note that this is a one-way journey, and it might not be possible to get back again.
Hobson also offers exactly one choice of ticket, which allows a passenger to travel up to legs in one trip. At the exit from each station is an automated ticket reader (only one, so that passengers do not need to decide which to use). The reader checks that the distance from the initial station to the final station does not exceed legs.
Each ticket reader must be programmed with a list of valid starting stations, but the more memory this list needs, the more expensive the machine will be. Help Hobson by determining, for each station , the number of stations (including ) from which a customer can reach in at most legs.
Figure H.1: Illustration of Sample Input 1. Each circle represents a station. The numbers outside the circles are the station numbers loaded into the ticket readers when .
Input
The first line contains two integers and , where is the number of stations and is the maximum number of legs that may be traveled on a ticket. Then follow lines, the -th of which contains an integer and , the station which may be reached from station in one leg.
Output
Output lines, with the -th line containing the number of stations from which station can be reached in at most legs.