루미 알고리즘

#1603플래티넘Claw Machine

PO 2025 결선코딩출제 Olle Lapidus영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Olivia and Anton are at Liseberg, where they've found a claw machine with N stuffed animals, numbered from 1 to N. Olivia is highly skilled and wants to show off her abilities to Anton.

The two-dimensional claw machine can be modeled as a grid of size R × C, with R rows and C columns. The stuffed animals are packed tightly, with each cell of the grid belonging to one stuffed animal. There are no empty cells in the grid. A stuffed animal may consist of several connected cells in the grid, forming a contiguous shape. In other words, you can draw each stuffed animal with a thick pen without lifting it from the paper.

In a single move, Olivia can choose a stuffed animal that does not have any other stuffed animal directly above it and lift it out of the claw machine. Since Olivia hasn't asked Anton about his favorite animal yet, she wants to know, for each stuffed animal, how many moves she will need to make in total before she can lift it.

Input

The first line contains the integers N, R, and C (1 ≤ N ≤ R × C ≤ 4 × 10^5).

The next R lines describe the grid. The first line corresponds to the top of the claw machine, and the last line corresponds to the bottom. The i-th rows contains C integers a_{i,1}, a_{i,2}, ..., a_{i,C} (1 ≤ a_{i,j} ≤ N).

Stuffed animal number x consists of all cells (i, j) such that a_{i,j} = x. It is guaranteed that each stuffed animal consists of at least one cell and forms a contiguous region.

Output

For each stuffed animal 1, 2, ..., N, print the number of moves Olivia needs to make before she can lift that animal. If it is not possible, print -1.

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

GroupPointsConstraints
1101 ≤ N ≤ R × C ≤ 20
25C = 1
310C ≤ 2
415Each stuffed animal has a rectangular shape.
515N ≤ 2000
67N ≤ 50000
738No additional constraints.

Explanation of Sample 1

In sample 1, the claw machine looks like this:

(그림은 원문 저장소에서 볼 수 있습니다)

To lift stuffed animal 1, no other animal needs to be lifted first, so the answer is 0. To lift stuffed animal 2, Olivia first needs to lift 3, 4, 6, and 7, so the answer is 4. To lift stuffed animal 3, no other animal needs to be lifted first. To lift stuffed animal 4, Olivia needs to lift 3 and 6 first, so the answer is 2. The answers for stuffed animals 5, 6, and 7 are 3, 0, and 3, respectively.

Here is how it looks after lifting stuffed animal 3:

(그림은 원문 저장소에서 볼 수 있습니다)

Explanation of Sample 4

Neither stuffed animal 1 nor 2 has anything above them, so the answers for both are 0. To lift stuffed animal 3, Olivia first needs to lift 4, but to lift 4, she needs to lift 3, creating a loop. Thus, the answer for both 3 and 4 is -1, as it is impossible to lift them.

예제

예제 1

입력
7 4 4
1 3 3 6
1 4 4 4
1 7 4 5
1 2 4 5
출력
0 4 0 2 3 0 3 

예제 2

입력
4 5 1
2
1
1
3
4
출력
1 0 2 3 

예제 3

입력
5 4 5
1 1 2 2 3
1 1 2 2 3
4 2 2 2 3
4 2 5 2 3
출력
0 1 0 1 2 

예제 4

입력
4 4 4
1 1 2 2
3 3 3 2
3 4 3 2
3 3 3 2
출력
0 0 -1 -1 

출처 표기

출처: Programmeringsolympiaden 2025년 결선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.