This page is still under construction.

Parts of this page are still being built. What you see may change.

Print Distribution

Time limit2sMemory limit512 MB

Summary
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 N×MN×M, 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.

  1. First, the teacher gives student SS a stack of KK printouts. Here KK equals the total number of students.
  2. A student who has printouts simultaneously gives every printout except their own to all students in the 88 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.
  3. 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.
  4. Deliveries are made so that every student receives printouts as quickly as possible.
  5. 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 NN, MM and the number of students KK.

The next KK lines give the coordinates XiX_i, YiY_i of student ii. All students have distinct coordinates.

The last line gives the number SS 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

  • 1≤N,M≤5001 ≤ N, M ≤ 500
  • 1≤K≤N×M1 ≤ K ≤ N×M
  • 1≤Xi≤N1 ≤ X_i ≤ N, 1≤Yi≤M1 ≤ Y_i ≤ M (1≤i≤K)(1 ≤ i ≤ K)
  • 1≤S≤K1 ≤ S ≤ K

Examples3

  1. Example 1

    Input
    1 2 2
    1 1
    1 2
    1
    
    Expected output
    2 1
    
  2. Example 2

    Input
    3 3 3
    1 2
    2 1
    3 3
    1
    
    Expected output
    -1
    
  3. Example 3

    Input
    3 5 9
    3 4
    2 4
    2 2
    3 1
    1 4
    1 3
    3 3
    2 5
    1 1
    3
    
    Expected output
    2 1 9 1 1 3 3 1 1