#P10265. [GESP样题 七级] 迷宫统计

[GESP样题 七级] 迷宫统计

Background

Related multiple-choice and true/false problems: https://ti.luogu.com.cn/problemset/1107.

Problem Description

On a mysterious fantasy continent, there are nn ancient and magical mazes, numbered from 11 to nn. Between some mazes, you can travel directly both ways; between some others, you can travel from one maze to another, but cannot come back. The player Xiao Yang wants to challenge different mazes, and he decides to start from maze mm. Now he needs your help to count: how many mazes can directly reach maze mm, how many other mazes maze mm can directly reach, and output the sum of these two numbers.

Note that for maze ii (1in1 \leq i \leq n), it can always directly reach itself.

Input Format

The first line contains two integers nn and mm, representing the total number of maze nodes and the index of the specified starting maze.
The next nn lines each contain nn integers, describing the relations between mazes. For the integer in row ii, column jj, 11 means you can directly go from maze ii to maze jj, and 00 means you cannot directly go.

Output Format

Output one line with three integers separated by spaces: the number of other mazes that maze mm can directly reach, the number of mazes that can directly reach maze mm, and the sum of these two counts.

6 4
1 1 0 1 0 0
0 1 1 0 0 0
1 0 1 0 0 1
0 0 1 1 0 1
0 0 0 1 1 0
1 0 0 0 1 1
3 3 6

Hint

Explanation of Sample 1

Maze 44 can directly reach mazes 3,4,63,4,6, for a total of 33.
Mazes that can directly reach maze 44 are 1,4,51,4,5, for a total of 33.

In total, there are 66.

Constraints

Subtask Score nn \leq
11 3030 1010
22 100100
33 4040 10001000

For all testdata, it is guaranteed that 1mn10001 \leq m \leq n \leq 1000.

Translated by ChatGPT 5