#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 NN nodes, numbered from 00 to N1N - 1, where node 00 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 MM volunteers (numbered from 00 to M1M - 1) 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 33 and 44 fall, so they write down 3,43, 4 or 4,34, 3. On the second day, leaves 11 and 22 fall, so they continue by writing 1,21, 2 or 2,12, 1. The final record may be any one of (3,4,1,2)(3, 4, 1, 2), (4,3,1,2)(4, 3, 1, 2), (3,4,2,1)(3, 4, 2, 1), or (4,3,2,1)(4, 3, 2, 1).

This process continues for KK 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 MM volunteers. Try to infer the maximum possible value of KK 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);
  • NN: the number of nodes of the ancient tree.
  • MM: the number of volunteers.
  • FF: an array of length NN. For 1iN11 \le i \le N - 1, F[i]F[i] is the index of the parent of node ii. F[0]F[0] is always 1-1.
  • SS: an array of length MM. Each element of SS is an array of length N1N - 1. S[i][j]S[i][j] is the jj-th node number recorded by volunteer ii (starting from 00).
  • This function must return an integer, meaning the maximum possible value of KK 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 11: TT

For each of the next TT test cases:

  • Line 11: N MN\ M
  • Line 22: F[1] F[2]  F[N1]F[1]\ F[2]\ \ldots\ F[N - 1]
  • Line 3+i (0iM1)3 + i\ (0 \le i \le M - 1): S[i][0] S[i][1] S[i][2]  S[i][N2]S[i][0]\ S[i][1]\ S[i][2]\ \ldots\ S[i][N - 2]

Output Format

The sample grader prints your answer in the following format:

For each test case:

  • Line 11: 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 11 and 22 may fall on the same day, or leaf 11 falls first on day 1 and then leaf 22 falls on day 2. The number of days with leaf shedding is at most 22.

Therefore, the program should return 22.

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 22 days with leaf shedding. From the volunteers' records, leaf 44 would fall on different days (the first day and the last day), which is a contradiction.

Therefore, the program should return 11.

Constraints

  • 2N1052 \le N \le 10^5
  • 1M51 \le M \le 5
  • NM8×105\sum NM \le 8 \times 10^5
  • F[0]=1F[0] = -1, and for 1iN11 \le i \le N - 1, 0F[i]i10 \le F[i] \le i - 1
  • For 1iM11 \le i \le M - 1, the array S[i]S[i] is a permutation of 1,2,,N11, 2, \ldots , N - 1
  • It is guaranteed that FF describes a rooted tree with node 00 as the root.

The detailed additional constraints and scores of subtasks are shown in the table below.

Subtask ID Additional Constraints Score
1 M=1,N10,N30M=1,N\le 10,\sum N\le 30 1111
2 N10,N30N\le 10,\sum N\le 30 1414
3 M=1,N1000,N2000,F[i]=i1M=1,N\le 1\,000,\sum N\le 2\,000,F[i]=i-1 55
4 M=1,N1000,N2000M=1,N\le 1\,000,\sum N\le 2\,000 99
5 N1000,N2000,F[i]=i1N\le 1\,000,\sum N\le 2\,000,F[i]=i-1 55
6 N1000,N2000N\le 1\,000,\sum N\le 2\,000 1111
7 M=1,F[i]=i1M=1,F[i]=i-1 99
8 M=1M=1 1111
9 F[i]=i1F[i]=i-1 99
10 No additional constraints. 1616

Translated by ChatGPT 5