Hobson's Trains
Time limit5sMemory limit512 MB
Each station has exactly one outgoing edge, and for every station the task asks how many stations can reach it in at most k steps along these edges.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Tree, Implementation
- Solved
- No attempts yet
Problem
Mr. Hobson has retired from running a stable and invested in a more modern form of transport, trains. He built a rail network with stations. He still wants to spare passengers from too many choices: from each station, a passenger can catch a train to exactly one other station. Such a trip is called a leg. Trips are one-way, so a passenger may not be able to return.
Hobson also sells exactly one type of ticket, which lets a passenger travel up to legs in one trip. Each station has one automated ticket reader at its exit (only one, so passengers do not need to decide which to use). The reader checks that the distance from the starting station to the final station does not exceed legs.
Each reader must be programmed with a list of valid starting stations. The longer the list, the more expensive the machine. For each station , count the stations (including itself) from which a customer can reach in at most legs.

Figure H.1: Sample Input 1 with . Each circle is 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 lines follow. The -th of them contains an integer (, ), the station that can be reached from station in one leg.
Output
Output lines. The -th line contains the number of stations from which station can be reached in at most legs.