#P10537. [APIO2024] 九月
[APIO2024] 九月
Problem Description
In the central square of Hangzhou, there is a famous ancient tree. This ancient tree can be viewed as a rooted tree with nodes, numbered from to , where node is the root.
A node with no children is called a leaf. Each time the tree sheds leaves, it chooses one current leaf node and deletes it. In one day, the tree may shed leaves multiple times.
There are volunteers (numbered from to ) who take care of the ancient tree. Each volunteer independently records the leaf-shedding situation of this year in the following way:
Each day, they collect the numbers of all newly fallen leaves (that is, the numbers of nodes deleted that day), and then write them in an arbitrary order after the previously written leaf numbers.
For example, on the first day, leaves and fall, so they write down or . On the second day, leaves and fall, so they continue by writing or . The final record may be any one of , , , or .
This process continues for days, with new leaves falling every day, until only the root node remains.
During your journey, you pass through Hangzhou. Now it is already midwinter. Looking up at the bare branches of the ancient tree, you cannot help but recall the beautiful scene of falling leaves.
You want to know on how many days it was possible to see leaves falling this year, but you can only find the records of the volunteers. Try to infer the maximum possible value of from these records.
Interaction
Please do not submit using C++14(GCC9).
You do not need to include the library september.h at the beginning of your program.
You only need to implement the following function:
int solve(int N, int M, std::vector<int> F,
std::vector<std::vector<int>> S);
- : the number of nodes of the ancient tree.
- : the number of volunteers.
- : an array of length . For , is the index of the parent of node . is always .
- : an array of length . Each element of is an array of length . is the -th node number recorded by volunteer (starting from ).
- This function must return an integer, meaning the maximum possible value of according to the rules above (i.e. the maximum possible number of days with leaf shedding).
- For each test case, the interaction library may call this function more than once. Each call should be handled as a new, independent situation.
Note: Since the function may be called multiple times, you should be careful about the effect of leftover data from previous calls on later calls, especially the state of global variables.
Input Format
The sample grader reads input in the following format:
- Line :
For each of the next test cases:
- Line :
- Line :
- Line :
Output Format
The sample grader prints your answer in the following format:
For each test case:
- Line : the return value of function
solve.
1
3 1
0 0
1 2
2
1
5 2
0 0 1 1
1 2 3 4
4 1 2 3
1
Hint
Sample Explanation
For sample 1, consider the following call:
solve(3, 1, {-1, 0, 0}, {{1, 2}});
The corresponding tree is shown below:

Leaves and may fall on the same day, or leaf falls first on day 1 and then leaf falls on day 2. The number of days with leaf shedding is at most .
Therefore, the program should return .
For sample 2, consider the following call:
solve(5, 2, {-1, 0, 0, 1, 1}, {{1, 2, 3, 4}, {4, 1, 2, 3}});
The corresponding tree is shown below:

Assume there are at least days with leaf shedding. From the volunteers' records, leaf would fall on different days (the first day and the last day), which is a contradiction.
Therefore, the program should return .
Constraints
- , and for ,
- For , the array is a permutation of
- It is guaranteed that describes a rooted tree with node as the root.
The detailed additional constraints and scores of subtasks are shown in the table below.
| Subtask ID | Additional Constraints | Score |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | No additional constraints. |
Translated by ChatGPT 5