#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 made up of time points: the root represents birth, and the leaves represent death. Each non-leaf node has one or more children , meaning that at the time point represented by , the person can make different choices, leading to different possibilities. Formally, a choice is an edge in the tree, where is the parent of .
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 to such that , , and is an ancestor of . Mathematically, such a potential life experience is written as an ordered pair , and the set of all potential life experiences of the tree is denoted by .
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 , such that every potential life experience is important.
The shape of the tree has long been computed and fixed, and the set 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 and set , how many different ways are there to decide whether each edge is important, such that the observed constraints from are satisfied? That is, for every , there must be an edge on the path from to that is determined to be important.
Formally: Given a tree and a set of pairs , such that for all , we have and is an ancestor of in the tree . Here, and are the node set and edge set of , respectively. Find the number of different functions (assigning each edge a value equal to or ), such that for any , there exists an edge on the path from to with . Since the answer may be very large, you only need to output it modulo (a prime number).
Input Format
Read from standard input.
The first line contains a positive integer , denoting the size of the tree . The nodes are numbered from to , and node is the root.
In the next lines, each line contains two integers separated by spaces, with , indicating that there is an edge between nodes and , but the direction of this edge is not guaranteed.
The next line contains a non-negative integer , denoting the number of observed pieces of information.
In the next lines, each line contains two integers separated by spaces, indicating . Note: The input may contain duplicate information. In other words, there may exist such that and .
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 .
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 assignments in total. Among them, the following do not satisfy the requirements:
- Set to unimportant, and set to important: no constraint in is satisfied.
- Set to unimportant: no constraint in is satisfied.
- Set to unimportant, and set to important: in , is not satisfied.
- Set to unimportant, and set to important: in , is not satisfied.
- Set to unimportant, and set to important: in , is not satisfied.
- Set to unimportant, and set to important: in , is not satisfied.
- Under other assignments, all constraints in 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 | is a complete binary tree | ||
|---|---|---|---|
| No | |||
| Yes | |||
| No | |||
Constraints
All testdata satisfy: , . The input forms a tree, and for , is always an ancestor of .
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
京公网安备 11011102002149号