#P6773. [NOI2020] 命运

[NOI2020] 命运

Description

Hint: In the last paragraph of the statement, we provide a brief, formal description.

In the distant future, physicists finally discovered the natural laws of time and causality. Even before a person is born, we can learn some information about his or her life through theoretical analysis. In other words, physics allows us to “predict” a person’s “destiny” to some extent.

Simply put, a person’s destiny is a rooted tree TT made up of time points: the root represents birth, and the leaves represent death. Each non-leaf node uu has one or more children v1,v2,,vcuv_1, v_2,\dots, v_{c_u}, meaning that at the time point represented by uu, the person can make cuc_u different choices, leading to different possibilities. Formally, a choice is an edge (u,vi)(u, v_i) in the tree, where uu is the parent of viv_i.

A person’s life is a path from birth (the root) to death (some leaf), without repeating nodes. Any subpath of this path that contains at least one edge is a life experience. All life experiences that he or she could have, by living the life in all possible ways, are called potential life experiences. In other words, all potential life experiences are all paths from uu to vv such that u,vTu, v \in T, uvu \neq v, and uu is an ancestor of vv. Mathematically, such a potential life experience is written as an ordered pair (u,v)(u, v), and the set of all potential life experiences of the tree TT is denoted by PT\mathcal P_T.

Physical theory not only allows us to observe the tree that represents destiny, but also lets us analyze whether some potential life experiences are “important”. Each choice a person makes—namely, each edge of the tree—may be important or unimportant. A potential life experience is called important if and only if there exists at least one important edge on the corresponding path. We can observe that some potential life experiences are important. In other words, we can observe a set QPT\mathcal Q \subseteq \mathcal P_T, such that every potential life experience (u,v)Q(u, v) \in \mathcal Q is important.

The shape of the tree TT has long been computed and fixed, and the set Q\mathcal Q has also been observed, so the uncertainty of a person’s destiny has been greatly reduced. But the uncertainty is still huge—let us compute it. For a given tree TT and set Q\mathcal Q, how many different ways are there to decide whether each edge is important, such that the observed constraints from Q\mathcal Q are satisfied? That is, for every (u,v)Q(u, v) \in \mathcal Q, there must be an edge on the path from uu to vv that is determined to be important.

Formally: Given a tree T=(V,E)T = (V, E) and a set of pairs QV×V\mathcal Q \subseteq V \times V, such that for all (u,v)Q(u, v) \in \mathcal Q, we have uvu \neq v and uu is an ancestor of vv in the tree TT. Here, VV and EE are the node set and edge set of TT, respectively. Find the number of different functions f:E{0,1}f : E \to \{0, 1\} (assigning each edge eEe \in E a value f(e)f(e) equal to 00 or 11), such that for any (u,v)Q(u, v) \in \mathcal Q, there exists an edge ee on the path from uu to vv with f(e)=1f(e) = 1. Since the answer may be very large, you only need to output it modulo 998,244,353998,244,353 (a prime number).

Input Format

Read from standard input.

The first line contains a positive integer nn, denoting the size of the tree TT. The nodes are numbered from 11 to nn, and node 11 is the root.

In the next n1n - 1 lines, each line contains two integers xi,yix_i, y_i separated by spaces, with 1xi,yin1 \leq x_i, y_i \leq n, indicating that there is an edge between nodes xix_i and yiy_i, but the direction of this edge is not guaranteed.

The next line contains a non-negative integer mm, denoting the number of observed pieces of information.

In the next mm lines, each line contains two integers ui,viu_i, v_i separated by spaces, indicating (ui,vi)Q(u_i, v_i) \in \mathcal Q. Note: The input may contain duplicate information. In other words, there may exist iji \neq j such that ui=uju_i = u_j and vi=vjv_i = v_j.

The input size and constraints can be found in the table at the end of the statement.

Output Format

Write to standard output.

Output a single line with one integer, the number of valid assignments modulo 998,244,353998,244,353.

5
1 2
2 3
3 4
3 5
2
1 3
2 5
10
15
2 1
3 1
4 3
5 2
6 3
7 6
8 4
9 5
10 7
11 5
12 10
13 3
14 9
15 8
6
3 12
5 11
2 5
3 13
8 15
1 13
960

Hint

Explanation for Sample 1

There are 1616 assignments in total. Among them, the following 66 do not satisfy the requirements:

  • Set (1,2),(2,3),(3,5)(1, 2),(2, 3),(3, 5) to unimportant, and set (3,4)(3, 4) to important: no constraint in Q\mathcal Q is satisfied.
  • Set (1,2),(2,3),(3,4),(3,5)(1, 2),(2, 3),(3, 4),(3, 5) to unimportant: no constraint in Q\mathcal Q is satisfied.
  • Set (1,2),(2,3)(1, 2),(2, 3) to unimportant, and set (3,4),(3,5)(3, 4),(3, 5) to important: in Q\mathcal Q, (1,3)(1, 3) is not satisfied.
  • Set (1,2),(2,3),(3,4)(1, 2),(2, 3),(3, 4) to unimportant, and set (3,5)(3, 5) to important: in Q\mathcal Q, (1,3)(1, 3) is not satisfied.
  • Set (2,3),(3,5)(2, 3),(3, 5) to unimportant, and set (1,2),(3,4)(1, 2),(3, 4) to important: in Q\mathcal Q, (2,5)(2, 5) is not satisfied.
  • Set (2,3),(3,4),(3,5)(2, 3),(3, 4),(3, 5) to unimportant, and set (1,2)(1, 2) to important: in Q\mathcal Q, (2,5)(2, 5) is not satisfied.
  • Under other assignments, all constraints in Q\mathcal Q are satisfied.

Sample 3

See destiny/destiny3.in and destiny/destiny3.ans in the contestant directory.

Sample 4

See destiny/destiny4.in and destiny/destiny4.ans in the contestant directory.

Test Point ID nn mm TT is a complete binary tree
141\sim 4 10\le 10 No
55 500\le 500 15\le 15
66 104\le 10^4 10\le 10
77 105\le 10^5 16\le 16
88 5×105\le 5\times 10^5
99 105\le 10^5 22\le 22
1010 5×105\le 5\times 10^5
1111 600\le 600
1212 103\le 10^3
131413\sim 14 2×103\le 2\times 10^3 5×105\le 5\times 10^5
151615\sim 16 5×105\le 5\times 10^5 2×103\le 2\times 10^3
171817\sim 18 105\le 10^5 105\le 10^5 Yes
1919 5×104\le 5\times 10^4 No
2020 8×104\le 8\times 10^4
212221\sim 22 105\le 10^5 5×105\le 5\times 10^5
232523\sim 25 5×105\le 5\times 10^5

Constraints

All testdata satisfy: n5×105n \leq 5 \times 10^5, m5×105m \leq 5 \times 10^5. The input forms a tree, and for 1im1 \leq i \leq m, uiu_i is always an ancestor of viv_i.

Complete binary tree: In this problem, a full binary tree is a tree in which every non-leaf node has left and right children, and all leaf nodes have the same depth. Number the nodes of a full binary tree from top to bottom and from left to right; the tree formed by the first few nodes with the smallest indices is called a complete binary tree.

Translated by ChatGPT 5