Under construction

Still being built. Things may change

Hobson's Trains

Time limit5sMemory limit512 MB

Summary
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 nn 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 kk 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 kk 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 AA, count the stations (including AA itself) from which a customer can reach AA in at most kk legs.

Figure H.1: Sample Input 1 with k=2k = 2. Each circle is a station. The numbers outside the circles are the station numbers loaded into the ticket readers when k=2k = 2.

Input

The first line contains two integers nn and kk, where 2≤n≤5⋅1052 \le n \le 5 \cdot 10^5 is the number of stations and 1≤k≤n−11 \le k \le n - 1 is the maximum number of legs that may be traveled on a ticket. Then nn lines follow. The ii-th of them contains an integer did_i (1≤di≤n1 \le d_i \le n, di≠id_i \ne i), the station that can be reached from station ii in one leg.

Output

Output nn lines. The ii-th line contains the number of stations from which station ii can be reached in at most kk legs.

Examples2

  1. Example 1

    Input
    6 2
    2
    3
    4
    5
    4
    3
    
    Expected output
    1
    2
    4
    5
    3
    1
    
  2. Example 2

    Input
    5 3
    2
    3
    1
    5
    4
    
    Expected output
    3
    3
    3
    2
    2