Croatian Olympiad in Informatics April $28^{st}$ 2024 After a successful career in a party we won’t name, Pero got a job at the Ministry of Tourism. Pero oversees a network of $N$ cities, labeled with numbers from 1 to $N$, where there is exactly one, one-way road between each pair of cities. In order to increase revenue, he has decided to introduce permits for traffic. Pero would prefer to introduce a special permit for each road, but that would alert his superiors. Therefore, he will introduce $K$ different permits, labeled from 1 to $K$, and possession of a specific permit will be required to travel on each road. To still ensure substantial revenue, Pero will settle for the following property.
- For each city $v$, there is some city $u$, such that it is not possible to travel from city $v$ to city $u$ with just one permit. Pero asks for your help to determine the minimum $K$ such that there exists an assignment of permits with the desired property, if such an assignment exists! If no such assignment exists, output -1.
Input
The first line contains a positive integer $N$. The $i$-th of the following $N$ lines contains $N$ numbers ai,j where ai,$j = 1$ if there is a road from city $i$ to city $j$. Note that $a_{i,i} = 0$ and for $i$̸ = $j$, exactly one of the numbers $a_{i,j}$ and $a_{j,i}$ is non-zero.
Output
If there is no assignment with the desired property, output -1 in the first and only line. Otherwise, output the minimum positive integer $K$ in the first line. In the following $N$ lines, output the description of the assignment. In the $i$-th line, output $N$ numbers $b_{i,j}$ where if $a_{i,j} = 0$, then $b_{i,j} = 0$, otherwise $1 \le b_{i,j} \le K$ indicating which permit is required for traveling on that road.
Constraints
In all subtasks, $2 \le N \le 1000$. In each subtask, 15% of the points come from only deciding whether such an assignment exists or not. For these points, if you don’t output -1, you need to output some assignment, but it doesn’t have to satisfy Pero’s desired property. Subtask Score Constraints 1 20 $N \le 5$ 2 80 No additional constraints. Croatian Olympiad in Informatics April $28^{st}$ 2024
Example 1
3 0 1 0 0 0 1 1 0 0
3 0 1 0 0 0 2 3 0 0
Example 2
3 0 1 1 0 0 1 0 0 0
-1
Example 3
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
Explanation for the third sample test: Roads requiring the first permit are marked in red, the second permit in blue, and the third permit in green. From city 1, it is not possible to reach city 3 using just one permit. From city 2, it is not possible to reach city 1 using just one permit. From city 3, it is not possible to reach city 2 using just one permit. From city 4, it is not possible to reach city 1 using just one permit.
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.