Depth First Search
Time limit6sMemory limit1024 MB
Count subsets of the m extra edges so that tree T, plus the chosen edges, is a DFS tree from one of the k special vertices.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
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 as visited.
- If there is a vertex adjacent to 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 3. 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.
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, output the number of solutions modulo .
Input
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 special vertices.
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
Output a line containing an integer, representing the number of solutions, taken modulo .
Constraints
For all test data, it is guaranteed that and .