#P3561. [POI 2017] Turysta

[POI 2017] Turysta

Problem Description

You are given a directed graph with nn vertices. Between any two vertices, there is exactly one directed edge.

For each vertex vv, find a simple path starting from vv that visits the maximum number of vertices, and does not visit any vertex twice or more.

Input Format

The first line contains a positive integer nn, the number of vertices.

The next n1n-1 lines follow. The ii-th of these lines contains i1i-1 numbers.

If the jj-th number is 11, it means there is a directed edge ji+1j\rightarrow i+1. If it is 00, it means there is a directed edge ji+1j\leftarrow i+1.

Output Format

Output nn lines. On the ii-th line, first output a positive integer kk, the number of vertices on an optimal path starting from vertex ii.

Then output kk positive integers, in order, representing each vertex on the path.

If there are multiple optimal solutions, output any one.

This problem uses SPJ (made by Claris).

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

Hint

For 100%100\% of the testdata, 2n2×1032\le n\le 2 \times 10^3.

Translated by ChatGPT 5