Logo
(追記) (追記ここまで)

31273번 - Slučajna Cesta 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 1024 MB1000.000%

문제

Vito lives in a city with $n$ parks labeled from 1ドル$ to $n$. The parks are connected with $n - 1$ roads such that there is a path between any two pairs of parks. Every park has some beauty value, beauty value of $i$-th park is $v_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 $i$-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 $n$ (2ドル ≤ n ≤ 10^6$), which denotes the number of parks.

In the second line there are $n - 1$ integers $p_i$ (1ドル ≤ p_i < i$), which denote a road between the $(i + 1)$-th park and $p_i$-th park.

In the third line there are $n$ integers $v_i$ (0ドル ≤ v_i ≤ 10^6$), where $v_i$ denotes the beauty of $i$-th park.

출력

If expected beauty of Vito’s journey which starts at $i$-th park is $\frac{a}{b}$ for integers $a$ and $b,ドル then in $i$-th line of output print $ab^{-1} \pmod {10^9 + 7}$ where $b^{-1}$ is modular inverse of $b \pmod {10^9 + 7}$.

제한

서브태스크

번호배점제한
110

$n ≤ 10$

230

$n ≤ 1000$

330

In sequence $p_i$ no value is present more than 2ドル$ times.

440

No additional constraints.

예제 입력 1

2
1
2 1

예제 출력 1

500000006
2

예제 입력 2

3
1 1
8 8 8

예제 출력 2

14
14
14

예제 입력 3

11
1 1 1 2 3 4 1 2 6 2
1 1000 5 3 18 200 8 9 0 2 2

예제 출력 3

968750272
610352580
450521029
536458466
199219275
662760680
190972315
90277951
824219264
941840425
532552597

힌트

Clarification of the first example:

The expected beauty of a journey starting at the first park is 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 2ドル$.

Clarification of the second example:

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

출처

Contest > Croatian Open Competition in Informatics > COCI 2023/2024 > Contest #3 5번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /