Print Distribution
Time limit2sMemory limit512 MB
Given student positions on an N by M grid and a starting student, compute how many printouts each student must pass on so everyone ends with exactly one, or print -1.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Greedy, Simulation
- Solved
- No attempts yet
Problem
A classroom is a rectangle of size , and each cell holds one desk. The students of Bukgwago sit wherever they like: crowded at the back, or scattered apart from each other. The teacher often hands out class materials as printouts, but because the students are seated irregularly, ordinary methods such as passing printouts row by row cannot deliver a printout to every student. So the teacher and the students distribute printouts as follows.
- First, the teacher gives student a stack of printouts. Here equals the total number of students.
- A student who has printouts simultaneously gives every printout except their own to all students in the cells adjacent to them horizontally, vertically, or diagonally who have not yet received a printout. The time this takes is constant regardless of the number of printouts.
- If a student can receive printouts from two or more students at the same time, they always receive only from the student with the smallest number.
- Deliveries are made so that every student receives printouts as quickly as possible.
- The process repeats until every student holds exactly one printout.
With this method, however, some students may fail to receive a printout or printouts may be left over, depending on how many printouts each student receives. Find the number of printouts each student must receive so that every student can receive one, and tell the students in advance.

Blue is the number of printouts each student received, red is the number of printouts being delivered.
The teacher has given student 3 a stack of 9 printouts.
Student 3 hands them out incorrectly, so student 4 is left with extra printouts and student 8 receives none.

A correct distribution. Every student holds one printout.
Input
The first line gives the size of the classroom , and the number of students .
The next lines give the coordinates , of student . All students have distinct coordinates.
The last line gives the number of the student who received printouts from the teacher.
Output
If any student cannot receive a printout, print -1 and terminate. Otherwise, print the number of printouts each student must receive, in order of student number.
Constraints
- ,