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

19363번 - Lowest Common Ancestor 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB159382517.857%

문제

Bobo has a rooted tree with $n$ nodes which are conveniently labeled with 1,ドル 2, \dots, n$. Node 1ドル$ is the root, and the $i$-th node has weight $w_i$.

He would like to find out $f(2), f(3), \dots, f(n)$ where

$$f(i) = \sum_{j = 1}^{i - 1} w_{\mathrm{LCA}(i, j)}\text{.}$$

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains an integer $n$ (2ドル \leq n \leq 2 \cdot 10^5$).

The second line contains $n$ integers $w_1, w_2, \dots, w_n$ (1ドル \leq w_i \leq 10^4$).

The third line contains $(n - 1)$ integers $p_2, p_3, \dots, p_n,ドル where $p_i$ denotes an edge from the $p_i$-th node to the $i$-th node (1ドル \leq p_i \leq n$). The edges form a tree.

It is guaranteed that the sum of $n$ does not exceed 2ドル \cdot 10^5$.

출력

For each test case, output $(n - 1)$ integers: $f(2), f(3), \dots, f(n)$.

제한

예제 입력 1

3
1 2 3
1 1
5
1 2 3 4 5
1 2 2 1

예제 출력 1

1
2
1
3
5
4

힌트

출처

Camp > Petrozavodsk Programming Camp > Winter 2017 > Day 2: Xiaoxu Guo Contest E번

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

출처

대학교 대회

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

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