ICPC 2019 · Problem H · Hobson’s Trains

43rd ICPC · Porto, Portugal

Statement

Time limit: 5 seconds

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 nn 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 kk 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 kk 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 AA, the number of stations (including AA) from which a customer can reach AA in at most kk 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 k = 2.

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 k=2k = 2.

Input

The first line of input contains two integers nn and kk, where nn (2n51052\le n\le 5\cdot 10^{5}) is the number of stations and kk (1kn11\le k \le n- 1) is the maximum number of legs that may be traveled on a ticket. Then follow nn lines, the iith of which contains an integer did_{i} (1din1\le di \le n and di=idi =i), the station which may be reached from station ii in one leg.

Output

Output nn lines, with the iith line containing the number of stations from which station ii can be reached in at most kk legs.

ICPC World Finals 2019 Problem H: Hobson’s Trains

Sample Input 1

6 2
5

Sample Output 1

5

Sample Input 2

5 3
5

Sample Output 2

ICPC World Finals 2019 Problem H: Hobson’s Trains

No official solution in the source collection.