#P198. [Sleeping Cup #7] Garbage Collection
[Sleeping Cup #7] Garbage Collection
Person in Charge
Attention
The sample cases for this problem are not located in subtask 0. Instead, they are placed elsewhere:
| Sample | Data File | Subtask ID | Test Point |
|---|---|---|---|
| Sample 1 | 1.in / 1.out |
Subtask 1 | #1-1 |
6.in / 6.out |
Subtask 2 | #2-1 | |
10.in / 10.out |
Subtask 3 | #3-1 | |
| Sample 2 | 14.in / 14.out |
Subtask 4 | #4-1 |
Sample 1 will be judged three times.
Subtask dependencies are as follows:
| Subtask ID | Depends on |
|---|---|
| Subtask 1 | none |
| Subtask 2 | |
| Subtask 3 | |
| Subtask 4 | Subtasks 1, 2, and 3 |
File I/O is required (garbage.in / garbage.out).
In this problem, two cleanup sequences and are considered essentially different if and only if at least one of the following holds:
- , or
- there exists such that .
Problem Background
Sleeping Goose is the chief administrator of the garbage stations in the Kingdom of Sleeping Cup.
In recent years, as the number of garbage stations keeps increasing, Sleeping Goose feels more and more overwhelmed.
To better manage the kingdom’s garbage stations, it has hired some employees.
Unfortunately, these employees are often derelict in their duties…
Problem Description
The Kingdom of Sleeping Cup has garbage stations.
- Station is the central station.
- Stations are branch stations.
Each branch station has exactly one direct superior station whose number is strictly smaller than itself.
This morning, Sleeping Goose checked the surveillance of all stations, counted the number of garbage bags currently stored at each station (all positive integers not exceeding ), and decided to let its employees perform a major cleanup.
According to the on-the-job training, Sleeping Goose must provide an operation sequence with , and the employees will, in ascending order , perform the following for each :
- If , do nothing.
- Otherwise, transport all garbage from station to its direct superior station.
After the employees finish, all garbage in station will be taken away by Sleeping Goose itself to the waste treatment plant.
Unfortunately, as mentioned in the background, the employees are very lazy. Specifically, they will actually execute the following lazy version of the procedure:
- If , do nothing.
- If station currently contains bags, do nothing.
- Otherwise, leave exactly bags in station and transport the remaining bags to its direct superior station.
Nevertheless, Sleeping Goose still wants to maximize the amount of garbage cleaned up while using the shortest possible sequence of operations.
To help Sleeping Goose, you need to answer three questions:
- What is the maximum number of garbage bags that can be cleaned up in this cleanup?
- Under the condition that the amount cleaned is maximized, what is the minimum length of the operation sequence?
- Under the above two conditions (maximum bags cleaned and shortest sequence), how many essentially different cleanup plans are available?
- The answer to the first question is not taken modulo.
- The answer to the second question is not taken modulo.
- The answer to the third question is modulo .
Input Format
Multiple test cases.
First line: an integer , the number of test cases.
For each test case:
lines, each containing two positive integers :
- — the direct superior station of station (for , ).
- — the number of garbage bags currently stored at station ().
Special note: For , the line actually contains and (i.e., the first line’s two integers are and ).
Output Format
For each test case, output one line with three integers:
- The answer to the first question (no modulo).
- The answer to the second question (no modulo).
- The answer to the third question (modulo ).
Samples
2
1 1
2 1
1 1
1 0 1
1 0 1
2
6 6
1 2
1 2
2 3
2 3
2 3
13 10
1 1
2 2
3 3
2 4
3 2
4 3
3 4
4 5
5 3
4 4
5 5
6 6
8 2 3
14 3 1
Sample 2 Explanation
For the first test case, the three optimal cleanup plans are:
- Clean one of stations , , or ( choices), transporting bags and leaving bag behind, so station has bags.
- Clean station , transporting bags and leaving bags behind, so station has bags.
The three operation sequences are: .
Note that the following six sequences, though they achieve the maximum bags cleaned, do not satisfy the shortest-sequence requirement, hence they are not counted in the answer to the third question:
- After choosing one of , , and cleaning it ( choices), leaving bag, station has bags.
- Then choose another of the remaining two ( choices), leaving bags, station has bags.
- Finally clean station , transporting bags, leaving bags, station has bags.
For the second test case, the only optimal sequence is .
Data Range
For of the data: , .
There are four equally-weighted subtasks:
- Guaranteed for all .
- Guaranteed for all .
- Guaranteed for all .
- No additional constraints.
相关
在下列比赛中: