#C133. Balanced Task Assignment

    ID: 42822 Type: Default 1000ms 256MiB

Balanced Task Assignment

Balanced Task Assignment

You are given a set of tasks and a set of team members. Each task has a unique id and a list of required skills, and each team member has a unique id and a list of skills they possess. Your job is to assign each task to a team member such that:

  • The team member assigned to a task must have all the skills required for that task.
  • The tasks are distributed as evenly as possible among the team members; formally, if a team member i is assigned Ai tasks and team member j is assigned Aj tasks, then \(|A_i - A_j| \le 1\).

If it is impossible to assign all tasks under these constraints, output Impossible.

The challenge requires you to decide if a valid distribution exists and, if so, output the assignment as a JSON object where keys are team member IDs and values are lists of task IDs assigned to them.

inputFormat

The input is provided via standard input (stdin) in the following format:

T M
<task 1>
<task 2>
...
<task T>
<team member 1>
<team member 2>
...
<team member M>

Here, the first line contains two integers T (the number of tasks) and M (the number of team members).

Each of the next T lines describes a task in the following format:

id k skill1 skill2 ... skillk

Where id is the task id (an integer), k is the number of skills required and then k strings representing the skills.

Each of the next M lines describes a team member in the following format:

id k skill1 skill2 ... skillk

Where id is the member id (a string), k is the number of skills the member has and then k strings representing the skills.

outputFormat

Output the assignment on standard output (stdout) as a JSON object. The keys are team member ids and the values are lists of task ids assigned to that team member. If it is impossible to assign all tasks according to the requirements, output exactly the string Impossible.

For example, a valid output might be:

{"A": [1, 4], "B": [3], "C": [2]}
## sample
4 3
1 2 python sql
2 1 javascript
3 1 python
4 1 sql
A 2 python sql
B 1 python
C 2 javascript sql
{"A": [1, 4], "B": [3], "C": [2]}