atcoder#ABC291F. [ABC291F] Teleporter and Closed off
[ABC291F] Teleporter and Closed off
Score : points
Problem Statement
There are cities numbered city , city , , and city .
There are also one-way teleporters that send you to different cities.
Whether a teleporter can send you directly from city to another is represented by a length- string consisting of 0
and 1
. Specifically, for ,
- if and the -th character of is
1
, then a teleporter can send you directly from city to city ; - otherwise, it cannot send you directly from city to city .
Solve the following problem for :
Can you travel from city to city without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print .
Constraints
- $M
- is a string of length consisting of
0
and1
. - If , then the -th character of is
0
. - and are integers.
Input
The input is given from Standard Input in the following format:
Output
Print integers, separated by spaces, in a single line. The -th integer should be the answer to the problem for .
5 2
11
01
11
10
00
2 3 2
A teleporter sends you
- from city to cities and ;
- from city to city ;
- from city to cities and ;
- from city to city ; and
- from city to nowhere.
Therefore, there are three paths to travel from city to city :
- path : city city city city ;
- path : city city city city ; and
- path : city city city .
Among these paths,
- two paths, path and path , do not visit city . Among them, path requires the minimum number of teleporter uses (twice).
- Path is the only path without city . It requires using a teleporter three times.
- Path is the only path without city . It requires using a teleporter twice.
Thus, , , and , separated by spaces, should be printed.
6 3
101
001
101
000
100
000
-1 3 3 -1
The only path from city to city is city city city city . For , there is no way to travel from city to city without visiting city . For , the path above satisfies the condition; it requires using a teleporter three times.
Thus, , , , and , separated by spaces, should be printed.
Note that a teleporter is one-way; a teleporter can send you from city to city , but not from city to city , so the following path, for example, is invalid: city city city city .