QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 18

#14005. Get to Work

統計

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.

Input

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.

Output

  • 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.

Limits

  • 1 ≤ TN
  • 1 ≤ HN
  • 0 ≤ P ≤ 6

Small Dataset (9 points)

  • C = 50
  • 1 ≤ N ≤ 10
  • 1 ≤ E ≤ 100

Large Dataset (9 points)

  • C = 100
  • 1 ≤ N ≤ 100
  • 1 ≤ E ≤ 500

Example

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

Output

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

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.