Depth-first search is a common search algorithm. Using this algorithm, we can obtain a tree from an undirected connected graph with no self-loops nor parallel edges, and a certain starting point .
The algorithm can be described as follows:
- Set the stack to be empty, and let , which means that the edge set of is initially empty.
- First, push the starting point into .
- Visit the top vertex of the stack and mark u as "visited".
- If there is a vertex adjacent to u and not yet visited, arbitrarily select one from these vertices and let be added to the edge set of . Then, push into the stack , and go back to step . If there is no such vertex, pop out of the stack.
It can be proved that when is a connected graph, the algorithm will obtain a certain spanning tree of . However, the tree obtained by the algorithm may not be unique, depending on the search order, i.e., the vertex selected in step 4. If a specific search order can be chosen so that the tree obtained by the algorithm is exactly , then we call an -dfs tree of with respect to the starting point .
Now, given a tree with vertices labeled from to , and an additional edges, we guarantee that these edges are distinct and connect different vertices, and are different from the tree edges in . We call these additional edges non-tree edges. Among these vertices, we specify exactly vertices as special vertices.
Now, you want to know how many ways there are to select a subset of these non-tree edges (you can possibly select none) such that: after the tree edges of and the selected non-tree edges are combined to form a graph , there exists a special vertex such that is an -dfs tree of .
Since the answer may be very large, you only need to output the number of solutions modulo .
Input Specification
The first line of input contains an integer , which represents the test case number. represents that this test case is a sample test.
The second line of input contains three positive integers , which represent the number of vertices, the number of non-tree edges, and the number of critical points, respectively.
Then lines follow, each containing two positive integers , representing a tree edge of . It is guaranteed that these edges form a tree.
Then lines follow, each containing two positive integers , representing a non-tree edge. It is guaranteed that does not coincide with an edge on the tree and there are no duplicate edges.
The last line of input contains positive integers , representing the labels of the special vertices. It is guaranteed that are distinct from each other.
Output Specification
Output a line containing an integer, representing the number of solutions, taken modulo .
Sample Input 1
0
4 2 2
1 2
2 3
3 4
1 3
2 4
2 3
Sample Output 1
3
Explanation for Sample Output 1
In this sample, there are three ways to select the non-tree edges: selecting only the edge , selecting only the edge , or not selecting any non-tree edges. If we select only the edge , or do not select any non-tree edges, we can show that is a -dfs tree of . The specified search order is as follows:
- Put into the stack . At this time, .
- Mark as "visited".
- Since is adjacent to and is "unvisited", put into the stack and add to tree . At this time, .
- Mark as "visited".
- Since is adjacent to and is "unvisited", put into stack and add to tree . At this time, .
- Since all the vertices adjacent to are "visited", pop off the stack. At this time, .
- Since all the vertices adjacent to are "visited", pop off the stack. At this time, .
- Since is adjacent to and is "unvisited", put into stack and add to tree T. At this time, .
- Since all the vertices adjacent to are "visited", pop off the stack. At this time, .
- Since all the vertices adjacent to are "visited", pop off the stack and becomes empty again.
If we select only the edge , we can show that is a -dfs tree of . The specified search order is as follows:
- Put into stack . At this time, .
- Mark as "visited".
- Since is adjacent to and is "unvisited", put into the stack , and add to tree . At this time, .
- Mark as "visited".
- Since is adjacent to and is "unvisited", put into the stack , and add to tree . At this time, .
- Since all the neighboring vertices of are "visited", pop out of the stack. At this time, .
- Since all the neighboring vertices of are "visited", pop out of the stack. At this time, .
- Since is adjacent to and is "unvisited", put into the stack , and add to tree T. At this time, .
- Since all the neighboring vertices of are "visited", pop out of the stack. At this time, .
- Since all the neighboring vertices of are "visited", pop out of the stack and becomes empty again.
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
ex_dfs2.in
andex_dfs2.ans
) corresponds to test cases 4-6. - Sample 3 (
ex_dfs3.in
andex_dfs3.ans
) corresponds to test cases 10-11. - Sample 4 (
ex_dfs4.in
andex_dfs4.ans
) corresponds to test cases 12-13. - Sample 5 (
ex_dfs5.in
andex_dfs5.ans
) corresponds to test cases 14-16. - Sample 6 (
ex_dfs6.in
andex_dfs6.ans
) corresponds to test cases 23-25.
Problem Constraints
For all test data, it is guaranteed that: .
Test ID | Additional Constraints | |||
---|---|---|---|---|
None | ||||
A | ||||
B | ||||
None | ||||
A | ||||
B | ||||
None | ||||
Additional Constraint A: It is guaranteed that in , vertex is connected to vertex .
Additional Constraint B: It is guaranteed that if the edges of are combined with all non-tree edges to form a graph , then is an -dfs tree of .
Comments