Feline Friendship

시간 제한2초메모리 제한1024 MB

요약
순열이 주어질 때, 어떤 사이클의 길이가 정확히 k가 되도록 최소 개수의 원소를 바꾼다.
난이도

보통10점 중 7점

유형
그래프, 그리디, 조합론
정답자
아직 제출이 없습니다

문제

There is a big community of nn cats in Delft. The cats are numbered from 11 to nn. Each cat has a favourite playing partner, p_ip\_i (cats can be very egocentric, so p_i=ip\_i = i is allowed). It turns out that no two cats share the same favourite playing partner, so the p_ip\_i are distinct.

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

To get kk 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 ii selects its favourite playing partner p_ip\_i, adding p_ip\_i to the team. Subsequently, cat p_ip\_i will select its favourite playing partner, adding p_p_ip\_{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 kk, the game can be played.

Sometimes, it is not possible to find a team of kk cats in this way. Therefore, you have decided to convince some cats to change their favourite playing partner. Formally, you repeatedly select a cat ii (1≤i≤n1 \leq i \leq n) and choose an xx (1≤x≤n1 \leq x\leq n) and update the playing partner p_i:=xp\_i \mathrel{\mathop:}= x After the change, it can be the case that p_1,p_2,p_3,…,p_np\_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, nn and kk (1≤n≤2⋅1051\leq n\leq 2 \cdot 10^5, 1≤k≤n1 \leq k \leq n), the number of cats and the team size for the football game.
  • One line with nn integers, p_1,p_2,…,p_np\_1, p\_2, \dots , p\_n (1≤p_i≤n1 \leq p\_i \leq n), where the iith integer is the favourite playing partner of cat ii.

It is guaranteed that the p_ip\_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.

예제2

  1. 예제 1

    입력
    2 1
    2 1
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5 5
    3 4 1 2 5
    
    예상 출력
    2