Report for Investors
Time limit3sMemory limit1024 MB
Each consultant picks one of their improvements so that the reports concatenated up the tree form an increasing sequence, or the task reports that it is impossible.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, Bit manipulation, Sorting
- Solved
- No attempts yet
Statement
The company has chipmunks (), numbered from 1 to . Chipmunk 1 is the founder, and every other chipmunk has exactly one direct manager. The hierarchy is therefore a rooted tree in which a parent is the manager of its children. A chipmunk with subordinates is a manager, and the rest are consultants. Each manager has at most 8 direct subordinates. The founder is also a manager.
The founder wants to write a report for investors about the latest improvements to the product. Each improvement is the work of one consultant. The improvements are numbered in the order they were made.
Each consultant has a list of improvements they made. A consultant must pick one of them and report it to their manager, so each consultant's report consists of exactly one improvement.
Each manager, including the founder, combines the reports from their subordinates. They write the reports one after another, unchanged, in any order. For example, if the first report is [1, 3] and the second is [2, 4, 10], the combined report can be [2, 4, 10, 1, 3] or [1, 3, 2, 4, 10], and no other result is possible.
The founder wants the report to be as logical as possible, so the improvements in it must appear in chronological order, meaning increasing numbers. Choose which improvement each consultant reports, and choose the order in which each manager concatenates the reports of its subordinates, so that the improvements in the founder's final report appear in increasing order.
Input format
The first line contains an integer (), the number of chipmunks. Then descriptions follow, one per line:
- If the chipmunk is a manager, the line starts with 1, then (), the number of direct subordinates, followed by distinct numbers from 2 to , the subordinates' numbers.
- If the chipmunk is a consultant, the line starts with 2, then , the number of improvements the consultant can report, followed by distinct integers from 1 to 100000, the improvement numbers.
Chipmunk 1 is always a manager. Every other chipmunk appears exactly once as someone's subordinate, and each one is directly or indirectly subordinate to chipmunk 1.
The sum of all does not exceed 100000. No improvement was made by two different consultants, so all improvement numbers listed by the consultants are distinct.
Output format
If the report cannot be formed as required, print No.
If it can be formed, print Yes. You may then print a certificate that describes, for each chipmunk in order of their numbers:
- For a manager: the list of its subordinates, in the order their reports must be placed.
- For a consultant: the number of the improvement the consultant should report.
This problem has a special property: you may print the certificate or omit it. A solution that omits the certificate but correctly determines whether the report can be formed gets partial credit.
Be careful: printing an incorrect certificate gives a Wrong Answer verdict and zero points for that test, even if the Yes/No answer is correct.
Hints
The second sample omits the certificate, which corresponds to a partial solution.
In the third sample, each consultant made exactly one improvement, so the choice of improvements is fixed.
The third manager can form the report [1, 3] or [3, 1].
The first manager has four possible reports, taking into account the possibilities for the third manager's report: [1, 3] + [2] = [1, 3, 2], [2] + [1, 3] = [2, 1, 3], [3, 1] + [2] = [3, 1, 2], and [2] + [3, 1] = [2, 3, 1]. None of these reports lists the improvements in chronological order.