#P8341. [AHOI2022] 回忆

    ID: 9421 远端评测题 3000ms 2048MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>各省省选2022安徽Special JudgeO2优化

[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 nn problems, numbered from 11 to nn. Each problem is derived from a more basic problem, so the problems form a tree structure: problem 11 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 mm studies. The ii-th study started from a more basic problem sis_i, continuously modified and generalized it, and finally proposed problem tit_i. These studies satisfy sitis_i \ne t_i and si\bm{s_i} must be an ancestor of ti\bm{t_i}. Even if the studied problems are exactly the same, studying them from different angles may produce different results, so it is possible that ij,(si,ti)=(sj,tj)\bm{i \ne j, (s_i, t_i) = (s_j, t_j)}.

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 nn 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 sis_i and problem tit_i, then the ii-th study is said to be remembered.

To better understand the above concepts, consider the following example: n=5n = 5, problem 11 is related to problems 2,32, 3, and problem 33 is related to problems 4,54, 5. One possible round of recalling is: start from problem 22, switch to problem 11, then switch to problem 33, and finally switch to problem 55 and end. If m=4m = 4, $(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 11-st, 33-rd, and 44-th studies be remembered, while the 22-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 TT, representing the number of test cases.

For each test case, the first line contains two positive integers n,mn, m, representing the number of problems and the number of studies.

The next n1n - 1 lines each contain two positive integers ui,viu_i, v_i, indicating that problem uiu_i is related to problem viv_i.

The next mm lines each contain two positive integers si,tis_i, t_i, 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 mm 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 22, then switch thoughts to problems 1,3,51, 3, 5 in order. At this time, the 11-st, 33-rd, and 44-th studies are remembered, but the 22-nd and 55-th are not.
  • In the second round, start from problem 44, then switch thoughts to problems 3,13, 1 in order. At this time, the 22-nd and 55-th studies are remembered.

The second test case satisfies the requirement of property A. One possible plan is: in the first round, recall 2,1,3,5,62, 1, 3, 5, 6 in order, and in the second round, recall 8,7,9,108, 7, 9, 10 in order.

【Sample #2】

See the attached files memory/memory2.in and memory/memory2.ans.

This test case satisfies the conditions of test points 141 \sim 4.

【Sample #3】

See the attached files memory/memory3.in and memory/memory3.ans.

This sample satisfies property A. Also, except for the last 33 test cases, all the other samples satisfy n,m1000n, m \le 1000. Except for the last 3030 test cases, all the other samples satisfy n,m30n, m \le 30. 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 242524 \sim 25. Same as sample #3, it satisfies: except for the last 33 test cases, all the other samples satisfy n,m1000n, m \le 1000. Except for the last 3030 test cases, all the other samples satisfy n,m30n, m \le 30. 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 44 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 1\bm{1}, then you will get 33 points on this test point.

【Constraints】

This problem has 2525 test points. For all test points, 1n,m2×1051 \le n, m \le 2 \times {10}^5, 1n,m5×1051 \le \sum n, \sum m \le 5 \times {10}^5, 1ui,vi,si,tin1 \le u_i, v_i, s_i, t_i \le n, sitis_i \ne t_i. It is guaranteed that the input (ui,vi)(u_i, v_i) forms a tree, and in the tree rooted at 11, sis_i is an ancestor of tit_i.

Test Point Size Limit Special Property
141 \sim 4 T3000T \le 3000, n50n \le 50, m15m \le 15, and at most 55 test cases satisfy m10m \ge 10 None
565 \sim 6 n,m100n, m \le 100, n3,m33×107\sum n^3, \sum m^3 \le 3 \times {10}^7 A
77 B
88 C
99 None
101110 \sim 11 n,m1000n, m \le 1000, n2,m23×107\sum n^2, \sum m^2 \le 3 \times {10}^7 A
1212 B
1313 C
141614 \sim 16 None
171817 \sim 18 n,m2×105n, m \le 2 \times {10}^5, n,m5×105\sum n, \sum m \le 5 \times {10}^5 B
192019 \sim 20 C
212321 \sim 23 A
242524 \sim 25 None

Special property A: It is guaranteed that nn is even, and the tree structure is: for any positive integer 1in21 \le i \le \lfloor \frac{n}{2} \rfloor, the parent of 2i2 i is 2i12 i - 1; if i2i \ge 2, then the parent of 2i12 i - 1 is 2i32 i - 3.
Special property B: It is guaranteed that for all positive integers 1im1 \le i \le m, sis_i is the parent of tit_i.
Special property C: It is guaranteed that for all positive integers 1im1 \le i \le m, si=1s_i = 1.

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 mm lines, and each line is an integer.

In addition, if for some test case your output is smaller than the correct answer by 11 rather than larger by 11, then you cannot get 33 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