CF1436D.Bandit in a City

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Bandits appeared in the city! One of them is trying to catch as many citizens as he can.

The city consists of nn squares connected by n−1n-1 roads in such a way that it is possible to reach any square from any other square. The square number 11 is the main square.

After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.

At the moment when the bandit appeared on the main square there were aia_i citizens on the ii-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.

The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?

强盗出现在了这座城市!其中一名强盗正试图抓捕尽可能多的市民。

这座城市由 nn 个广场组成,这些广场通过 n−1n-1 条道路连接,使得任意两个广场之间均可相互到达。编号为 11 的广场是主广场。

在周日散步之后,所有道路均被改为单向道路,且满足从主广场出发可以到达任意一个广场。

当强盗出现在主广场时,第 ii 个广场上有 aia_i 名市民。此时,以下过程将开始:首先,每位当前位于拥有若干条向外单向道路的广场上的市民,会选择其中一条道路并沿其移动到另一个广场;接着,强盗选择自己所在广场的一条向外单向道路,并沿其移动。该过程不断重复,直到强盗所处的广场不再有任何向外的单向道路为止。此时,强盗将抓捕该广场上的所有市民。

强盗希望抓捕尽可能多的市民;而市民则希望最小化被捕人数。强盗与市民均能实时知晓所有市民的位置,且市民可以相互协作。若双方均采取最优策略,最终将有多少名市民被抓获?

输入格式

The first line contains a single integer nn — the number of squares in the city (2≤n≤2⋅1052 \le n \le 2\cdot10^5).

The second line contains n−1n-1 integers p2,p3…pnp_2, p_3 \dots p_n meaning that there is a one-way road from the square pip_i to the square ii (1≤pi<i1 \le p_i \lt i).

The third line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n — the number of citizens on each square initially (0≤ai≤1090 \le a_i \le 10^9).

第一行包含一个整数 nn —— 城市中广场的数量(2≤n≤2⋅1052 \le n \le 2\cdot10^5)。

第二行包含 n−1n-1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n,表示存在一条从广场 pip_i 到广场 ii 的单向道路(1≤pi<i1 \le p_i \lt i)。

第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n —— 每个广场上初始的市民数量(0≤ai≤1090 \le a_i \le 10^9)。

输出格式

Print a single integer — the number of citizens the bandit will catch if both sides act optimally.

输出一个整数——如果双方都采取最优策略,强盗将抓住的市民人数。

输入输出样例

  • 输入#1

    3
    1 1
    3 1 2

    输出#1

    3
  • 输入#2

    3
    1 1
    3 1 3

    输出#2

    4

说明/提示

In the first example the citizens on the square 11 can split into two groups 2+12 + 1, so that the second and on the third squares will have 33 citizens each.

In the second example no matter how citizens act the bandit can catch at least 44 citizens.

在第一个例子中,位于方格 11 的市民可以分为两组:2+12 + 1,从而使第二个和第三个方格各自拥有 33 名市民。

在第二个例子中,无论市民如何行动,强盗至少能抓住 44 名市民。

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

首页