CF1872F.Selling a Menagerie
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are the owner of a menagerie consisting of n animals numbered from 1 to n . However, maintaining the menagerie is quite expensive, so you have decided to sell it!
It is known that each animal is afraid of exactly one other animal. More precisely, animal i is afraid of animal ai ( ai=i ). Also, the cost of each animal is known, for animal i it is equal to ci .
You will sell all your animals in some fixed order. Formally, you will need to choose some permutation † p1,p2,…,pn , and sell animal p1 first, then animal p2 , and so on, selling animal pn last.
When you sell animal i , there are two possible outcomes:
- If animal ai was sold before animal i , you receive ci money for selling animal i .
- If animal ai was not sold before animal i , you receive 2⋅ci money for selling animal i . (Surprisingly, animals that are currently afraid are more valuable).
Your task is to choose the order of selling the animals in order to maximize the total profit.
For example, if a=[3,4,4,1,3] , c=[3,4,5,6,7] , and the permutation you choose is [4,2,5,1,3] , then:
- The first animal to be sold is animal 4 . Animal a4=1 was not sold before, so you receive 2⋅c4=12 money for selling it.
- The second animal to be sold is animal 2 . Animal a2=4 was sold before, so you receive c2=4 money for selling it.
- The third animal to be sold is animal 5 . Animal a5=3 was not sold before, so you receive 2⋅c5=14 money for selling it.
- The fourth animal to be sold is animal 1 . Animal a1=3 was not sold before, so you receive 2⋅c1=6 money for selling it.
- The fifth animal to be sold is animal 3 . Animal a3=4 was sold before, so you receive c3=5 money for selling it.
Your total profit, with this choice of permutation, is 12+4+14+6+5=41 . Note that 41 is not the maximum possible profit in this example.
† A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation ( 2 appears twice in the array) and [1,3,4] is also not a permutation ( n=3 , but 4 is present in the array).
输入格式
The first line of the input contains an integer t ( 1≤t≤104 ) — the number of test cases.
Then follow the descriptions of the test cases.
The first line of each test case description contains an integer n ( 2≤n≤105 ) — the number of animals.
The second line of the test case description contains n integers a1,a2,…,an ( 1≤ai≤n , ai=i ) — ai means the index of the animal that animal i is afraid of.
The third line of the test case description contains n integers c1,c2,…,cn ( 1≤ci≤109 ) — the costs of the animals.
It is guaranteed that the sum of n over all test cases does not exceed 105 .
输出格式
Output t lines, each containing the answer to the corresponding test case. The answer should be n integers — the permutation p1,p2,…,pn , indicating in which order to sell the animals in order to maximize the profit. If there are multiple possible answers, you can output any of them.
输入输出样例
输入#1
8 3 2 3 2 6 6 1 8 2 1 4 3 6 5 8 7 1 2 1 2 2 1 2 1 5 2 1 1 1 1 9 8 1 1 1 2 2 1 1000000000 999999999 7 2 3 2 6 4 4 3 1 2 3 4 5 6 7 5 3 4 4 1 3 3 4 5 6 7 3 2 1 1 1 2 2 4 2 1 4 1 1 1 1 1
输出#1
1 2 3 2 4 5 1 6 3 7 8 3 4 5 1 2 1 2 7 5 1 3 2 6 4 5 3 2 4 1 3 2 1 3 4 1 2