Closed room mystery

Time limit: 2.5s Memory limit: 512MB Input: Output:

Task

Given a number NN and a matrix FF of dimensions N×NN \times N, we define a set as closed if it is non-empty and for any two elements xx and yy in the set (not necessarily distinct!), Fx,yF_{x,y} is also in the set. The task is to display all subsets of the set {1,2,,N}\{1, 2, \ldots, N\} that are closed and do not strictly contain any other closed set.

Input data

The first line contains the number NN, followed by the matrix FF on the subsequent lines.

Output data

XX, the number of such sets, followed by each set on a new line, sorted in ascending order by the minimum element, and in case of a tie, by size. Additionally, the individual sets should also be sorted in increasing order.

Constraints and clarifications

  • 1N15001 \leq N \leq 1500
  • The values in matrix FF are natural numbers between 11 and NN
  • A set AA strictly contains another set BB if every element of BB belongs to AA and ABA \neq B

Subtasks

  • For 3030 points, N500N \leq 500
  • For the remaining 7070 points, there are no additional constraints.
  • It can be proven that the required sets can be uniquely sorted according to the given criteria.
  • It can be proven that, under the given conditions, the sum of the sizes of the required sets does not exceed 10610^6

Example 1

stdin

5
5 4 1 5 4
5 1 2 4 4
5 3 3 3 2
4 5 5 2 5
2 5 4 4 2

stdout

2
1 2 4 5
3

Explanation

The sets {1,2,4,5}\{1, 2, 4, 5\} and {3}\{3\} do not strictly contain any other closed set. Note that we have not displayed the set {1,2,3,4,5}\{1, 2, 3, 4, 5\} although it is closed because it contains the closed set {3}\{3\}.

Log in or sign up to be able to send submissions!