#P11363. [NOIP2024] 树的遍历

[NOIP2024] 树的遍历

Description

Xiao Q, a beginner in algorithmic competitions, is learning about tree traversal in graph theory. A tree with nn nodes and n1n - 1 edges initially has all nodes unmarked. Its traversal process is as follows:

  1. Select a node ss as the starting node for traversal and mark it.
  2. Suppose the currently visited node is uu. Find any adjacent node vv to uu that is unmarked, set vv as the new current node, mark it, and repeat Step 2.
  3. If in Step 2, all adjacent nodes of uu are already marked: if u=su = s, the traversal ends; otherwise, set uu to the previous node before traversing uu and repeat Step 2.

For example, in the following tree, a possible traversal process is:

  • Select node 11 as the starting node and mark it.
  • Node 22 is adjacent to 11 and unmarked. Set 22 as the current node and mark it.
  • Node 33 is adjacent to 22 and unmarked. Set 33 as the current node and mark it.
  • All adjacent nodes of 33 are marked. Return to the previous node 22.
  • Node 44 is adjacent to 22 and unmarked. Set 44 as the current node and mark it.
  • All adjacent nodes of 44 are marked. Return to the previous node 22.
  • All adjacent nodes of 22 are marked. Return to the previous node 11.
  • All adjacent nodes of 11 are marked, and since 11 is the starting node, the traversal ends.

Pic1: The tree in sample 1\text{Pic1: The tree in sample 1}

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:

  1. Select an edge bb as the starting edge for traversal and mark it.
  2. Suppose the currently visited edge is ee. Find any adjacent edge ff to ee that is unmarked, set ff as the new current edge, mark it, and repeat Step 2.
  3. If in Step 2, all adjacent edges of ee are marked: if e=be = b, the traversal ends; otherwise, set ee to the previous edge before traversing ee and repeat Step 2.

For example, in the above tree, a possible traversal process (where {u,v}\{u, v\} denotes the edge connecting nodes uu and vv) is:

  • Select {1,2}\{1, 2\} as the starting edge and mark it.
  • {1,2}\{1, 2\} is adjacent to {2,3}\{2, 3\}, which is unmarked. Set {2,3}\{2, 3\} as the current edge and mark it.
  • {2,3}\{2, 3\} is adjacent to {2,4}\{2, 4\}, which is unmarked. Set {2,4}\{2, 4\} as the current edge and mark it.
  • All adjacent edges of {2,4}\{2, 4\} are marked. Return to the previous edge {2,3}\{2, 3\}.
  • All adjacent edges of {2,3}\{2, 3\} are marked. Return to the previous edge {1,2}\{1, 2\}.
  • All adjacent edges of {1,2}\{1, 2\} are marked, and since {1,2}\{1, 2\} 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 ee and ff where ff is chosen in Step 2, this generates a new tree with n1n-1 new nodes and n2n-2 new edges. For example, the traversal above produces the following new tree (red nodes and edges):

Pic2: A possible new tree\text{Pic2: A possible new tree}

Now, Xiao Q has selected kk key edges among the n1n-1 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 109+710^9+7.

Input Format

The problem contains multiple test cases.

The first line of input contains two integers cc and TT, representing the test case ID and the number of test cases. In sample inputs, cc indicates that the sample shares the same data range as test case cc.

This is followed by TT test cases, each formatted as:

  • The first line contains two integers nn and kk, representing the number of nodes in the tree and the number of key edges selected.
  • The next n1n - 1 lines each contain two integers uiu_i and viv_i, representing the nodes connected by the ii-th edge in the tree.
  • The next line contains kk integers e1,e2,,eke_1, e_2, \dots, e_k, 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 109+710^9 + 7.

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:

  1. New edges between {1,2}\{1, 2\} and {2,3}\{2, 3\}, and between {2,3}\{2, 3\} and {2,4}\{2, 4\}.
  2. New edges between {1,2}\{1, 2\} and {2,4}\{2, 4\}, and between {2,4}\{2, 4\} and {2,3}\{2, 3\}.

【Sample 2 Explanation】

Three possible new trees:

  1. New edges between {1,2}\{1, 2\} and {1,3}\{1, 3\}, {1,2}\{1, 2\} and {2,4}\{2, 4\}, {2,4}\{2, 4\} and {2,5}\{2, 5\} (starting from {1,2}\{1, 2\}).
  2. New edges between {1,2}\{1, 2\} and {1,3}\{1, 3\}, {1,2}\{1, 2\} and {2,5}\{2, 5\}, {2,5}\{2, 5\} and {2,4}\{2, 4\} (starting from {1,2}\{1, 2\} or {2,4}\{2, 4\}).
  3. New edges between {1,2}\{1, 2\} and {1,3}\{1, 3\}, {1,2}\{1, 2\} and {2,4}\{2, 4\}, {1,2}\{1, 2\} and {2,5}\{2, 5\} (starting from {2,4}\{2, 4\}).

【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:

  • 1T101 \leq T \leq 10;
  • 2n1052 \leq n \leq 10^5;
  • 1k<n1 \leq k < n;
  • For every ii (1in11 \leq i \leq n - 1), 1ui,vin1 \leq u_i, v_i \leq n, forming a valid tree;
  • For every ii (1ik1 \leq i \leq k), 1ei<n1 \leq e_i < n, all distinct.
Test Case Range nn kk Special Properties
131\sim 3 5\leq 5 1\leq 1 None
464\sim 6 105\leq 10^5
7107\sim 10 2\leq 2
11,1211,12 500\leq 500 8\leq 8
13,1413,14 102\leq 10^2 <n<n
1515 500\leq 500
16,1716,17 105\leq 10^5 500\leq 500
1818 <n<n A
192119\sim 21 B
22,2322,23 2×104\leq 2\times 10^4 None
24,2524,25 105\leq 10^5
  • Special Property A: For every ii (1in11 \leq i \leq n - 1), ui=iu_i = i, vi=i+1v_i = i + 1.
  • Special Property B: For every ii (1in11 \leq i \leq n - 1), ui=1u_i = 1, vi=i+1v_i = i + 1.

【Note】

Input data may be large; ensure efficient reading methods.


Translated by DeepSeek R1