KUBC League (Small)
Time limit1sMemory limit256 MB
Given a tournament on N players, find the longest simple path starting at player 1 and output the lexicographically smallest such path.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Backtracking, Brute force
- Solved
- No attempts yet
Problem
KUBC, the badminton club of Korea University, ran a league for its full members. players including Taeyang entered, every pair played exactly once, so the league had matches. No match ended in a draw, so the results produced a standings table.

Hyunsu looked at the table and screamed. 'I am tied for last? Last place... no, me, in last place! What is this! Aaaargh.' Then he started to rationalize. 'I beat Hanyong, Hanyong beat Sechan, Sechan beat Chanwoo, and Chanwoo beat Taeyang. So I beat everyone else after all!'
Taeyang is tied for last as well and wants to argue the same way, but he cannot find a way to beat the other 4 players. Help Taeyang and write a program that builds a chain of players, where each player in the chain beat the next one.
Input
The first line contains . ()
Each of the next lines contains integers. The -th number on line follows the rules below.
- It is 1 if player beat player .
- It is 0 if player lost to player .
- It is 0 if .
For two different players and , exactly one of the two numbers is 1. Taeyang is player 1.
Output
On the first line, print the maximum length of the chain.
On the second line, print the chain separated by spaces. The chain must satisfy every condition below.
- .
- are pairwise different.
- For every with , player beat player . Player does not have to beat player .
If several chains have length , print the lexicographically smallest one, comparing the numbers from the front.