#P9480. [NOI2023] 深搜
[NOI2023] 深搜
Problem Description
Depth-first search is a common search algorithm. Using this algorithm, given a simple (no multiple edges, no self-loops) undirected connected graph and a starting vertex , we can obtain a tree .
The procedure is described as follows:
- Set the stack to be empty, and let , i.e. the edge set of is initially empty.
- First, push the starting vertex into .
- Visit the top vertex of the stack, and mark as “visited”.
- If there exists a vertex adjacent to that has not been visited, then choose arbitrarily one of these vertices and denote it by . Add the edge into the edge set of , and push into the stack , then return to Step 3. If no such vertex exists, then pop from the stack.
It can be proved that when is connected, this algorithm will produce some spanning tree of . However, the tree obtained by the algorithm may not be unique: it depends on the search order, that is, the vertex chosen in Step 4. After fixing the starting vertex , if there exists a specific search order such that the algorithm produces exactly , then we say that is an -dfs tree of .
Now you are given a tree with vertices, numbered , and additionally edges are given. We guarantee that these edges are all different from each other, connect different vertices, and are all different from the tree edges in . We call the additional edges non-tree edges. Among these vertices, exactly vertices are specified as key vertices.
Now you want to know how many ways there are to select a subset of these non-tree edges (possibly selecting none), such that: after forming a graph using the edges of together with the selected non-tree edges, there exists some key vertex such that is an -dfs tree of .
Since the answer may be very large, you only need to output the number of valid selections modulo .
Input Format
The first line contains an integer , indicating the test point ID. means this test point is the sample.
The second line contains three positive integers , representing the number of vertices, the number of non-tree edges, and the number of key vertices.
The next lines each contain two positive integers , indicating an edge of the tree . It is guaranteed that these edges form a tree.
The next lines each contain two positive integers , indicating a non-tree edge. It is guaranteed that does not coincide with any tree edge, and there are no multiple edges.
The last line contains positive integers , indicating the IDs of the key vertices. It is guaranteed that are pairwise distinct.
Output Format
Output one line containing a non-negative integer, representing the number of valid selections modulo .
0
4 2 2
1 2
2 3
3 4
1 3
2 4
2 3
3
见附件中的 dfs/dfs2.in。
见附件中的 dfs/dfs2.ans。
见附件中的 dfs/dfs3.in。
见附件中的 dfs/dfs3.ans。
见附件中的 dfs/dfs4.in。
见附件中的 dfs/dfs4.ans。
见附件中的 dfs/dfs5.in。
见附件中的 dfs/dfs5.ans。
见附件中的 dfs/dfs6.in。
见附件中的 dfs/dfs6.ans。
Hint
[Sample Explanation #1]
In this sample, there are three ways to select non-tree edges: select only edge , select only edge , or select no non-tree edges.
If we select only edge , or select no non-tree edges, then we can find that is a -dfs tree of graph . The specified search order is as follows:
- Push into stack . Now .
- Mark as “visited”.
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Mark as “visited”.
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since all vertices adjacent to are “visited”, pop from the stack, and now becomes empty again.
If we select only edge , then we can show that is a -dfs tree of graph . The specified search order is as follows:
- Push into stack . Now .
- Mark as “visited”.
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Mark as “visited”.
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since is connected to and is “unvisited”, push into stack , and add into tree . Now .
- Since all vertices adjacent to are “visited”, pop from the stack. Now .
- Since all vertices adjacent to are “visited”, pop from the stack, and now becomes empty again.
[Sample Explanation #2]
This sample satisfies the constraints of test points .
[Sample Explanation #3]
This sample satisfies the constraints of test points .
[Sample Explanation #4]
This sample satisfies the constraints of test points .
[Sample Explanation #5]
This sample satisfies the constraints of test points .
[Sample Explanation #6]
This sample satisfies the constraints of test points .
[Constraints]
For all testdata, it is guaranteed that: , .
| Test Point ID | Special Property | |||
|---|---|---|---|---|
| None | ||||
| A | ||||
| B | ||||
| None | ||||
| A | ||||
| B | ||||
| None | ||||
Special property A: In , vertex is connected to vertex ().
Special property B: If we form a graph using the edges of together with all non-tree edges, then is a -dfs tree of .
Please note that vertex is not necessarily one of the key vertices.
Translated by ChatGPT 5