#6413. [ICPC 2024 Hangzhou R] Gathering Mushrooms
[ICPC 2024 Hangzhou R] Gathering Mushrooms
[ICPC 2024 Hangzhou R] Gathering Mushrooms
题目描述
BaoBao is picking mushrooms in a forest. There are $n$ locations in the forest, and in the $i$-th location there grows an infinite amount of mushrooms of type $t_i$. Each location also has a wooden sign. The sign of the $i$-th location points to location $a_i$ (it is possible that $a_i = i$).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 $T$ ($1 \le T \le 10^4$) 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 $\sum\limits_{i = 1}^n (i \times v_i)$, where $v_i$ is the answer for $s = i$.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