Given a tournament graph on N players, find the lexicographically smallest longest path starting at player 1.
Medium7GraphGreedyDynamic programmingImplementationNo attempts yetTime limit1sMemory limit256 MBThe badminton club KUBC at Korea University ran a league among its full members. N members took part, Taeyang among them, every pair met once, and the league had N(N−1)/2 matches in total. No match ended in a draw, so every match had a winner and the league produced a full ranking.
The picture below is the result grid and the ranking of a league with five players. The picture is written in Korean, and the players, in the order the rows appear, are Taeyang, Chanwoo, Sechan, Hyunsu and Hanyong.

Hyunsu looked at the ranking and screamed. "I am tied for last? Last place... no, me, in last place! What is this!" Then he started talking himself out of it. "I beat Hanyong, Hanyong beat Sechan, Sechan beat Chanwoo, Chanwoo beat Taeyang... so I beat everyone else after all!"
Taeyang is tied for last with Hyunsu and wants to talk himself out of it the same way, but he cannot find a chain that links the other four players. Help Taeyang and write a program that builds the chain.
The first line contains the number of full members N. (2≤N≤2000)
Each of the next N lines holds one row of the result grid. The q-th number on line p+1 follows these rules.
There are no draws, so for p=q exactly one of the two entries at (p,q) and (q,p) is 1. Taeyang is player 1.
On the first line print the maximum length L of the chain.
On the second line print that chain S1 S2 … SL, separated by spaces. The chain must satisfy every rule below.
If several chains of length L exist, print the lexicographically smallest one.