#DSU0000051. Đưa các giá trị về vị trí đúng (Equals)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đưa các giá trị về vị trí đúng (Equals)
Equals
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of and a list of allowed pairs of positions. In one operation, choose any listed pair and swap the values currently stored at those two positions.
Each pair may be used any number of times, and swaps may be performed in any order. Repeated swaps can indirectly move a value through several positions. A position is correct if its final value is .
Find the maximum number of positions that can be correct simultaneously after some sequence of allowed swaps. The entire permutation does not necessarily have to be sorted.
Input
The first line contains , the second the permutation , and each of the next lines contains .
Output
Print the maximum number of simultaneously correct positions.
Subtasks
- Subtask 1 (20 points): , ; ; is a permutation of ; , ; allowed pairs are distinct.
- Subtask 2 (30 points): , ; ; is a permutation of ; , ; allowed pairs are distinct.
- Subtask 3 (50 points): , ; ; is a permutation of ; , ; allowed pairs are distinct.
Examples
Input
5 2
5 3 1 4 2
1 3
5 4
Output
2
Explanation
Position 4 is already correct, and swapping positions 1 and 3 fixes position 1, giving 2.