ICPC Strikes Again
Time limit1sMemory limit128 MB
Given a DAG of task dependencies, basic significances, and which employees perform which tasks, compute each employee's salary as the sum of significances of tasks they perform that no other task they perform depends on.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Topological sort, Dynamic programming
- Solved
- No attempts yet
Problem
International Concrete Projects Company (ICPC) is a construction company that specializes in building houses for the high-end market. Thanks to a very efficient land-division method it has used in its housing projects since last year, it is the most profitable company in the world.
Recently there was chaos at ICPC: employees refused to work, arguing that they were not paid enough. Worried about losing profit to the strike, the board proposed a new way to compute salaries, which everyone happily accepted.
A worker's salary reflects how significant the tasks he or she performs are, and this significance depends on how the tasks relate to one another.
We say a task depends on a task if either (i) depends directly on , or (ii) there is a task such that depends directly on and depends on . Because every task at ICPC must be completed, the dependence relation contains no cycles. A single task may also be performed by more than one worker.
Each task has a basic significance that reflects its importance (for example, developing the efficient land-division method matters more than building the houses themselves). The significance of a task is defined as the basic significance of plus the significance of every task that depends directly on . In particular, if no task depends directly on , then its significance equals its basic significance.
A worker's salary is the sum of the significances of all the tasks the worker performs that do not depend on any other task the same worker performs. In other words, if a worker performs a task , the significance of is added to 's salary exactly when there is no other task that also performs and on which depends.
Help ICPC determine the salary of each employee.
Input
The input contains several test cases.
The first line of a test case contains two integers and : the number of tasks and the number of employees ( and ). Tasks are numbered from to and employees from to .
Then follow the descriptions of tasks to , in ascending order. Each task is described by two lines. The first line contains three integers , , and : the basic significance of the task, the number of tasks that depend directly on it, and the number of employees who perform it (, , and ). The second line contains integers: first the tasks that depend directly on this task, then the employees who perform it.
Input ends with a line containing , which must not be processed.
Output
Answer the test cases in the order they are given. For each test case print:
- a single line containing five stars
*****, marking the start of the case; - for each employee , a line with two integers and separated by a single space, meaning that employee has a salary of .