#P10894. Counting Good Subtrees After Pruning

    ID: 12939 Type: Default 1000ms 256MiB

Counting Good Subtrees After Pruning

Counting Good Subtrees After Pruning

jq sister has a rooted tree with root node \(1\). We call a nonempty subset \(S\) of nodes good if it satisfies that for any two nodes \(i,j\in S\), their lowest common ancestor (denoted by \(\mathrm{LCA}(i,j)\)) is also in \(S\). For more details on \(\mathrm{LCA}\), see LCA.

To make the tree more "cute", she plans to prune one of its subtrees. Pruning a subtree rooted at some node \(i\) means deleting node \(i\) together with every node in its subtree (and all edges incident to these nodes) from the original tree.

She has several pruning plans but has never executed any. Your task is to report, for each plan, the number of good nonempty subsets in the remaining tree, modulo \(998244353\).

Note: A subset \(S\) is good if for any \(i,j\in S\), we have \(\mathrm{LCA}(i,j)\in S\). It turns out that the good subsets in a tree are exactly the connected induced subtrees. In a rooted tree \(T\) (with root \(1\)), every connected subtree has a unique vertex that is closest to the root. Moreover, if we define for each node \(v\) a value

[ f(v)=\prod_{u\in \text{child}(v)} (1+f(u)), ]

then the number of connected subtrees (i.e. good subsets) in \(T\) is \(\sum_{v\in T} f(v)\). When pruning a subtree, we simply remove the corresponding branch from the tree. If the pruned node is the root \(1\), then the tree becomes empty and the answer is \(0\).

inputFormat

The first line contains two integers \(n\) and \(q\) \((1\leq n, q\leq 2\cdot10^5)\) representing the number of nodes in the tree and the number of queries, respectively.

The second line contains \(n-1\) integers. For \(i=2,3,\ldots,n\), the \(i\)th node's parent is given. It is guaranteed that the tree is rooted at node \(1\).

Each of the next \(q\) lines contains a single integer \(i\) \((1\le i\le n)\) indicating that the subtree rooted at node \(i\) is pruned from the tree.

outputFormat

For each query, output one integer: the number of good nonempty subsets (i.e. connected subtrees) in the remaining tree modulo \(998244353\). If the remaining tree is empty, output \(0\).

sample

5 3
1 1 2 2
2
3
1
3

10 0

</p>