#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 NN cities, numbered from 11 to NN, 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 KK different permits, numbered from 11 to KK, 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 vv, there exists a city uu such that it is impossible to travel from city vv to city uu using only a single permit.

Pero asks for your help to determine the minimum value of KK. If there exists a permit assignment that satisfies the property above, output this KK and such an assignment. If no such assignment exists, output 1-1.

Input Format

The first line contains a positive integer NN.

In the next NN lines, the ii-th line contains NN numbers ai,ja_{i,j}, where if there is a road from city ii to city jj, then ai,j=1a_{i,j} = 1. Note that ai,i=0a_{i,i} = 0, and for iji \neq j, exactly one of ai,ja_{i,j} and aj,ia_{j,i} is non-zero.

Output Format

If there is no assignment satisfying the property, output 1-1 on the first line, and only on the first line.

Otherwise, output the minimum positive integer KK on the first line.

In the next NN lines, output the description of the assignment. The ii-th line should contain NN numbers bi,jb_{i,j}. If ai,j=0a_{i,j} = 0, then bi,j=0b_{i,j} = 0; otherwise, 1bi,jK1 \leq b_{i,j} \leq K, 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 33: roads that require the first permit are marked in red, the second permit in blue, and the third permit in green. Starting from city 11, it is impossible to reach city 33 using only one permit. Starting from city 22, it is impossible to reach city 11 using only one permit. Starting from city 33, it is impossible to reach city 22 using only one permit. Starting from city 44, it is impossible to reach city 11 using only one permit.

Constraints

In all subtasks, 2N10002 \leq N \leq 1000. In each subtask, 15%15\% of the points are only for deciding whether such an assignment exists. For these points, if you do not output 1-1, you need to output some assignment, but it does not have to satisfy the property Pero wants.

  • Subtask 1 (20 points): N5N \leq 5.
  • Subtask 2 (80 points): no additional constraints.

Translated by ChatGPT 5