Get to Work (Large)
Time limit5sMemory limit512 MB
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 has employees. The area holds towns where those employees live. Every employee has to reach town , 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 people. The capacity counts the driver, so means the driver can carry nobody else. 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 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 , the number of test cases.
Each test case is given as follows.
- One line with the number of towns in the area and the number of the town where the office is, , separated by a space.
- One line with the number of employees .
- lines, one per employee. Each line holds the number of the town the employee lives in, , and the capacity of the car that employee drives, , separated by a space. If the employee has no licence, is .
Limits
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 is the test case number counting from . Follow it with one of these.
- The string
IMPOSSIBLE, if there are not enough drivers for every employee to commute. - Otherwise space separated integers, one for each town from to , giving the number of cars that commute from that town.