#abc291f. F - Teleporter and Closed off

F - Teleporter and Closed off

Score : 500500 points

问题描述

NN 座城市,依次编号为城市 11、城市 22\ldots 和城市 NN

同时存在一些单向传送器,可以将你传送到不同的城市。从城市 ii (1iN)(1\leq i\leq N) 是否可以直接通过传送器到达另一座城市,由长度为 MM 的字符串 SiS_i 表示,其中包含字符 01。具体来说,对于 1jN1\leq j\leq N

  • 1jiM1\leq j-i\leq M 并且 SiS_i 中的第 (ji)(j-i) 个字符为 1,则有一个传送器可以直接将你从城市 ii 传送到城市 jj
  • 否则,不能直接从城市 ii 传送到城市 jj

求解以下问题,对于 k=2,3,,N1k=2,3,\ldots, N-1

你能否通过重复使用传送器,从城市 11 到达城市 NN 且不经过城市 kk?如果可以,请输出所需的最小传送器使用次数;否则,输出 1-1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN cities numbered city 11, city 22, \ldots, and city NN.
There are also one-way teleporters that send you to different cities. Whether a teleporter can send you directly from city ii (1iN)(1\leq i\leq N) to another is represented by a length-MM string SiS_i consisting of 0 and 1. Specifically, for 1jN1\leq j\leq N,

  • if 1jiM1\leq j-i\leq M and the (ji)(j-i)-th character of SiS_i is 1, then a teleporter can send you directly from city ii to city jj;
  • otherwise, it cannot send you directly from city ii to city jj.

Solve the following problem for k=2,3,,N1k=2,3,\ldots, N-1:

Can you travel from city 11 to city NN without visiting city kk by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print 1-1.

Constraints

  • 3N1053 \leq N \leq 10^5
  • 1M101\leq M\leq 10
  • M<NM<N
  • SiS_i is a string of length MM consisting of 0 and 1.
  • If i+j>Ni+j>N, then the jj-th character of SiS_i is 0.
  • NN and MM are integers.

Input

The input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

Output

Print (N2)(N-2) integers, separated by spaces, in a single line. The ii-th (1iN2)(1\leq i\leq N-2) integer should be the answer to the problem for k=i+1k=i+1.

Sample Input 1

5 2
11
01
11
10
00

Sample Output 1

2 3 2

A teleporter sends you

  • from city 11 to cities 22 and 33;
  • from city 22 to city 44;
  • from city 33 to cities 44 and 55;
  • from city 44 to city 55; and
  • from city 55 to nowhere.

Therefore, there are three paths to travel from city 11 to city 55:

  • path 11 : city 11 \to city 22 \to city 44 \to city 55;
  • path 22 : city 11 \to city 33 \to city 44 \to city 55; and
  • path 33 : city 11 \to city 33 \to city 55.

Among these paths,

  • two paths, path 22 and path 33, do not visit city 22. Among them, path 33 requires the minimum number of teleporter uses (twice).
  • Path 11 is the only path without city 33. It requires using a teleporter three times.
  • Path 33 is the only path without city 44. It requires using a teleporter twice.

Thus, 22, 33, and 22, separated by spaces, should be printed.

Sample Input 2

6 3
101
001
101
000
100
000

Sample Output 2

-1 3 3 -1

The only path from city 11 to city 66 is city 11 \to city 22 \to city 55 \to city 66.
For k=2,5k=2,5, there is no way to travel from city 11 to city 66 without visiting city kk.
For k=3,4k=3,4, the path above satisfies the condition; it requires using a teleporter three times.

Thus, 1-1, 33, 33, and 1-1, separated by spaces, should be printed.

Note that a teleporter is one-way; a teleporter can send you from city 33 to city 44, but not from city 44 to city 33,
so the following path, for example, is invalid: city 11 \to city 44 \to city 33 \to city 66.

update @ 2024/3/10 12:09:18