#P14193. [ICPC 2024 Hangzhou R] Gathering Mushrooms
[ICPC 2024 Hangzhou R] Gathering Mushrooms
题目描述
BaoBao is picking mushrooms in a forest. There are locations in the forest, and in the -th location there grows an infinite amount of mushrooms of type . Each location also has a wooden sign. The sign of the -th location points to location (it is possible that ).
As it is very foggy in the forest, BaoBao decides to move between locations according to the signs just for safety. Starting from location with an empty basket, each time BaoBao walks into a location (including the starting location , and regardless of whether he has visited location before), he will pick one mushroom of type into his basket and move to location .
Given an integer , for each , determine the first type of mushroom that appears at least times in the basket.
输入格式
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first line contains two integers and (, ) indicating the number of locations and the required times of appearance of mushrooms.
The second line contains integers (), where is the type of mushroom growing in location .
The third line contains integers (), where is the location pointed to by the sign in location .
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
To decrease the size of output, for each test case, output one line containing one integer indicating , where is the answer for .
3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2
41
45
14
提示
For the first sample test case, , , , , , so you should output $1 \times 2 + 2 \times 3 + 3 \times 2 + 4 \times 3 + 5 \times 3 = 41$. Consider , the types of mushrooms BaoBao picks in order are , so mushrooms of type is the very first type which appears at least times in the basket.