benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2019_H / Hobson’s Trains

Problem

Answer published by the source. Consult the official source to check your work against its answer.

platform

atcoder

question content

## Problem Statement 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 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 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 k k 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 k k 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 A A , the number of stations (including A A ) from which a customer can reach A A in at most k k legs. ## Input The first line of input contains two integers n n and k k , where n n ( 2n5105 2 \leq n \leq 5 \cdot 10^5 ) is the number of stations and k k ( 1kn1 1 \leq k \leq n - 1 ) is the maximum number of legs that may be traveled on a ticket. Then follow n n lines, the i i th of which contains an integer di d_i ( 1din 1 \leq d_i \leq n and dii d_i \neq i ), the station which may be reached from station i i in one leg. ## Output Output n n lines, with the i i th line containing the number of stations from which station i i can be reached in at most k k legs. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

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 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 k 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 k 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 A, the number of stations (including A) from which a customer can reach A in at most k legs.

## Input

The first line of input contains two integers n and k, where n ( 2≤n≤5⋅10⁵ ) is the number of stations and k ( 1≤k≤n−1 ) is the maximum number of legs that may be traveled on a ticket. Then follow n lines, the ith of which contains an integer d_(i) ( 1≤d_(i)≤n and d_(i)≠i ), the station which may be reached from station i in one leg.

## Output

Output n lines, with the ith line containing the number of stations from which station i can be reached in at most k legs.

## Sample Input 1
Original LaTeX notation
## Problem Statement

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 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 \( k \) 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 \( k \) 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 \( A \), the number of stations (including \( A \)) from which a customer can reach \( A \) in at most \( k \) legs.

## Input

The first line of input contains two integers \( n \) and \( k \), where \( n \) ( \( 2 \leq n \leq 5 \cdot 10^5 \) ) is the number of stations and \( k \) ( \( 1 \leq k \leq n - 1 \) ) is the maximum number of legs that may be traveled on a ticket. Then follow \( n \) lines, the \( i \)th of which contains an integer \( d_i \) ( \( 1 \leq d_i \leq n \) and \( d_i \neq i \) ), the station which may be reached from station \( i \) in one leg.

## Output

Output \( n \) lines, with the \( i \)th line containing the number of stations from which station \( i \) can be reached in at most \( k \) legs.

## Sample Input 1

Code

6 2
2
3
4
5
4
3

## Sample Output 1

Code

1
2
4
5
3
1

## Sample Input 2

Code

5 3
2
3
1
5
4

## Sample Output 2

Code

3
3
3
2
2

question title

Hobson’s Trains

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import