#P11363. [NOIP2024] 树的遍历
[NOIP2024] 树的遍历
Description
Xiao Q, a beginner in algorithmic competitions, is learning about tree traversal in graph theory. A tree with nodes and edges initially has all nodes unmarked. Its traversal process is as follows:
- Select a node as the starting node for traversal and mark it.
- Suppose the currently visited node is . Find any adjacent node to that is unmarked, set as the new current node, mark it, and repeat Step 2.
- If in Step 2, all adjacent nodes of are already marked: if , the traversal ends; otherwise, set to the previous node before traversing and repeat Step 2.
For example, in the following tree, a possible traversal process is:
- Select node as the starting node and mark it.
- Node is adjacent to and unmarked. Set as the current node and mark it.
- Node is adjacent to and unmarked. Set as the current node and mark it.
- All adjacent nodes of are marked. Return to the previous node .
- Node is adjacent to and unmarked. Set as the current node and mark it.
- All adjacent nodes of are marked. Return to the previous node .
- All adjacent nodes of are marked. Return to the previous node .
- All adjacent nodes of are marked, and since is the starting node, the traversal ends.

As a creative student, Xiao Q is not satisfied with node-based traversal and begins researching edge-based traversal. Two edges are defined as adjacent if they share a common node. Initially, all edges are unmarked. The edge-based traversal process is:
- Select an edge as the starting edge for traversal and mark it.
- Suppose the currently visited edge is . Find any adjacent edge to that is unmarked, set as the new current edge, mark it, and repeat Step 2.
- If in Step 2, all adjacent edges of are marked: if , the traversal ends; otherwise, set to the previous edge before traversing and repeat Step 2.
For example, in the above tree, a possible traversal process (where denotes the edge connecting nodes and ) is:
- Select as the starting edge and mark it.
- is adjacent to , which is unmarked. Set as the current edge and mark it.
- is adjacent to , which is unmarked. Set as the current edge and mark it.
- All adjacent edges of are marked. Return to the previous edge .
- All adjacent edges of are marked. Return to the previous edge .
- All adjacent edges of are marked, and since is the starting edge, the traversal ends.
Xiao Q discovers that if each edge is treated as a new node, and a new edge is created between every pair of edges and where is chosen in Step 2, this generates a new tree with new nodes and new edges. For example, the traversal above produces the following new tree (red nodes and edges):

Now, Xiao Q has selected key edges among the edges. He wants to know how many distinct new trees can be generated by starting the traversal from any key edge. Two trees are considered different if there exists at least one pair of new nodes connected by an edge in one tree but not in the other.
Since the result may be large, output the answer modulo .
Input Format
The problem contains multiple test cases.
The first line of input contains two integers and , representing the test case ID and the number of test cases. In sample inputs, indicates that the sample shares the same data range as test case .
This is followed by test cases, each formatted as:
- The first line contains two integers and , representing the number of nodes in the tree and the number of key edges selected.
- The next lines each contain two integers and , representing the nodes connected by the -th edge in the tree.
- The next line contains integers , representing the indices of the key edges. All key edge indices are distinct.
Output Format
For each test case, output one line containing the result modulo .
1 1
4 1
1 2
2 3
2 4
1
2
7 1
5 2
1 2
1 3
2 4
2 5
1 3
3
Hint
Explanation
【Sample 1 Explanation】
Two possible new trees:
- New edges between and , and between and .
- New edges between and , and between and .
【Sample 2 Explanation】
Three possible new trees:
- New edges between and , and , and (starting from ).
- New edges between and , and , and (starting from or ).
- New edges between and , and , and (starting from ).
【Additional Samples】
Samples 3 to 12 are provided in attachments (traverse3.in to traverse12.ans), corresponding to test cases 4, 7, 11, 13, 15, 16, 18, 19, 22, and 24.
【Data Range】
For all test data:
- ;
- ;
- ;
- For every (), , forming a valid tree;
- For every (), , all distinct.
| Test Case Range | Special Properties | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| None | |||
- Special Property A: For every (), , .
- Special Property B: For every (), , .
【Note】
Input data may be large; ensure efficient reading methods.
Translated by DeepSeek R1
京公网安备 11011102002149号