目录

CF1388D-Captain Flint and Treasure

CF1388D-Captain Flint and Treasure

题目:

题目描述:

Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure’s location or may not. That’s why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this…

There are two arrays $ a $ and $ b $ of length $ n $ . Initially, an $ ans $ is equal to $ 0 $ and the following operation is defined:

  1. Choose position $ i $ ( $ 1 \le i \le n $ );
  2. Add $ a_i $ to $ ans $ ;
  3. If $ b_i \neq -1 $ then add $ a_i $ to $ a_{b_i} $ .

What is the maximum $ ans $ you can get by performing the operation on each $ i $ ( $ 1 \le i \le n $ ) exactly once?

Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

输入格式:

The first line contains the integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of arrays $ a $ and $ b $ .

The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ −10^6 \le a_i \le 10^6 $ ).

The third line contains $ n $ integers $ b_1, b_2, \ldots, b_n $ ( $ 1 \le b_i \le n $ or $ b_i = -1 $ ).

Additional constraint: it’s guaranteed that for any $ i $ ( $ 1 \le i \le n $ ) the sequence $ b_i, b_{b_i}, b_{b_{b_i}}, \ldots $ is not cyclic, in other words it will always end with $ -1 $ .

输出格式:

In the first line, print the maximum $ ans $ you can get.

In the second line, print the order of operations: $ n $ different integers $ p_1, p_2, \ldots, p_n $ ( $ 1 \le p_i \le n $ ). The $ p_i $ is the position which should be chosen at the $ i $ -th step. If there are multiple orders, print any of them.

样例:

样例输入 1:

1
2
3
3
1 2 3
2 3 -1

样例输出 1:

1
2
10
1 2 3

样例输入 2:

1
2
3
2
-1 100
2 -1

样例输出 2:

1
2
99
2 1

样例输入 3:

1
2
3
10
-10 -1 2 2 5 -2 -3 -4 2 -6
-1 -1 2 2 -1 5 5 7 7 9

样例输出 3:

1
2
-9
3 5 6 1 9 4 10 7 8 2

思路:

实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "ybwhead/ios.h"
const int maxn = 2e5 + 10;
long long a[maxn];
int n;
int in[maxn];
vector<int> c, d;
int b[maxn];
int main()
{
    yin >> n;
    for (int i = 1; i <= n; i++)
        yin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        yin >> b[i];
        if (b[i] == -1)
            continue;
        ++in[b[i]];
    }
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (in[i] == 0)
            q.push(i);
    }
    long long sum = 0;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        int w = b[v];
        sum += a[v];
        if (a[v] > 0)
        {
            if (w > 0)
            {
                a[w] += a[v];
            }
            c.push_back(v);
        }
        else
            d.push_back(v);
        if (w > 0)
        {
            --in[w];
            if (in[w] == 0)
                q.push(w);
        }
    }
    yout << sum << endl;
    for (int i = 0; i < c.size(); i++)
        yout << c[i] << ' ';
    for (int i = d.size() - 1; i >= 0; i--)
        yout << d[i] << " ";
    yout << endl;
    return 0;
}