This page is still under construction.

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

Get to Work (Large)

Time limit5sMemory limit512 MB

Summary
For each town, count how many drivers and passengers must leave; report per-town car counts or IMPOSSIBLE if seats do not cover riders.
Level

Medium5 of 10

Topics
Greedy, Implementation
Solved
No attempts yet

Problem

A company in town TT has EE employees. The area holds NN towns where those employees live. Every employee has to reach town TT, and you want as few cars on the road as possible.

The rules are these.

  • The only way an employee travels between towns is in a car owned by an employee.
  • An employee can only ride with an employee who lives in the same town.
  • A driving employee drives a car with a capacity of PP people. The capacity counts the driver, so P=1P = 1 means the driver can carry nobody else. P=0P = 0 means the employee has no licence and cannot drive.
  • The number of cars used has to be the smallest possible.

An employee who lives in town TT is already at the office and needs no car.

Decide whether every employee can get to work, and if so, how many cars leave each town for the office.

Input

The first line holds an integer CC, the number of test cases.

Each test case is given as follows.

  • One line with the number of towns in the area NN and the number of the town where the office is, TT, separated by a space.
  • One line with the number of employees EE.
  • EE lines, one per employee. Each line holds the number of the town the employee lives in, HH, and the capacity of the car that employee drives, PP, separated by a space. If the employee has no licence, PP is 00.

Limits

  • 1≤C≤1001 \le C \le 100
  • 1≤N≤1001 \le N \le 100
  • 1≤T≤N1 \le T \le N
  • 1≤E≤5001 \le E \le 500
  • 1≤H≤N1 \le H \le N
  • 0≤P≤60 \le P \le 6

Output

Print one line per test case, in the order the test cases appear in the input. Each line starts with the string Case #X: , where XX is the test case number counting from 11. Follow it with one of these.

  • The string IMPOSSIBLE, if there are not enough drivers for every employee to commute.
  • Otherwise NN space separated integers, one for each town from 11 to NN, giving the number of cars that commute from that town.

Examples2

  1. Example 1

    Input
    3
    5 1
    3
    1 0
    1 0
    1 0
    5 1
    3
    2 4
    2 0
    3 0
    5 3
    5
    1 2
    1 0
    4 2
    4 4
    4 0
    
    Expected output
    Case #1: 0 0 0 0 0
    Case #2: IMPOSSIBLE
    Case #3: 1 0 0 1 0
    
  2. Example 2

    Input
    1
    1 1
    1
    1 0
    
    Expected output
    Case #1: 0