#P8968. 觅光 | Searching for Hope (hard ver.)

    ID: 10035 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>暴力数据结构博弈论洛谷原创O2优化洛谷月赛

觅光 | Searching for Hope (hard ver.)

Background

This is the hard version of the problem. The only difference between the two versions under the 100%\bm{100 \%} testdata constraints is the limit on n\bm{n}. In this version, n106\bm{n \le {10}^6}.


There are places we long for in dreams, and there are distant places in reality that we can see but can never reach.

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

In an instant, everything in the upheaval is overturned. The sky falls into the sea, choking every breath of those who live. Wings are wrapped in freezing seawater, sorrowful enough to make one forget the meaning of breathing from that moment on.

Only a hair’s breadth away from the air, yet I no longer want to try to breathe. I begin to understand: when sorrow reaches the extreme, maybe there will be no tears.

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

Tears blur my eyes; my 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 some 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, every node holds 00 balls. 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 this node has no child nodes, or all child nodes are already full, then it stops, and the number of balls held by this node increases by 11;
  • If this node has exactly one child node that is not full, then the ball falls to that child;
  • If both child nodes are not full:
    • If the algebraic sum of charges of all balls in the left subtree is greater than the algebraic sum of charges of all balls 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 the algebraic sum of charges of all balls 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 sum of charges of all balls in the left subtree equals the algebraic sum of charges of all balls in the right subtree, then the gods decide which direction it falls.

Here, the algebraic sum of charges means the number of positively charged balls minus the number of negatively charged balls.

Before the game starts, both sides agree on a target node uu. In one round, the mortal chooses the polarity of the ball dropped in this round, and the gods control 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, and the gods want the number of rounds to be as large as possible. Assume both sides are smart enough.

For all 1un1\leq u\leq n, find 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 is 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 with 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 Score
4 105{10}^5 33
5 106{10}^6 67

For 100%100\% of the testdata, 2n1062 \le n \le {10}^6. The tree is a rooted binary tree with root 11, and it satisfies 1fi<i1 \le f_i < i and 1ci10121 \le c_i \le {10}^{12}.


【Hint】

The maximum I/O size of this problem reaches 20 MiB. Please pay attention to I/O efficiency.

Translated by ChatGPT 5