#P8966. 觅光 | Searching for Hope (easy ver.)

    ID: 10033 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>博弈论洛谷原创O2优化洛谷月赛

觅光 | Searching for Hope (easy ver.)

Background

This is the easy version of the problem. The only difference between the two versions in the 100%\bm{100 \%} testdata constraints is the limit on n\bm{n}. In this version, n1000\bm{n \le 1000}.


There are places longed for in dreams, and there are distant places in reality that can be seen but never reached.

We are waiting for countless hopes, a new era; life has not yet played its final movement.

In an instant, everything is overturned. The sky falls into the sea, choking every last breath of those who breathe. Wings are wrapped in freezing seawater, and the sorrow is so deep that one forgets the meaning of breathing.

Clearly separated from the air by only a hair’s breadth, yet no longer wanting to try to breathe again. I begin to understand: when sorrow reaches its extreme, perhaps there are no tears.

The gods, in the name of life, fabricate a dim truth.

Tears blur the eyes. The body falls into the sky. The pale daylight has no choice but to light up the hope of this day.

Problem Description

There is now a rooted binary tree with nn nodes.

Mortals and gods play a game on this tree. The mortal needs to drop several balls from the root. Each ball carries either 11 unit of positive charge or 11 unit of negative charge.

Each node on the tree has a capacity. Node ii can hold cic_i balls. Initially, the number of balls in every node is 00. We say a node is full if and only if the number of balls it holds equals its capacity.

Each time a ball falls onto a node:

  • If the node has no children, or all of its children are already full, then it stops: the number of balls held by this node increases by 11.
  • If exactly one child is not full, then the ball falls to that child.
  • If both children are not full:
    • If the algebraic sum of charges of all balls in the left subtree is greater than that in the right subtree, then if the current ball is positively charged it falls to the right, otherwise it falls to the left.
    • If the algebraic sum of charges of all balls in the left subtree is less than that in the right subtree, then if the current ball is positively charged it falls to the left, otherwise it falls to the right.
    • If the algebraic sums are equal, then the god decides which direction it falls.

Here, the algebraic sum of charges means (number of positive charges) minus (number of negative charges).

Before the game starts, both sides agree on a target node uu. In one round, the mortal chooses the charge of the ball dropped this time, and the god controls the falling process according to the rules above. When uu becomes full, the game ends.

The mortal wants the number of rounds to be as small as possible, while the god wants it to be as large as possible. Assume both sides are smart enough.

For all 1un1\leq u\leq n, compute the number of rounds rur_u.

Input Format

The first line contains a positive integer nn.

The second line contains n1n-1 integers f2,f3,,fnf_2, f_3, \ldots, f_n, where fif_i denotes the index of the parent of node ii.

The third line contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n.

Output Format

Output one line containing nn integers r1,r2,,rnr_1, r_2, \ldots, r_n.

5
1 1 2 2
1 1 1 1 1

5 4 2 3 3

Hint

Constraints

This problem uses bundled tests.

Subtask ID nn \le Special Property Score
1 10001000 A 11
2 1010 B 27
3 10001000 62
  • Special Property A: The tree degenerates into a chain with 11 as one endpoint.
  • Special Property B: ci=1c_i = 1.

For 100%100\% of the data, 2n10002 \le n \le 1000. The tree is a binary tree rooted at 11, 1fi<i1 \le f_i < i, and 1ci10121 \le c_i \le {10}^{12}.

Translated by ChatGPT 5