This page is still under construction.

Parts of this page are still being built. What you see may change.

ICPC Strikes Again

Time limit1sMemory limit128 MB

Summary
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 XX depends on a task YY if either (i) XX depends directly on YY, or (ii) there is a task ZZ such that XX depends directly on ZZ and ZZ depends on YY. 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 TT is defined as the basic significance of TT plus the significance of every task that depends directly on TT. In particular, if no task depends directly on TT, 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 WW performs a task XX, the significance of XX is added to WW's salary exactly when there is no other task YY that WW also performs and on which XX 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 TT and EE: the number of tasks and the number of employees (1≤T≤10001 \le T \le 1000 and 1≤E≤10001 \le E \le 1000). Tasks are numbered from 11 to TT and employees from 11 to EE.

Then follow the descriptions of tasks 11 to TT, in ascending order. Each task is described by two lines. The first line contains three integers BSBS, NDND, and NENE: the basic significance of the task, the number of tasks that depend directly on it, and the number of employees who perform it (1≤BS≤10001 \le BS \le 1000, 0≤ND<T0 \le ND < T, and 1≤NE≤E1 \le NE \le E). The second line contains ND+NEND + NE integers: first the NDND tasks that depend directly on this task, then the NENE employees who perform it.

Input ends with a line containing T=E=0T = E = 0, 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 ii, a line with two integers ii and ss separated by a single space, meaning that employee ii has a salary of ss.

Examples3

  1. Example 1

    Input
    3 2
    100 2 2
    2 3 1 2
    40 0 1
    1
    60 0 1
    2
    7 2
    10 2 1
    2 3 1
    10 2 1
    4 5 2
    10 2 1
    6 7 2
    10 0 1
    1
    10 0 1
    1
    10 0 1
    1
    10 0 1
    1
    0 0
    
    Expected output
    *****
    1 200
    2 200
    *****
    1 70
    2 60
    
  2. Example 2

    Input
    1 1
    5 0 1
    1
    0 0
    
    Expected output
    *****
    1 5
    
  3. Example 3

    Input
    2 3
    10 1 1
    2 1
    20 0 1
    2
    0 0
    
    Expected output
    *****
    1 30
    2 20
    3 0