#P7268. Power Source Circuit Connection

    ID: 20468 Type: Default 1000ms 256MiB

Power Source Circuit Connection

Power Source Circuit Connection

Given a grid defined by an $(n-1)\times (n-1)$ square, its intersection points form $n\times n$ nodes. The nodes are numbered from 1 to $n^2$ from left to right and top to bottom. Some of these nodes are designated as power sources. For each power source that is not already on the boundary of the grid, you are required to find a path (circuit connection) starting from the power source and ending at a node on the boundary (i.e. on the top, bottom, left or right edge) such that the path does not pass through any other power source.

Note: If a power source is already located on the boundary, no connection is needed for that source.

Path requirements:

  • The path must start at a power source (which is strictly inside the grid) and end at a node on the boundary.
  • Except for the starting node, the path must not include any other power source.
  • You may output any valid set of connections if multiple solutions exist.

The grid is interpreted as a graph where each node (with coordinates computed by \( row = \lfloor (id-1)/n \rfloor \) and \( col = (id-1) \% n \)) is adjacent to its four neighbors (up, down, left, right) if they exist.

inputFormat

The input consists of two lines:

  1. The first line contains two integers n and k, where n specifies that the grid has \( n \times n \) nodes, and k denotes the number of power sources.
  2. The second line contains k space-separated integers, each representing the id of a power source. The nodes are numbered from 1 to \( n^2 \) in row-major order.

outputFormat

For each power source that is not on the boundary, output a circuit connection. The output should be formatted as follows:

  1. First, print an integer m which is the number of connections (i.e. the number of inside power sources that need a connection).
  2. Then for each connection, print a line beginning with an integer l (the length of the connection path) followed by l space-separated integers representing the node ids in the order of the path. The path must start with the power source and end at a boundary node, and none of the intermediate nodes (except the starting one) can be a power source.

sample

3 1
5
1

2 5 2

</p>