#P6258. [ICPC 2019 WF] Hobson's Trains

[ICPC 2019 WF] Hobson's Trains

题目背景

Warning: If you submit a malicious program, you will be banned.

警告:恶意提交本题将被封号。

题目描述

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 n 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 legleg. 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.

输入格式

The first line of input contains two integers nn and kk, where nn (2n5×105)(2 \leq n \leq 5\times10^5) is the number of stations and kk (1kn1)(1 \leq k \leq n − 1) is the maximum number of legs that may be traveled on a ticket. Then follow nn lines, the ithi^{th} of which contains an integer did_i (1din(1 \leq d_i \leq n and dii)d_i ≠ i), the station which may be reached from station ii in one leg.

输出格式

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

6 2
2
3
4
5
4
3

1
2
4
5
3
1
5 3
2
3
1
5
4

3
3
3
2
2

提示

Source: ICPC World Finals 2019.