#P10894. 虚树

虚树

Background

First, you do not need to know anything about virtual trees to solve this problem.

—Even though this is the first problem in the theme set whose name contains “virtual tree” (

Problem Description

Sister jq has a tree rooted at node 11. Define that a non-empty subset ss of the node set SS of the tree is good if and only if it satisfies i,js,LCA(i,j)s\forall i,j \in s, \text{LCA}(i,j) \in s. What is LCA\text{LCA}?

To make the shape of the tree cuter, Sister jq plans to prune (delete) a subtree rooted at some node ii (that is, in the original tree, delete node ii and all nodes in its subtree, and also delete all edges connected to them).

She has several pruning plans but has never carried them out. You need to compute, after carrying out each plan, the number of good non-empty subsets of this tree. Since the answer may be very large, output it modulo 998244353998244353.

Input Format

The first line contains an integer nn (n5×105n \leq 5 \times 10^5), which is the number of nodes in the tree.

Lines 22 to nn each contain two integers ui,viu_i, v_i, representing an edge connecting uiu_i and viv_i.

Line n+1n+1 contains an integer mm (m5×105m \leq 5 \times 10^5), which is the number of pruning plans.

The next line contains mm integers qiq_i, meaning that the subtree rooted at qiq_i will be pruned (deleted).

Output Format

Output mm lines. On line ii, output an integer kk, which is the number of good non-empty subsets of the original tree after deleting the subtree rooted at qiq_i.

5
1 2
2 3
2 4
1 5
2
2 5
3
13
10
1 2
2 3
2 4
1 5
5 6
5 7
7 8
7 10
1 9
3
5 2 8
21
66
201

Hint

Explanation of Sample 1: After deleting the subtree rooted at 22, all 33 non-empty subsets of the remaining node set satisfy the property. After deleting the subtree rooted at 55, among all 1515 non-empty subsets of the remaining node set, {3,4}\{3,4\} and {1,3,4}\{1,3,4\} do not satisfy the property.

For the first 10%10\% of the testdata, n,m20n, m \leq 20.

For the first 30%30\% of the testdata, n,m3000n, m \leq 3000.

For 100%100\% of the testdata, 1n,m5×1051 \leq n, m \leq 5 \times 10^5, q[1,n]q \in [1,n].

Translated by ChatGPT 5