Logo
(追記) (追記ここまで)

32613번 - Feline Friendship 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB27141257.143%

문제

There is a big community of $n$ cats in Delft. The cats are numbered from 1ドル$ to $n$. Each cat has a favourite playing partner, $p_i$ (cats can be very egocentric, so $p_i = i$ is allowed). It turns out that no two cats share the same favourite playing partner, so the $p_i$ are distinct.

You are organising a big game of Cats versus Coatis football1, for which you will need exactly $k$ cats in a team.

To get $k$ cats to join your game, you appoint one cat as team captain. Then the following process is repeated, starting with the team captain cat. A cat $i$ selects its favourite playing partner $p_i,ドル adding $p_i$ to the team. Subsequently, cat $p_i$ will select its favourite playing partner, adding $p_{p_i}$ to the team, and so on. The process only stops when a cat tries to invite a cat that is already on the team. If, for some choice of the team captain, the number of cats in the team is exactly $k,ドル the game can be played.

Sometimes, it is not possible to find a team of $k$ cats in this way. Therefore, you have decided to convince some cats to change their favourite playing partner. Formally, you repeatedly select a cat $i$ (1ドル \leq i \leq n$) and choose an $x$ (1ドル \leq x\leq n$) and update the playing partner $p_i \mathrel{\mathop:}= x$ After the change, it can be the case that $p_1, p_2, p_3, \dots , p_n$ are no longer distinct, but that is fine.

What is the minimum number of times you need to convince a cat to change their favourite playing partner, such that the football game can be played?


1Non-American.

입력

The input consists of:

  • One line with two integers, $n$ and $k$ (1ドル\leq n\leq 2 \cdot 10^5,ドル 1ドル \leq k \leq n$), the number of cats and the team size for the football game.
  • One line with $n$ integers, $p_1, p_2, \dots , p_n$ (1ドル \leq p_i \leq n$), where the $i$th integer is the favourite playing partner of cat $i$.

It is guaranteed that the $p_i$ are all distinct.

출력

Output the minimum number of times you need to convince a cat to change their favourite playing partner, such that the football game can be played.

제한

예제 입력 1

2 1
2 1

예제 출력 1

1

예제 입력 2

5 5
3 4 1 2 5

예제 출력 2

2

힌트

출처

University > Delft University of Technology > Freshmen Programming Contest 2023 F번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /