#P4835. [JSOI2014] 学生选课

[JSOI2014] 学生选课

Description

After entering university, students need to choose courses. There are nn students who need to choose courses, and there are three teachers in the school: JYY, YJY, and YYJ.

In the first year, each student chose one of the teachers. After a year of study, students have formed some impressions of each other. Each student ranks the other n1n - 1 students from best to worst based on their impressions. The second-year course selection begins. Each student needs to choose a teacher, and perhaps because they have been “tricked” too many times, every student wants to switch to a different teacher. You need to schedule the students' course selections so that, among students taking the same class, the worst mutual impression is as good as possible.

Input Format

The first line contains an integer nn.

The next nn lines each contain nn positive integers. Line i+1i + 1 provides the information of student ii. The first integer is AiA_i, which is one of 0,1,20, 1, 2, representing the teacher chosen by student ii in the first year. The next n1n - 1 numbers are a permutation of 1,2,,i1,i+1,,n1, 2, \cdots, i - 1, i + 1, \cdots, n, representing student ii's ranking of the other students from best to worst.

Output Format

Output one integer: the smallest non-negative integer TT such that:

  • Every student chooses a different teacher from the one they chose in the first year.
  • For any two students who choose the same teacher, their impressions of each other are both within the top TT.
6
0 2 3 4 5 6
0 1 3 4 5 6
1 6 5 4 2 1
2 6 5 3 2 1
1 1 2 3 4 6
2 1 2 3 4 5
4

Hint

Sample Explanation 1

The six students chose teachers 1,2,0,0,2,01, 2, 0, 0, 2, 0, respectively.

At this time, in teacher 00's class, student 66 ranks student 44 as the 44-th best, so the answer is T=4T = 4, and there is no smaller TT.

Constraints

For 100%100\% of the testdata, n1000n \le 1000.

Translated by ChatGPT 5