#P6258. Rail Network Leg Count
Rail Network Leg Count
Rail Network Leg Count
Mr. Hobson has retired from running a stable and has invested in modern transport, trains. He has built a rail network with n stations. From each station, a passenger can catch a train to exactly one other station. Such a journey is called a $\textbf{leg}$ (i.e. a one‐way journey). Note that it might not be possible to return to the initial station.
Hobson offers exactly one type of ticket which allows a passenger to travel up to $k$ legs in one trip. At the exit from each station, there is an automated ticket reader which checks that the distance (in legs) from the initial station to the final station does not exceed $k$. In order to minimize the memory usage on these machines, each must be programmed with a list of valid starting stations.
Your task is to help Hobson: For each station A, determine the number of stations (including A itself) from which a customer can reach A in at most $k$ legs.
Note: The rail network is defined by a function f: {1, 2, \dots, n} \to {1, 2, \dots, n} where for each station i the train goes to station f(i) (and f(i) is guaranteed to be different from i).
Input constraints: You may assume that n is reasonably small so that a solution with a worst-case time complexity of O(n2) will pass.
inputFormat
The input consists of two lines:
- The first line contains two integers n and k where 1 ≤ n ≤ 105 (or as specified) and 0 ≤ k.
- The second line contains n integers. The i-th integer denotes the station that station i's train goes to (i.e. f(i)), where 1 ≤ f(i) ≤ n and f(i) \neq i.
Stations are numbered from 1 to n.
outputFormat
Output a single line with n integers where the i-th integer is the number of stations from which station i can be reached in at most $k$ legs.
sample
4 1
2 3 4 1
2 2 2 2
</p>