#P8581. [CoE R5] X 细胞

    ID: 9041 远端评测题 1000~3000ms 256~320MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学图论贪心洛谷原创O2优化洛谷月赛

[CoE R5] X 细胞

Problem Description

Brief statement

A rooted tree is a directed graph with a special vertex called the root. From the root, there is a unique directed path to every other vertex in the graph. By convention, vertices in a rooted tree are called nodes. A subtree is a subgraph of a rooted tree TT that is itself a rooted tree whose root is some node in TT. In a rooted tree, if there is an edge from node ii to node jj, then node ii is called the parent of node jj, and node jj is called a child of node ii. A node with no children is called a leaf node.

Given a rooted tree TT, the ii-th node has weights aiZ+a_i \in \mathbb{Z^+} and biZ+b_i \in \mathbb{Z^+}.

Let TT' be a subtree of TT, and let FiF_i be the set of all subtrees of TT rooted at node ii.

  • Define r(T)=a(T)b(T)r(T') = \frac {a(T')}{b(T')}, where a(T)=jTaja(T') = \sum \limits_{j \in T'}{a_j} and b(T)=jTbjb(T') = \sum \limits_{j \in T'}{b_j}.

  • Define TiT_i to be a subtree rooted at node ii such that r(Ti)=minTFir(T)r(T_i) = \min \limits_{T' \in F_i}r(T'), and among those, it has the maximum number of nodes.

  • Define S(T)S(T'): for a node jj in TT, let its parent be ii. Then jS(T)j \in S(T') if and only if iTi \in T' but jTj \notin T'.

Given a rooted tree TT with nn nodes, let the root be ii, and perform the following operations:

(11) Put the root node ii into a node set QQ, i.e., initially set Q{i}Q \leftarrow \{i\}.

(22) Choose any element in QQ and let it be jj. Determine TjT_j. For each node kS(Tj)k \in S(T_j), set akak+r(Tj)a_k \leftarrow a_k + \lceil r(T_j) \rceil.

(33) Remove jj from QQ, and set QQS(Tj)Q \leftarrow Q \cup S(T_j).

(44) If Q=Q = \varnothing, stop; otherwise go to step (22).

Each execution of step (22) determines a subtree of TT. Suppose step (22) is executed a total of mm times when the process ends. Let the subtree determined in the ii-th execution of step (22) be KiK_i. Minimize the following value WW:

$$W = 1 \times \lceil r(K_1) \rceil + 2 \times \lceil r(K_2) \rceil + \cdots + m \times \lceil r(K_m) \rceil = \sum_{i = 1}^{m}i \times \lceil r(K_i) \rceil$$

Z+\mathbb{Z^+} denotes the set of all positive integers, and x\lceil x \rceil denotes the smallest integer not less than xx.


Original statement

X\text{X} cells are an ancient life form from planet Gamma\text{Gamma} in the Andromeda galaxy.

Initially, there is only 11 X\text{X} cell, and X\text{X} cells can produce descendant X\text{X} cells by direct division. For an X\text{X} cell ii, if it produces a direct descendant X\text{X} cell jj, then cell ii is called the mother cell of cell jj, and cell jj is called a child cell of ii.

Note that the definitions of mother cell and child cell are not transitive. Suppose cell ii produces a direct descendant cell jj, and cell jj produces a direct descendant cell kk. Then jj is a child cell of ii, and kk is a child cell of jj, but kk is not a child cell of ii.

Each X\text{X} cell has a vitality value hxh_x and a volume vxv_x. For convenience, people define a mutation index for X\text{X} cells:

dx=hxvxd_x = \frac{h_x}{v_x}

This index measures how well an X\text{X} cell adapts to the environment. The lower the mutation index, the higher the probability that the cell survives.

It has been found that when an X\text{X} cell receives a certain specific external stimulus, it will activate and begin a process called assimilation to transform into a Z\text{Z} cell. Before assimilation starts, the activated X\text{X} cell changes its state into a Y\text{Y} cell. The Y\text{Y} cell continuously absorbs its child cells and fuses with them, making the child cell become part of the Y\text{Y} cell.

After fusion, the vitality value and volume of the Y\text{Y} cell become the sums of those before fusion. That is, suppose nn cells fuse into one Y\text{Y} cell, and these nn cells have vitality values and volumes h1h_1, h2h_2, …, hnh_n and v1v_1, v2v_2, …, vnv_n respectively. After fusion, the Y\text{Y} cell has vitality value hy=i=1nhih_y = \sum_{i=1}^{n}h_i, volume vy=i=1nviv_y = \sum_{i=1}^{n}v_i, and mutation index dy=hyvyd_y = \frac{h_y}{v_y}.

During assimilation, the Y\text{Y} cell follows these rules:

  • If a child cell jj has a mother cell ii that has not yet been assimilated, then the child cell jj will not be assimilated.
  • Make the mutation index of the Y\text{Y} cell as small as possible, and assimilate as many cells as possible.

When the Y\text{Y} cell can no longer assimilate more cells, it stops assimilating, transforms into a Z\text{Z} cell, and releases pheromones (the vitality value and volume do not change before and after the state change). The pheromone has the following effect: let the mutation index of the resulting Z\text{Z} cell be dz=hzvzd_z = \frac{h_z}{v_z}. If an unassimilated child cell jj has a mother cell ii that is assimilated by this Z\text{Z} cell, then the vitality value hjh_j of cell jj increases by dz\lceil d_z \rceil (x\lceil x \rceil denotes the smallest integer not less than xx).

Note that when the assimilation process ends, the mutation index of the Y\text{Y} cell is required to be as small as possible, but during the assimilation process, the mutation index of the Y\text{Y} cell does not need to remain minimal at all times (see input #1\#1).

Researchers need to use a special device to produce the specific external stimulus that activates X\text{X} cells. Each use of the device consumes a certain amount of activator. The amount of activator consumed ctc_t is computed by:

ct=t×dzc_t = t \times \lceil d_z \rceil

where tt is the usage index of the device (starting from 11), and dzd_z is the mutation index of the Z\text{Z} cell finally produced by this activation.

Because mother cells secrete pheromones that prevent child cells from being activated, one can only choose an X\text{X} cell that has no mother cell, or whose mother cell has already been assimilated, as the target of the specific external stimulus, so that it activates and begins assimilation.

Given the relationships among all X\text{X} cells and their vitality values and volumes, and since activators are very hard to produce, you now need to design an optimal activation order plan for X\text{X} cells so that all X\text{X} cells transform into Z\text{Z} cells while the total activator consumption is minimized.

You only need to output this minimum value.

Input Format

The first line contains an integer nn, representing the number of X\text{X} cells.

The next line contains n1n - 1 integers, in order representing the mother cell index of X\text{X} cells numbered 2n2 \sim n.

The next line contains nn integers, in order representing the vitality value hih_i of the X\text{X} cell numbered ii.

The next line contains nn integers, in order representing the volume viv_i of the X\text{X} cell numbered ii.

The initial X\text{X} cell is numbered 11.


Input format corresponding to the brief statement:

The first line contains an integer nn, representing the number of nodes in the rooted tree.

The next line contains n1n - 1 integers, in order representing the parent index of nodes numbered 2n2 \sim n.

The next line contains nn integers, in order representing the weight aia_i of the node numbered ii.

The next line contains nn integers, in order representing the weight bib_i of the node numbered ii.

The root node is numbered 11.

Output Format

Output one integer, representing the minimum total activator consumption needed to make all X\text{X} cells transform into Z\text{Z} cells.


Output format corresponding to the brief statement:

Output one integer, representing the minimum value of WW.

3
1 2
5 7 12
1 1 10
2
3
1 1
2 2 12
2 3 4
9
5
1 2 2 1
1 7 10 20 9
1 1 1 1 1
223
12
1 2 3 1 5 6 7 1 9 10 11
4 10 2 1 50 1 20 9 40 2 15 10
1 1 1 1 1 1 1 1 1 1 1 1
124

Hint

Sample explanation

Input #1\#1:

  • Activate cell 11, assimilate cells 22 and 33. The resulting Z\text{Z} cell has vitality value hz=24h_z = 24, volume vz=12v_z = 12, and mutation index dz=hzvz=2412=2d_z = \frac {h_z}{v_z} = \frac {24}{12} = 2.

  • The minimum activator consumption for the 11-st activation is $c_1 = t \times \lceil d_z \rceil = 1 \times \lceil \frac {24}{12} \rceil = 1 \times 2 = 2$.

  • Initially, the Y\text{Y} cell’s mutation index is 55. After assimilating cell 22, the mutation index is 66. After assimilating cell 33, the mutation index becomes 22. This shows that during assimilation, the Y\text{Y} cell’s mutation index does not need to stay minimal at all times; it only needs to be minimal when it finally stops assimilating and transforms into a Z\text{Z} cell.

Input #2\#2:

  • Activate cell 11, assimilate cell 22. The resulting Z\text{Z} cell has vitality value hz=2+2=4h_z = 2 + 2 = 4, volume vz=2+3=5v_z = 2 + 3 = 5, mutation index dz=hzvz=45d_z = \frac {h_z}{v_z} = \frac {4}{5}. The activator consumption for this activation is $c_1 = t \times \lceil d_z \rceil= 1 \times \lceil \frac {4}{5} \rceil = 1 \times 1 = 1$. This Z\text{Z} cell releases pheromones that increase the vitality value of cell 33 by 11, so the vitality value of cell 33 becomes 1313.

  • Activate cell 33, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=13h_z = 13, volume vz=4v_z = 4, mutation index dz=hzvz=134d_z = \frac {h_z}{v_z} = \frac {13}{4}. The activator consumption for this activation is $c_2 = t \times \lceil d_z \rceil = 2 \times \lceil \frac {13}{4} \rceil= 2 \times 4 = 8$.

  • The minimum total activator consumption for 22 activations is c1+c2=1+8=9c_1 + c_2 = 1 + 8 = 9.

Input #3\#3:

  • Activate cell 11, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=1h_z = 1, volume vz=1v_z = 1, mutation index dz=hzvz=11=1d_z = \frac {h_z}{v_z} = \frac {1}{1} = 1. The total activator consumption is $c_1 = t \times \lceil d_z \rceil = 1 \times \lceil 1 \rceil = 1$. The Z\text{Z} cell releases pheromones, increasing the vitality values of cells 22 and 55 by 11 each, so their current vitality values are 88 and 1010.

  • Activate cell 22, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=8h_z = 8, volume vz=1v_z = 1, mutation index dz=hzvz=81=8d_z = \frac {h_z}{v_z} = \frac {8}{1} = 8. The total activator consumption is $c_2 = t \times \lceil d_z \rceil = 2 \times \lceil 8 \rceil = 16$. The Z\text{Z} cell releases pheromones, increasing the vitality values of cells 33 and 44 by 88 each, so their current vitality values are 1818 and 2828.

  • Activate cell 44, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=28h_z = 28, volume vz=1v_z = 1, mutation index dz=hzvz=281=28d_z = \frac {h_z}{v_z} = \frac {28}{1} = 28. The total activator consumption is $c_3 = t \times \lceil d_z \rceil = 3 \times \lceil 28 \rceil = 84$.

  • Activate cell 33, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=18h_z = 18, volume vz=1v_z = 1, mutation index dz=hzvz=181=18d_z = \frac {h_z}{v_z} = \frac {18}{1} = 18. The total activator consumption is $c_4 = t \times \lceil d_z \rceil = 4 \times \lceil 18 \rceil = 72$.

  • Activate cell 55, assimilate no other cells. The resulting Z\text{Z} cell has vitality value hz=10h_z = 10, volume vz=1v_z = 1, mutation index dz=hzvz=101=10d_z = \frac {h_z}{v_z} = \frac {10}{1} = 10. The total activator consumption is $c_5 = t \times \lceil d_z \rceil = 5 \times \lceil 10 \rceil = 50$.

  • The minimum total activator consumption for 55 activations is $c_1 + c_2 + c_3 + c_4 + c_5 = 1 + 16 + 84 + 72 + 50 = 223$.

Input #4\#4:

  • Activate cell 11, assimilate no other cells. The resulting Z\text{Z} cell has mutation index dz=hzvz=41d_z = \frac {h_z}{v_z} = \frac {4}{1}. It releases pheromones that increase the vitality values of cells 22, 55, and 99 by 44. The activator consumption is $c_1 = 1 \times \lceil \frac {4}{1} \rceil = 1 \times 4 = 4$.

  • Activate cell 55, assimilate cells 66, 77, and 88. The resulting Z\text{Z} cell has mutation index dz=hzvz=844d_z = \frac {h_z}{v_z} = \frac {84}{4}. The activator consumption is $c_2 = 2 \times \lceil \frac {84}{4} \rceil = 2 \times 21 = 42$.

  • Activate cell 99, assimilate cells 1010, 1111, and 1212. The resulting Z\text{Z} cell has mutation index dz=hzvz=714d_z = \frac {h_z}{v_z} = \frac {71}{4}. The activator consumption is $c_3 = 3 \times \lceil \frac {71}{4} \rceil = 3 \times 18 = 54$.

  • Activate cell 22, assimilate cells 33 and 44. The resulting Z\text{Z} cell has mutation index dz=hzvz=173d_z = \frac {h_z}{v_z} = \frac {17}{3}. The activator consumption is $c_4 = 4 \times \lceil \frac {17}{3} \rceil = 4 \times 6 = 24$.

  • The minimum total activator consumption for 44 activations is c1+c2+c3+c4=4+42+54+24=124c_1 + c_2 + c_3 + c_4 = 4 + 42 + 54 + 24 = 124.


Constraints

This problem uses bundled testdata. Some subtask input files are large, so please use an appropriate input method.

Subtask Points nn \leq Special Property Time Limit Memory Limit Dependencies
11 1010 2020 1 s1 \text{ s} 256 MB256 \text{ MB}
22 3030 10310^3 11
33 1010 10510^5 121 \sim 2
44 10610^6 A\text{A} 3 s3 \text{ s}
55 2020 B\text{B} 1 s1 \text{ s} 320 MB320 \text{ MB}
66 1010 C\text{C} 256 MB256 \text{ MB}
77 3 s3 \text{ s} 320 MB320 \text{ MB} 161 \sim 6
  • Special property A\text{A}: the rooted tree corresponds to a star graph.  2in\forall \ 2 \leq i \leq n, fi=1f_i = 1.
  • Special property B\text{B}: the rooted tree corresponds to a directed chain.  2in\forall \ 2 \leq i \leq n, fi=i1f_i = i - 1.
  • Special property C\text{C}: the data is randomly generated.  2in\forall \ 2 \leq i \leq n, fif_i is a random integer chosen from [1,i1][1, i - 1]. hi,vih_i, v_i are random integers chosen from [1,106][1, 10^6].

For 100%100\% of the data, 1n1061 \leq n \leq 10^6, 1hi1061 \leq h_i \leq 10^6, 1vi1061 \leq v_i \leq 10^6, and the answer does not exceed 101810^{18}.

Translated by ChatGPT 5