Slučajna Cesta

시간 제한3초메모리 제한1024 MB

요약
각 간선이 독립적으로 파란 뱀 또는 빨간 뱀을 가질 때, 모든 시작 정점에 대해 더 갈 수 있는 안전한 간선이 없어질 때까지 방문한 정점 가치 합의 기댓값을 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 동적 계획법, 확률
정답자
아직 제출이 없습니다

문제

Vito lives in a city with nn parks labeled from 11 to nn. The parks are connected with n−1n - 1 roads such that there is a path between any two pairs of parks. Every park has some beauty value, beauty value of ii-th park is v_iv\_i.

Last night Vito decided to wander around the city in such a way that after he visits a park he chooses a random road with equal probability and visits a park to which that road leads. But before he started his journey he looked through the window of his skyscraper and saw that on every road there is either a blue or a red snake. Blue snakes attack all people traveling from the park with a lower label to a park with a higher one, a red snakes attack everyone traveling from a park with higher label to lower. As Vito doesn’t want to get attacked by a snake he decided to change his plans by considering only roads on which he will not get attacked by a snake when choosing a random road. Since he likes long walks he will not stop on his journey until there is at least one road he can safely pass.

And while Vito walks down the stairs of his skyscraper he completely forgot on which road is red or blue snake so he wonders: If on every road there is an equal probability of a blue or a red snake, what is the expected beauty of my journey which starts in the ii-th park?

Beauty of path is the sum of beauties of parks visited on that journey. Expected beauty of journey is defined as the sum of product of beauty of a path and probability Vito takes that path, for every possible path.

입력

In the first line there is an integer nn (2≤n≤1062 ≤ n ≤ 10^6), which denotes the number of parks.

In the second line there are n−1n - 1 integers p_ip\_i (1≤p_i<i1 ≤ p\_i < i), which denote a road between the (i+1)(i + 1)-th park and p_ip\_i-th park.

In the third line there are nn integers v_iv\_i (0≤v_i≤1060 ≤ v\_i ≤ 10^6), where v_iv\_i denotes the beauty of ii-th park.

출력

If expected beauty of Vito’s journey which starts at ii-th park is ab\frac{a}{b} for integers aa and bb, then in ii-th line of output print ab−1(mod109+7)ab^{-1} \pmod {10^9 + 7} where b−1b^{-1} is modular inverse of b(mod109+7)b \pmod {10^9 + 7}.

힌트

Clarification of the first example:

The expected beauty of a journey starting at the first park is 2.5(mod109+7)=52(mod109+7)=5⋅2−1(mod109+7)=5⋅500000004(mod109+7)=500000006(mod109+7)2.5 \pmod {10^9 + 7} = \frac{5}{2} \pmod {10^9 + 7} = 5 \cdot 2 ^{-1} \pmod {10^9 + 7} = 5 \cdot 500000004 \pmod {10^9 + 7} = 500000006 \pmod {10^9 + 7} and starting from the second park it is 22.

Clarification of the second example:

Probability that both snakes are red is 14\frac{1}{4} and in that case if Vito starts at the first park he randomly chooses which road he will take.

예제3

  1. 예제 1

    입력
    2
    1
    2 1
    
    예상 출력
    500000006
    2
    
  2. 예제 2

    입력
    3
    1 1
    8 8 8
    
    예상 출력
    14
    14
    14
    
  3. 예제 3

    입력
    11
    1 1 1 2 3 4 1 2 6 2
    1 1000 5 3 18 200 8 9 0 2 2
    
    예상 출력
    968750272
    610352580
    450521029
    536458466
    199219275
    662760680
    190972315
    90277951
    824219264
    941840425
    532552597