#P10751. [COI 2024] Ministarstvo(征集 checker)
[COI 2024] Ministarstvo(征集 checker)
Background
Source: https://hsin.hr/hio2024/. The translation is from Wenxin Yiyan. If you have a better translation, please provide it in the discussion area.
Problem Description
After a successful career at a party (we will not reveal the party’s name), Pero found a job at the tourism board. He supervises a network of cities, numbered from to , where for every pair of cities there is a one-way road connecting them. To increase revenue, he decided to introduce travel permits. Pero originally wanted to introduce a special permit for every road, but that would attract the attention of his superiors. Therefore, he decided to introduce different permits, numbered from to , and require that traveling on each road needs a specific permit.
To ensure considerable revenue, Pero wants the following property to hold:
- For every city , there exists a city such that it is impossible to travel from city to city using only a single permit.
Pero asks for your help to determine the minimum value of . If there exists a permit assignment that satisfies the property above, output this and such an assignment. If no such assignment exists, output .
Input Format
The first line contains a positive integer .
In the next lines, the -th line contains numbers , where if there is a road from city to city , then . Note that , and for , exactly one of and is non-zero.
Output Format
If there is no assignment satisfying the property, output on the first line, and only on the first line.
Otherwise, output the minimum positive integer on the first line.
In the next lines, output the description of the assignment. The -th line should contain numbers . If , then ; otherwise, , meaning which permit is required to travel on that road.
3
0 1 0
0 0 1
1 0 0
3
0 1 0
0 0 2
3 0 0
3
0 1 1
0 0 1
0 0 0
-1
4
0 1 0 1
0 0 1 1
1 0 0 0
0 0 1 0
3
0 1 0 1
0 0 2 3
3 0 0 0
0 0 2 0
Hint
Sample Explanation
For sample : roads that require the first permit are marked in red, the second permit in blue, and the third permit in green. Starting from city , it is impossible to reach city using only one permit. Starting from city , it is impossible to reach city using only one permit. Starting from city , it is impossible to reach city using only one permit. Starting from city , it is impossible to reach city using only one permit.

Constraints
In all subtasks, . In each subtask, of the points are only for deciding whether such an assignment exists. For these points, if you do not output , you need to output some assignment, but it does not have to satisfy the property Pero wants.
- Subtask 1 (20 points): .
- Subtask 2 (80 points): no additional constraints.
Translated by ChatGPT 5