Matrix multiplication
题号:NC207450
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

    Tazigo is learning linear algebra recently. He developed a strong interest in Matrix Multiplication. As we all know, a matrix can be multiplied by another matrix only if the number of columns of the left matrix is equal to the number of rows of the right matrix. The number of rows of the product matrix is equal to the number of rows of the left matrix, and the number of columns of the product matrix is equal to the number of columns of the right matrix. Given tuple (x, y) stands for the dimension of a matrix, whilex represents the number of rows,y represents the number of columns, the multiplication of matrix A and matrix B can be defined as follows:
      A (a, b) ×B (b, c) = C(a, c)

    Now tazigo would like to ask you whether you can multiply all matrices which are represented by tuple . You can calculate the number in all possible ways to multiply all matrices. Notice that, two matrices are different if and only if their positions in the input sequence are different.

 If there are more than one solution, please print the matrix multiplication sequence represented by dimension tuples, whose order is the least lexicographical order.

输入描述:

The first line is an integer n stands for the number of the matrices.
Every line of the following n lines contains two integer representing the number of rows and the number of columns of the ith matrix.()

输出描述:

Print an integer in the first line, representing the number of all possible ways to multiply all matrices. If the answer exists, the sequence of dimension tuples in the least lexicographical order must be output in the second line.


示例1

输入

复制
2
1 2
3 4

输出

复制
0

说明

Obviously, We can't multiply all matrices. So the answer is zero.

示例2

输入

复制
3
1 2
2 3
3 1

输出

复制
3
1 2 2 3 3 1

说明

There are three ways.

The second line is the sequence of dimension tuples in the least lexicographical order, which is “1 2 2 3 3 1”.

示例3

输入

复制
2
2 2
2 2

输出

复制
2
2 2 2 2