루미 알고리즘

#1580플래티넘Avalanche

PO 2024 결선코딩출제 Olle Lapidus영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

After Joshua saw through Alexander's attempts to fake pictures of snowflakes (See https://open.kattis.com/problems/falskflinga), Alexander has become vengeful and obsessed with snow. He has come up with an evil master plan which could be the end of Joshua, where the first step is to invite him on a skiing trip.

Together, they are now in a large skiing system with N ski villages numbered from 1 to N. The villages are connected by N-1 one-way ski slopes. At the top of the ski system is village 1, and from there it is possible to reach every other village by going down one or more skiing slopes. The rest of the villages each have exactly one slope leading into them.

The second step of Alexander's evil master plan is to start an avalanche in the skiing system. He plans to do this by making loud noises in one of the villages. The snow in this village will then tumble down along all slopes originating from the village. The snow will keep tumbling downward in all directions as far as possible. The damage of the avalanche is the number of villages affected.

Joshua, noticing that Alexander does not seem to be in his right mind, has seen through Alexander's evil master plan. Joshua is, as known by all, handy, and can choose some villages to build walls in. A wall in a village prevents the avalanche from reaching it. Alexander cannot start the avalanche from a walled ski village.

But time is running out, and after warning other skiers about what is about to happen, Joshua only has time to build walls in K different villages. Joshua does not know in which village Alexander will make loud noises, but he wants to minimize the number of villages affected.

First, Joshua will build the K walls so that the maximum damage Alexander can cause is minimized. Then Alexander will make loud noises in the village that causes the maximum possible damage. Your task is to calculate the damage, i.e., the number of villages affected by the avalanche.

Input

The first line of input has the two numbers N and K (1 ≤ K < N ≤ 10^5), the number of villages in the skiing system, and the number of walls Joshua can build.

Then there is a row containing the N-1 numbers a_1, a_2, ..., a_{N-1}, where 1 ≤ a_i ≤ i. For each i = 1, 2, ..., N-1, there is a ski slope from the village with number a_i to the village with number i+1.

Output

Output a single number: The maximum damage Alexander can cause, given that Joshua builds the walls optimally.

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

GroupPointsConstraints
110a_i = i for all (1 ≤ i ≤ N-1).
210K = 1, N ≤ 1000
310N ≤ 20
415K = 1
525N ≤ 1000
630No additional constraints.

예제

예제 1

입력
5 2
1 2 1 1
출력
1

예제 2

입력
7 1
1 1 2 2 3 3
출력
3

예제 3

입력
7 2
1 2 2 4 1 6
출력
2

출처 표기

출처: Programmeringsolympiaden 2024년 결선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.