QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#10128. Tree Traversal

统计

Little Q is a beginner in competitive programming and is currently learning about tree traversal in graph theory. For a tree consisting of $n$ nodes and $n-1$ edges, where all nodes are initially unmarked, the traversal process is as follows:

  1. Select a node $s$ as the starting node for the traversal and mark it.
  2. Suppose the currently visited node is $u$. Find any unmarked node $v$ adjacent to $u$, set $v$ as the new currently visited node, and mark it. Then, repeat step 2.
  3. Suppose in step 2, all nodes adjacent to $u$ have been marked. If $u = s$, the traversal process ends; otherwise, set $u$ to the node visited immediately before $u$ and return to step 2.

As a student with creative ideas, after learning the above, Little Q is not satisfied with node-based traversal and has begun researching edge-based traversal. Two edges are defined as adjacent if and only if they share a common node. Initially, all edges are unmarked. This edge-based traversal process is as follows:

  1. Select an edge $b$ as the starting edge for the traversal and mark it.
  2. Suppose the currently visited edge is $e$. Find any unmarked edge $f$ adjacent to $e$, set $f$ as the new currently visited edge, and mark it. Then, repeat step 2.
  3. Suppose in step 2, all edges adjacent to $e$ have been marked. If $e = b$, the traversal process ends; otherwise, set $e$ to the edge visited immediately before $e$ and return to step 2.

Little Q was surprised to discover that in this new tree traversal process, if each edge is treated as a new node, and a new edge is connected between all new nodes $e$ and $f$ from step 2, a new tree consisting of $n-1$ new nodes and $n-2$ new edges is generated.

Now, Little Q has selected $k$ key edges out of the $n-1$ edges. Little Q wants to know how many different new trees can be generated by using any one of the key edges as the starting traversal edge through the above process. Two trees are considered different if and only if there exists at least one pair of new nodes that are connected by a new edge in one tree but not in the other.

Since the result can be very large, you only need to output the result modulo $10^9 + 7$.

Input

The input is read from the file traverse.in.

There are multiple test cases. The first line contains two integers $c$ and $T$, representing the test case ID and the number of test cases. In the sample, $c$ indicates that the sample has the same data range as test case $c$.

Each of the $T$ test cases follows the format: The first line contains two integers $n$ and $k$, representing the number of nodes in the tree and the number of key edges selected by Little Q. The next $n-1$ lines each contain two integers $u_i$ and $v_i$, representing that the edge with index $i$ connects nodes $u_i$ and $v_i$. * The next line contains $k$ integers $e_1, e_2, \dots, e_k$, representing the indices of the key edges selected by Little Q. It is guaranteed that the indices of the key edges are distinct.

Output

Output to the file traverse.out.

For each test case, output a single line containing an integer representing the result modulo $10^9 + 7$.

Constraints

For all test cases, it is guaranteed that: $1 \le T \le 10$ $2 \le n \le 10^5$ $1 \le k < n$ For any $i$ ($1 \le i \le n-1$), $1 \le u_i, v_i \le n$, and they form a valid tree. * For any $i$ ($1 \le i \le k$), $1 \le e_i < n$, and all $e_i$ are distinct.

Test Case $n$ $k$ Special Property
$1 \sim 3$ $\le 5$ $\le 1$ None
$4 \sim 6$ $\le 10^5$ $\le 1$ None
$7 \sim 10$ $\le 10^5$ $\le 2$ None
$11, 12$ $\le 500$ $\le 8$ None
$13, 14$ $\le 10^2$ $< n$ None
$15$ $\le 500$ $< n$ None
$16, 17$ $\le 10^5$ $\le 500$ None
$18$ $\le 10^5$ $< n$ A
$19 \sim 21$ $\le 10^5$ $< n$ B
$22, 23$ $\le 2 \times 10^4$ $< n$ None
$24, 25$ $\le 10^5$ $< n$ None
  • Special Property A: For any $i$ ($1 \le i \le n-1$), $u_i = i, v_i = i+1$.
  • Special Property B: For any $i$ ($1 \le i \le n-1$), $u_i = 1, v_i = i+1$.

Examples

Input 1

1 1
4 1
1 2
2 3
2 4
1

Output 1

2

Note

The scale of the input data may be large; please pay attention to the efficiency of your input reading method.

Figure 1. An example of a tree with 4 nodes.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.