AlgoLensArena
ContestlərProblemlərOtaqlar
Vizualizator
← Məsələlər

FPC23-033.Feline Friendship

Easy · 2 s · 256 MB

  • icpc
  • fpc
  • 2023

There is a big community of nnn cats in Delft. The cats are numbered from 111 to nnn. Each cat has a favourite playing partner, pip_ipi​ (cats can be very egocentric, so pi=ip_i = ipi​=i is allowed). It turns out that no two cats share the same favourite playing partner, so the pip_ipi​ are distinct.

You are organising a big game of Cats versus Coatis football (Non-American.), for which you will need exactly kkk cats in a team.

To get kkk 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 iii selects its favourite playing partner pip_ipi​, adding pip_ipi​ to the team. Subsequently, cat pip_ipi​ will select its favourite playing partner, adding ppip_{p_i}ppi​​ 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 kkk, the game can be played.

Sometimes, it is not possible to find a team of kkk cats in this way. Therefore, you have decided to convince some cats to change their favourite playing partner. Formally, you repeatedly select a cat iii (1≤i≤n1 \leq i \leq n1≤i≤n) and choose an xxx (1≤x≤n1 \leq x\leq n1≤x≤n) and update the playing partner pi:=xp_i \mathrel{\mathop:}= xpi​:=x After the change, it can be the case that p1,p2,p3,…,pnp_1, p_2, p_3, \dots , p_np1​,p2​,p3​,…,pn​ 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?

Giriş formatı

The input consists of:

• One line with two integers, nnn and kkk (1≤n≤2⋅1051\leq n\leq 2 \cdot 10^51≤n≤2⋅105, 1≤k≤n1 \leq k \leq n1≤k≤n), the number of cats and the team size for the football game.

• One line with nnn integers, p1,p2,…,pnp_1, p_2, \dots , p_np1​,p2​,…,pn​ (1≤pi≤n1 \leq p_i \leq n1≤pi​≤n), where the iiith integer is the favourite playing partner of cat iii.

It is guaranteed that the pip_ipi​ are all distinct.

Çıxış formatı

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.

Qeyd

Source: Freshmen Programming Contest 2023 · Author: Jeroen Op de Beek · License: CC BY-SA 4.0 · Original: https://fpc.wisv.ch · Statement adapted for AlgoLens and shared under the same license. · 7 of the 27 original test cases (the largest) are omitted here to keep the problem lightweight.

Nümunələr

Giriş #1

2 1
2 1

Çıxış #1

1

Giriş #2

5 5
3 4 1 2 5

Çıxış #2

2