QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 2048 MB

#22841. Maximum Diameter

Statistics

Let $X = (x_1, x_2, \ldots, x_{n-1})$ be any integer sequence of length $n - 1$ whose elements are between $1$ and $n$, inclusive. Consider all trees satisfying the following condition. At least one such tree always exists. Define $f(X)$ as the maximum diameter among these trees.

  • The tree consists of $n$ nodes numbered $1$ through $n$. Edges are numbered $1$ through $n - 1$. For each $i$ ($1 \le i \le n - 1$), one of the endpoints of edge $i$ is $x_i$.

Here, the diameter of a tree is the maximum number of edges on a path between two nodes.

You are given two integer sequences, $A = (a_1, a_2, \ldots, a_{n-1})$ and $B = (b_1, b_2, \ldots, b_{n-1})$. Each element in $A$ is between $1$ and $n$, inclusive. Each element in $B$ is positive. You can assume that the pairs $(a_1, b_1), (a_2, b_2), \ldots, (a_{n-1}, b_{n-1})$ are distinct.

For each $d = 2, 3, \ldots, n - 1$, solve the following problem:

Choose an integer sequence $X = (x_1, x_2, \ldots, x_{n-1})$ whose elements are between $1$ and $n$, inclusive, and that satisfies $f(X) = d$. Such a sequence always exists. For each $i$ ($1 \le i \le n - 1$), you pay a cost of $2^{b_i}$ if and only if $x_i \ne a_i$. Find the minimum possible total cost.

Because each answer may be large, output it modulo $998\,244\,353$.

Input

The input consists of a single test case of the following format.

$$ \begin{aligned} &n \\ &a_1 \ a_2 \ \cdots \ a_{n-1} \\ &b_1 \ b_2 \ \cdots \ b_{n-1} \end{aligned} $$

The first line contains an integer $n$ ($3 \le n \le 2 \times 10^5$). The second line contains $n - 1$ integers in the sequence $A$ ($1 \le a_i \le n$). The third line contains $n - 1$ integers in the sequence $B$ ($1 \le b_i \le 10^9$). It is guaranteed that $(a_i, b_i) \ne (a_j, b_j)$ if $i \ne j$.

Output

Output $n - 2$ lines. The $k$-th line should contain the answer for $d = k + 1$, i.e., the minimal possible total cost for a sequence $X$ such that $f(X) = k + 1$, modulo $998\,244\,353$.

Examples

Input 1

5
2 4 3 4
1 2 4 8

Output 1

18
2
0

Input 2

10
6 2 1 6 10 4 6 10 4
1 1 7 8 1 5 5 5 10

Output 2

454
196
68
36
4
2
0
2

Input 3

9
1 1 1 1 1 1 1 1
24 27 30 33 36 39 42 45

Output 3

0
16777216
150994944
226492415
830472183
671088562
394263947

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#2902EditorialOpenNew Editorial for Problem #22841louyuxuanView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.