#P4129. [NEERC 2005 / SHOI2006] 仙人掌

    ID: 3061 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp高精度2006各省省选上海仙人掌

[NEERC 2005 / SHOI2006] 仙人掌

Description

A cactus is an undirected connected graph in which each edge appears in at most one simple cycle. Intuitively, a cactus can be viewed as a tree that is allowed to have cycles. There is, however, a fundamental difference between cacti and trees: a cactus can have multiple connected spanning subgraphs, whereas a tree has only one (itself). We call the number of connected spanning subgraphs the “Xian” number (xiān). Your task is to compute the “Xian” number of the given graph.

Some examples of cacti:

The first figure is a cactus. In the second figure, the edge (2,3)(2,3) lies on two different cycles, so it is not a cactus. The third figure is not connected, so it is not a cactus either.

Explanations of some terms:

  • Simple cycle: A simple cycle is a path in the original graph whose edge set forms a cycle, and each vertex appears at most once in the cycle. For example, for the second figure above, it has three simple cycles: (4,3,2,1,6,5)(4,3,2,1,6,5), (7,8,9,10,2,3)(7,8,9,10,2,3), and (4,3,7,8,9,10,2,1,6,5)(4,3,7,8,9,10,2,1,6,5).
  • Spanning subgraph: A spanning subgraph is also a subgraph of the original graph; it may have fewer edges, but it must preserve connectivity and must not remove any vertices of the original graph. The notion of “spanning” is similar to that in the familiar “minimum spanning tree.” For the first figure above, removing any one edge in cycle I or any one edge in cycle II yields a spanning subgraph, so the total number of spanning subgraphs is 6+4+6×4+1=356 + 4 + 6 \times 4 + 1 = 35 (note that a graph is also a subgraph of itself).

Input Format

The first line contains two integers nn and mm. The nn vertices are numbered from 11 to nn.

Each of the next mm lines describes a path in the graph (note: such a path is not necessarily a cycle). The format of a line is: an integer kik_i indicating how many vertices this path goes through, followed by kik_i integers between 11 and nn, each denoting a vertex of the graph; consecutive vertices define an edge. A path may pass through the same vertex multiple times. For example, in the first figure, the first path goes from 22 through 33, and then returns to 33 from 88. We guarantee that every edge appears in exactly one path: no edge appears in two different paths, and no edge appears twice within a single path.

Output Format

Output the graph’s “Xian” number. If it is not a cactus, output 0. Note that the final answer can be very large.

14 3
9 1 2 3 4 5 6 7 8 3
7 2 9 10 11 12 13 10
2 2 14
35
10 2
7 1 2 3 4 5 6 1
6 3 7 8 9 10 2
0
5 1
4 1 2 3 4
0

Hint

For all testdata, 1n200001 \le n \le 20000, 0m10000 \le m \le 1000, and 2ki10002 \le k_i \le 1000.

Translated by ChatGPT 5