CF1872F.Selling a Menagerie

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are the owner of a menagerie consisting of nn animals numbered from 11 to nn. 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 ii is afraid of animal aia_i (ai≠ia_i \neq i). Also, the cost of each animal is known, for animal ii it is equal to cic_i.

You will sell all your animals in some fixed order. Formally, you will need to choose some permutation†^\dagger p1,p2,…,pnp_1, p_2, \ldots, p_n, and sell animal p1p_1 first, then animal p2p_2, and so on, selling animal pnp_n last.

When you sell animal ii, there are two possible outcomes:

  • If animal aia_i was sold before animal ii, you receive cic_i money for selling animal ii.
  • If animal aia_i was not sold before animal ii, you receive 2⋅ci2 \cdot c_i money for selling animal ii. (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]a = [3, 4, 4, 1, 3], c=[3,4,5,6,7]c = [3, 4, 5, 6, 7], and the permutation you choose is [4,2,5,1,3][4, 2, 5, 1, 3], then:

  • The first animal to be sold is animal 44. Animal a4=1a_4 = 1 was not sold before, so you receive 2⋅c4=122 \cdot c_4 = 12 money for selling it.
  • The second animal to be sold is animal 22. Animal a2=4a_2 = 4 was sold before, so you receive c2=4c_2 = 4 money for selling it.
  • The third animal to be sold is animal 55. Animal a5=3a_5 = 3 was not sold before, so you receive 2⋅c5=142 \cdot c_5 = 14 money for selling it.
  • The fourth animal to be sold is animal 11. Animal a1=3a_1 = 3 was not sold before, so you receive 2⋅c1=62 \cdot c_1 = 6 money for selling it.
  • The fifth animal to be sold is animal 33. Animal a3=4a_3 = 4 was sold before, so you receive c3=5c_3 = 5 money for selling it.

Your total profit, with this choice of permutation, is 12+4+14+6+5=4112 + 4 + 14 + 6 + 5 = 41. Note that 4141 is not the maximum possible profit in this example.

†^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in any order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3, but 44 is present in the array).

你是一家动物园的主人,园中饲养着编号为 11 到 nn 的 nn 只动物。然而,维持动物园开销巨大,因此你决定将其全部出售!

已知每只动物恰好害怕另一只动物。更准确地说,动物 ii 害怕动物 aia_i(其中 ai≠ia_i \neq i)。此外,每只动物的售价也已知:动物 ii 的售价为 cic_i。

你将以某种固定顺序出售所有动物。形式上,你需要选择某个排列†^\dagger p1,p2,…,pnp_1, p_2, \ldots, p_n,首先出售动物 p1p_1,然后出售动物 p2p_2,依此类推,最后出售动物 pnp_n。

当你出售动物 ii 时,会出现以下两种情形之一:

  • 若动物 aia_i 在动物 ii 之前已被售出,则你从出售动物 ii 中获得 cic_i 的收入;
  • 若动物 aia_i 在动物 ii 之前尚未被售出,则你从出售动物 ii 中获得 2⋅ci2 \cdot c_i 的收入。(令人惊讶的是,当前仍处于恐惧状态的动物反而更具价值。)

你的任务是选择动物的出售顺序,以使总收益最大化。

例如,若 a=[3,4,4,1,3]a = [3, 4, 4, 1, 3],c=[3,4,5,6,7]c = [3, 4, 5, 6, 7],且你选择的排列为 [4,2,5,1,3][4, 2, 5, 1, 3],则:

  • 第一只被出售的动物是动物 44。由于动物 a4=1a_4 = 1 尚未被售出,你获得 2⋅c4=122 \cdot c_4 = 12 的收入;
  • 第二只被出售的动物是动物 22。由于动物 a2=4a_2 = 4 已被售出,你获得 c2=4c_2 = 4 的收入;
  • 第三只被出售的动物是动物 55。由于动物 a5=3a_5 = 3 尚未被售出,你获得 2⋅c5=142 \cdot c_5 = 14 的收入;
  • 第四只被出售的动物是动物 11。由于动物 a1=3a_1 = 3 尚未被售出,你获得 2⋅c1=62 \cdot c_1 = 6 的收入;
  • 第五只被出售的动物是动物 33。由于动物 a3=4a_3 = 4 已被售出,你获得 c3=5c_3 = 5 的收入。

按此排列所得的总收益为 12+4+14+6+5=4112 + 4 + 14 + 6 + 5 = 41。注意,在本例中,4141 并非可能的最大收益。

†^\dagger 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Then follow the descriptions of the test cases.

The first line of each test case description contains an integer nn (2≤n≤1052 \le n \le 10^5) — the number of animals.

The second line of the test case description contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n, ai≠ia_i \neq i) — aia_i means the index of the animal that animal ii is afraid of.

The third line of the test case description contains nn integers c1,c2,…,cnc_1, c_2, \dots, c_n (1≤ci≤1091 \le c_i \le 10^9) — the costs of the animals.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

接下来是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 表示动物的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n,且 ai≠ia_i \neq i)—— 其中 aia_i 表示第 ii 只动物所害怕的动物的编号。

每个测试用例的第三行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(1≤ci≤1091 \le c_i \le 10^9)—— 表示各只动物的成本。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

Output tt lines, each containing the answer to the corresponding test case. The answer should be nn integers — the permutation p1,p2,…,pnp_1, p_2, \ldots, p_n, 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.

输出 tt 行,每行包含对应测试用例的答案。答案应为 nn 个整数——排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,表示为使利润最大化而出售动物的顺序。若存在多个可能的答案,输出任意一个即可。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页