Under construction

Still being built. Things may change

Cactus Automorphisms

Time limit5sMemory limit256 MB

Summary
Count the automorphisms of a given cactus graph with up to 50000 vertices and print the count as a prime factorization.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Combinatorics, Number theory
Solved
No attempts yet

Problem

A cactus is a connected undirected graph in which every edge belongs to at most one simple cycle. Think of it as a tree that is allowed a few cycles. NEERC has been setting problems about cactuses for years. In 2005, the first year one appeared, the problem was called simply Cactus. In 2007 it was Cactus Reloaded, and in 2010 it was Cactus Revolution.

Preparing test data for those problems runs into one difficulty. Some wrong solutions behave differently depending on how the vertices are numbered in the input, so for the most interesting tests the judges include the same graph several times with different numbering. Some graphs are regular enough that renumbering the vertices leaves the graph unchanged. To decide objectively how many tests a graph deserves, the judges want a number that says how regular that graph is.

That number is the count of graph automorphisms. Let (V,E)(V, E) be an undirected graph, where VV is a set of vertices and EE is a set of edges, and each edge is a set of two distinct vertices {v1,v2}\{v_1, v_2\} with v1,v2∈Vv_1, v_2 \in V. A graph automorphism is a bijection mm from VV onto VV such that for every pair of vertices v1v_1 and v2v_2 joined by an edge (so {v1,v2}∈E\{v_1, v_2\} \in E), the condition {m(v1),m(v2)}∈E\{m(v_1), m(v_2)\} \in E holds.

Every graph has at least one automorphism, the identity, and a graph with nn vertices has at most n!n! of them. The count can be very large, so the answer must be written as a prime factorization ∏i=1kpiqi\prod_{i=1}^{k} p_i^{q_i}, where the pip_i are primes in ascending order (pi≥2p_i \ge 2, pi<pi+1p_i < p_{i+1}) and the qiq_i are the matching exponents (qi>0q_i > 0).

Input

The first line contains two integers nn and mm (1≤n≤50 0001 \le n \le 50\,000, 0≤m≤50 0000 \le m \le 50\,000). Here nn is the number of vertices in the graph, numbered from 11 to nn. The edges of the graph are given as a set of edge-distinct paths, and mm is the number of those paths.

Each of the next mm lines contains one path. A path starts with an integer kik_i (2≤ki≤10002 \le k_i \le 1000) followed by kik_i integers from 11 to nn. Those kik_i integers are the vertices of the path in order. Adjacent vertices in a path are distinct. A path may visit the same vertex several times, but every edge is traversed exactly once in the whole input. There are no multiedges: at most one edge joins any two vertices.

The graph in the input is a cactus.

Output

On the first line print the number kk of prime factors in the factorization of the number of graph automorphisms. Print 00 if the number of graph automorphisms is 11. On each of the next kk lines print a prime pip_i and its power qiq_i separated by a space. The primes must be given in ascending order.

Hint

The graph in the first example has 4=224 = 2^2 automorphisms.

The second example is a simple graph with two vertices and one edge between them, which has 2=212 = 2^1 automorphisms.

The third example is a star graph with a center vertex and 1414 rays, which has 14!=87 178 291 200=211×35×52×72×111×13114! = 87\,178\,291\,200 = 2^{11} \times 3^5 \times 5^2 \times 7^2 \times 11^1 \times 13^1 automorphisms.

Examples3

  1. Example 1

    Input
    15 3
    9 1 2 3 4 5 6 7 8 3
    7 2 9 10 11 12 13 10
    5 2 14 9 15 10
    
    Expected output
    1
    2 2
    
  2. Example 2

    Input
    2 1
    2 1 2
    
    Expected output
    1
    2 1
    
  3. Example 3

    Input
    15 7
    3 1 2 3
    3 4 2 5
    3 6 2 7
    3 8 2 9
    3 10 2 11
    3 12 2 13
    3 14 2 15
    
    Expected output
    6
    2 11
    3 5
    5 2
    7 2
    11 1
    13 1