시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB43282775.000%

문제

You work for a company that has E employees working in town T. There are N towns in the area where the employees live. You want to ensure that everyone will be able to make it to work. Some of the employees are drivers and can drive P passengers. A capacity of P == 1 indicates that the driver can only transport themselves to work. You want to ensure that everyone will be able to make it to work and you would like to minimize the number of cars on the road.

You want to calculate the number of cars on the road, with these requirements:

  • Every employee can get to town T.
  • The only way an employee may travel between towns is in a car belonging to an employee.
  • Employees can only take rides from other employees that live in the same town.
  • The minimum number of cars is used.

Find whether it is possible for everyone to make it to work, and if it is, how many cars will end up driving to the office.

입력

One line containing an integer C, the number of test cases in the input file.

For each test case there will be:

  • One line containing the integer N, the number of towns in your area and the integer T, the town where the office is located.
  • One line containing the integer E, the number of employees.
  • E lines, one for each employee, each containing:
    • An integer H >= 1, the home town of the employee, followed by
    • An integer P >= 0, the number of passengers they can drive. If the employee is not licensed to drive the number will be 0.

Limits

  • 1 ≤ T ≤ N
  • 1 ≤ H ≤ N
  • 0 ≤ P ≤ 6
  • C = 50
  • 1 ≤ N ≤ 10
  • 1 ≤ E ≤ 100

출력

  • C lines, one for each test case in the order they occur in the input file, each containing the string "Case #X: " where X is the number of the test case, starting from 1, followed by:
    • The string IMPOSSIBLE, if there are not enough drivers for everyone to commute; OR
    • N space-separated integers, one for each town from 1 to N, which indicate the number of vehicles commuting from the town.

예제 입력 1

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

예제 출력 1

Case #1: 0 0 0 0 0
Case #2: IMPOSSIBLE
Case #3: 1 0 0 1 0

채점 및 기타 정보

  • 예제는 채점하지 않는다.