#P17164. [CEOI 2026] DFS

[CEOI 2026] DFS

Problem Description

You might already be familiar with the famous DFS (depth-first search) algorithm for traversing a graph. In this problem, we will only consider connected undirected simple graphs (without loops and parallel edges) with vertices numbered 0,1,,n10,1,\ldots,n-1, and the DFS algorithm will output depths and vertices as follows:

DFS(d, v):
    output d/v
    mark vertex v as visited
    W = the list of neighbors of v ordered by increasing numbers
    for each w in W:
        if vertex w has not yet been visited:
            DFS(d + 1, w)

Write a program that outputs the number of different graphs for which the call DFS(00, n1n-1) produces the same printout as the one given on the input. For example, the output

0/20/2
1/01/0
2/12/1

is produced by calling DFS(00, 22) on either of the following two connected undirected simple 33-vertex graphs:

:::align{center} :::

Input Format

The input is the output of the call DFS(00, n1n-1) on an unknown nn-vertex connected undirected simple graph. The input thus consists of nn lines in the format d/vd/v, with the first line being 0/(n1)0/(n-1).

Output Format

Print the number of different graphs with the required property. Because this number can be very large, output the result modulo 10000000071\,000\,000\,007.

0/2
1/0
2/1
2

Hint

Constraints

  • 1n21051\le n\le 2\cdot 10^5

Subtasks

  • Subtask 11 (1010 points): n6n\le 6.
  • Subtask 22 (2020 points): n500n\le 500.
  • Subtask 33 (2020 points): n104n\le 10^4.
  • Subtask 44 (1010 points): For each i{2,,n}i\in\{2,\ldots,n\}, the ii-th input line is (i1)/(i2)(i-1)/(i-2).
  • Subtask 55 (2020 points): For each i{2,,n}i\in\{2,\ldots,n\}, the ii-th input line is (i1)/v(i-1)/v for some v{0,,n2}v\in\{0,\ldots,n-2\}.
  • Subtask 66 (2020 points): No additional constraints.