#P4129. [NEERC 2005 / SHOI2006] 仙人掌
[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 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: , , and .
- 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 (note that a graph is also a subgraph of itself).
Input Format
The first line contains two integers and . The vertices are numbered from to .
Each of the next lines describes a path in the graph (note: such a path is not necessarily a cycle). The format of a line is: an integer indicating how many vertices this path goes through, followed by integers between and , 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 through , and then returns to from . 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, , , and .
Translated by ChatGPT 5
京公网安备 11011102002149号