P29949 [洛谷] [ROI 2018] Viruses
普及+
题目描述

Problem Background

Translated from ROI 2018 Day1 T2. Вирусы (Viruses).

Problem Description

There are $n$ cells and $n$ viruses. The initial virus of the $i$-th cell is also the $i$-th virus. Each cell has a certain susceptibility level to each virus.

Cells can attack each other. If cell A attacks cell B, and B's susceptibility to A's current virus is higher than its susceptibility to its own virus, then B will be infected by A's virus (becoming a cell of A's virus).

Cells can arrange their attack order arbitrarily. The game ends if and only if no cell can be infected by any virus.

If there exists an attack order such that virus $i$ eventually has one or more cells, then we call virus $i$ a "feasible virus". If for any attack order, virus $i$ eventually has one or more cells, then we call virus $i$ a "stable virus".

The viruses want to know how many feasible and stable viruses there are.

Input Format

The first line contains a positive integer $n$, representing the number of cells and viruses.

The next $n$ lines, each containing $n$ positive integers, represent the virus sequence sorted by the susceptibility level in descending order for each cell.

For example, if the input is 2 1 3, it means that the cell has the highest susceptibility to virus 2, the second highest to virus 1, and the lowest to virus 3.

The last line contains an integer $p$.

Output Format

The first line contains an integer $x$. If $p=1$, output the number of stable viruses; if $p=2$, output the number of feasible viruses.

The next line contains $x$ integers, representing the IDs of these $x$ viruses, sorted in ascending order.

Sample Input and Output

Sample Input #1

2
1 2
2 1
1

Sample Output #1

2
1 2

Sample Input #2

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

Sample Output #2

1
3

Sample Input #3

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

Sample Output #3

3
1 3 4

Notes

For all data, $1 \leq n \leq 500$.

Subtask Number $n$ $p$
1 $1 \leq n \leq 5$ $p = 1$
2 $1 \leq n \leq 500$ $p = 1$
3 $1 \leq n \leq 5$ $p = 1,2$
4 $1 \leq n \leq 50$ $p = 1,2$
5 $1 \leq n \leq 500$ $p = 1,2$
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 1 2 2 1 1
样例输出
2 1 2