#P9480. [NOI2023] 深搜

    ID: 10684 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>线段树2023NOI离散化O2优化动态规划优化树形 DP容斥原理

[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 G=(V,E)G = (V, E) and a starting vertex ss, we can obtain a tree TT.

The procedure is described as follows:

  1. Set the stack SS to be empty, and let T=(V,)T = (V, \emptyset), i.e. the edge set of TT is initially empty.
  2. First, push the starting vertex ss into SS.
  3. Visit the top vertex uu of the stack, and mark uu as “visited”.
  4. If there exists a vertex adjacent to uu that has not been visited, then choose arbitrarily one of these vertices and denote it by vv. Add the edge (u,v)(u, v) into the edge set of TT, and push vv into the stack SS, then return to Step 3. If no such vertex exists, then pop uu from the stack.

It can be proved that when GG is connected, this algorithm will produce some spanning tree TT of GG. However, the tree TT 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 ss, if there exists a specific search order such that the algorithm produces exactly TT, then we say that TT is an ss-dfs tree of GG.

Now you are given a tree TT with nn vertices, numbered 1n1 \sim n, and additionally mm edges are given. We guarantee that these mm edges are all different from each other, connect different vertices, and are all different from the n1n - 1 tree edges in TT. We call the additional mm edges non-tree edges. Among these nn vertices, exactly kk vertices are specified as key vertices.

Now you want to know how many ways there are to select a subset of these mm non-tree edges (possibly selecting none), such that: after forming a graph GG using the edges of TT together with the selected non-tree edges, there exists some key vertex ss such that TT is an ss-dfs tree of GG.

Since the answer may be very large, you only need to output the number of valid selections modulo (109+7)(10 ^ 9 + 7).

Input Format

The first line contains an integer cc, indicating the test point ID. c=0c = 0 means this test point is the sample.

The second line contains three positive integers n,m,kn, m, k, representing the number of vertices, the number of non-tree edges, and the number of key vertices.

The next n1n - 1 lines each contain two positive integers u,vu, v, indicating an edge of the tree TT. It is guaranteed that these n1n - 1 edges form a tree.

The next mm lines each contain two positive integers a,ba, b, indicating a non-tree edge. It is guaranteed that (a,b)(a, b) does not coincide with any tree edge, and there are no multiple edges.

The last line contains kk positive integers s1,s2,,sks_1, s_2, \dots, s_k, indicating the IDs of the kk key vertices. It is guaranteed that s1,s2,,sks_1, s_2, \dots, s_k are pairwise distinct.

Output Format

Output one line containing a non-negative integer, representing the number of valid selections modulo (109+7)(10 ^ 9 + 7).

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 (1,3)(1, 3), select only edge (2,4)(2, 4), or select no non-tree edges.

If we select only edge (1,3)(1, 3), or select no non-tree edges, then we can find that TT is a 33-dfs tree of graph GG. The specified search order is as follows:

  1. Push 33 into stack SS. Now S=[3]S = [3].
  2. Mark 33 as “visited”.
  3. Since 33 is connected to 22 and 22 is “unvisited”, push 22 into stack SS, and add (3,2)(3, 2) into tree TT. Now S=[3,2]S = [3, 2].
  4. Mark 22 as “visited”.
  5. Since 22 is connected to 11 and 11 is “unvisited”, push 11 into stack SS, and add (2,1)(2, 1) into tree TT. Now S=[3,2,1]S = [3, 2, 1].
  6. Since all vertices adjacent to 11 are “visited”, pop 11 from the stack. Now S=[3,2]S = [3, 2].
  7. Since all vertices adjacent to 22 are “visited”, pop 22 from the stack. Now S=[3]S = [3].
  8. Since 33 is connected to 44 and 44 is “unvisited”, push 44 into stack SS, and add (3,4)(3, 4) into tree TT. Now S=[3,4]S = [3, 4].
  9. Since all vertices adjacent to 44 are “visited”, pop 44 from the stack. Now S=[3]S = [3].
  10. Since all vertices adjacent to 33 are “visited”, pop 33 from the stack, and now SS becomes empty again.

If we select only edge (2,4)(2, 4), then we can show that TT is a 22-dfs tree of graph GG. The specified search order is as follows:

  1. Push 22 into stack SS. Now S=[2]S = [2].
  2. Mark 22 as “visited”.
  3. Since 22 is connected to 33 and 33 is “unvisited”, push 33 into stack SS, and add (2,3)(2, 3) into tree TT. Now S=[2,3]S = [2, 3].
  4. Mark 33 as “visited”.
  5. Since 33 is connected to 44 and 44 is “unvisited”, push 44 into stack SS, and add (3,4)(3, 4) into tree TT. Now S=[2,3,4]S = [2, 3, 4].
  6. Since all vertices adjacent to 44 are “visited”, pop 44 from the stack. Now S=[2,3]S = [2, 3].
  7. Since all vertices adjacent to 33 are “visited”, pop 33 from the stack. Now S=[2]S = [2].
  8. Since 22 is connected to 11 and 11 is “unvisited”, push 11 into stack SS, and add (2,1)(2, 1) into tree TT. Now S=[2,1]S = [2, 1].
  9. Since all vertices adjacent to 11 are “visited”, pop 11 from the stack. Now S=[2]S = [2].
  10. Since all vertices adjacent to 22 are “visited”, pop 22 from the stack, and now SS becomes empty again.

[Sample Explanation #2]

This sample satisfies the constraints of test points 464 \sim 6.

[Sample Explanation #3]

This sample satisfies the constraints of test points 101110 \sim 11.

[Sample Explanation #4]

This sample satisfies the constraints of test points 121312 \sim 13.

[Sample Explanation #5]

This sample satisfies the constraints of test points 141614 \sim 16.

[Sample Explanation #6]

This sample satisfies the constraints of test points 232423 \sim 24.

[Constraints]

For all testdata, it is guaranteed that: 1kn5×1051 \le k \le n \le 5 \times 10 ^ 5, 1m5×1051 \le m \le 5 \times 10 ^ 5.

Test Point ID nn \le mm \le kk \le Special Property
131 \sim 3 66 nn None
464 \sim 6 1515 66
797 \sim 9 300300
101110 \sim 11 nn A
121312 \sim 13 B
141614 \sim 16 None
171817 \sim 18 2×1052 \times 10 ^ 5 A
192119 \sim 21 B
2222 None
232523 \sim 25 5×1055 \times 10 ^ 5

Special property A: In TT, vertex ii is connected to vertex i+1i + 1 (1i<n1 \le i < n).

Special property B: If we form a graph GG using the edges of TT together with all mm non-tree edges, then TT is a 11-dfs tree of GG.

Please note that vertex 11 is not necessarily one of the kk key vertices.

Translated by ChatGPT 5