#P8341. [AHOI2022] 回忆
[AHOI2022] 回忆
Background
Living inside the problem statement, they are a group of strange teenagers.
They ignore the basic physical limits required for building roads in a city, and are obsessed with all kinds of structures over hundreds of thousands of cities and millions of roads.
They clearly know that the true number they need is so huge that it cannot be computed, yet they still insist on caring about its value modulo some strange prime.
Although they are so intelligent, they always lose to the weird questions they提出 themselves, and throw all of them to you to solve.
Now, they have grown up. They have learned more general theories and got used to more abstract symbols, so they no longer need to think about such odd problems. But they never expected that you, driven by these “useless” problems, would open up a new world belonging only to OI in a corner of the computer science system.
One day, they each recalled the problems they proposed in their teenage years.
Problem Description
When they were young, they proposed problems, numbered from to . Each problem is derived from a more basic problem, so the problems form a tree structure: problem is the most basic one, i.e. the root of the tree, and every other problem is derived from the problem corresponding to its parent node. If two problems are adjacent on the tree, then these two problems are called related to each other.
In their teenage years, they conducted studies. The -th study started from a more basic problem , continuously modified and generalized it, and finally proposed problem . These studies satisfy and must be an ancestor of . Even if the studied problems are exactly the same, studying them from different angles may produce different results, so it is possible that .
Now they are recalling the problems they proposed back then, round by round. In each round of recalling, they first recall any one of the problems. Next, if there exists a problem that is related to the currently recalled problem and has not been recalled in this round, then they may switch their thoughts from the current problem to any one of these problems, and recall this new problem. They may keep switching thoughts, and they may also end the recalling after recalling any problem. Each round of recalling is independent, meaning a problem may be recalled in multiple rounds.
If in some round of recalling they recall both problem and problem , then the -th study is said to be remembered.
To better understand the above concepts, consider the following example: , problem is related to problems , and problem is related to problems . One possible round of recalling is: start from problem , switch to problem , then switch to problem , and finally switch to problem and end. If , $(s_1, t_1) = (1, 2), (s_2, t_2) = (1, 4), (s_3, t_3) = (s_4, t_4) = (3, 5)$, then this round of recalling will make the -st, -rd, and -th studies be remembered, while the -nd study will not be remembered.
Their final question to you is: if the starting point of each round of recalling and the switches of thoughts can be chosen arbitrarily, what is the minimum number of rounds of recalling needed so that all studies are remembered.
Input Format
This problem contains multiple test cases. The first line of input contains a positive integer , representing the number of test cases.
For each test case, the first line contains two positive integers , representing the number of problems and the number of studies.
The next lines each contain two positive integers , indicating that problem is related to problem .
The next lines each contain two positive integers , describing one study.
Output Format
For each test case, output one line with one positive integer, representing the minimum number of rounds of recalling they need so that all studies are remembered.
2
5 5
1 2
3 1
3 4
5 3
1 2
1 4
3 5
3 5
3 4
10 5
1 2
3 1
3 4
5 3
6 5
7 8
5 7
7 9
9 10
1 2
3 5
5 6
7 8
9 10
2
2
Hint
【Sample Explanation #1】
The first test case in the sample is the same as the example given in the statement. One possible plan is:
- In the first round, start from problem , then switch thoughts to problems in order. At this time, the -st, -rd, and -th studies are remembered, but the -nd and -th are not.
- In the second round, start from problem , then switch thoughts to problems in order. At this time, the -nd and -th studies are remembered.
The second test case satisfies the requirement of property A. One possible plan is: in the first round, recall in order, and in the second round, recall in order.
【Sample #2】
See the attached files memory/memory2.in and memory/memory2.ans.
This test case satisfies the conditions of test points .
【Sample #3】
See the attached files memory/memory3.in and memory/memory3.ans.
This sample satisfies property A. Also, except for the last test cases, all the other samples satisfy . Except for the last test cases, all the other samples satisfy . You can also use this sample to test on smaller-scale data.
【Sample #4】
See the attached files memory/memory4.in and memory/memory4.ans.
This sample satisfies the conditions of test points . Same as sample #3, it satisfies: except for the last test cases, all the other samples satisfy . Except for the last test cases, all the other samples satisfy . You can also use this sample to test on smaller-scale data.
【Sample #5】
See the attached files memory/memory5.in and memory/memory5.ans.
This sample satisfies property B.
【Sample #6】
See the attached files memory/memory6.in and memory/memory6.ans.
This sample satisfies property C.
【Scoring】
For each test point, if your output format is correct and the answers for every test case are correct, then you will get points.
Otherwise, if your output format is correct and for every test case, the answer you output is equal to the correct answer or larger than the correct answer by , then you will get points on this test point.
【Constraints】
This problem has test points. For all test points, , , , . It is guaranteed that the input forms a tree, and in the tree rooted at , is an ancestor of .
| Test Point | Size Limit | Special Property |
|---|---|---|
| , , , and at most test cases satisfy | None | |
| , | A | |
| B | ||
| C | ||
| None | ||
| , | A | |
| B | ||
| C | ||
| None | ||
| , | B | |
| C | ||
| A | ||
| None |
Special property A: It is guaranteed that is even, and the tree structure is: for any positive integer , the parent of is ; if , then the parent of is .
Special property B: It is guaranteed that for all positive integers , is the parent of .
Special property C: It is guaranteed that for all positive integers , .
Please note that the difficulty of test points has no direct relationship with their indices.
【Notes】
Please note that to obtain partial scores, you must ensure that the output format is correct, i.e. output exactly lines, and each line is an integer.
In addition, if for some test case your output is smaller than the correct answer by rather than larger by , then you cannot get points on this test point.
Some test points have a large input size. To optimize the total running time of your program, we suggest using a faster input method.
Translated by ChatGPT 5