CF1929E.Sasha and the Happy Tree Cutting
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Sasha was given a tree † with n vertices as a prize for winning yet another competition. However, upon returning home after celebrating his victory, he noticed that some parts of the tree were missing. Sasha remembers that he colored some of the edges of this tree. He is certain that for any of the k pairs of vertices (a1,b1),…,(ak,bk) , he colored at least one edge on the simple path ‡ between vertices ai and bi .
Sasha does not remember how many edges he exactly colored, so he asks you to tell him the minimum number of edges he could have colored to satisfy the above condition.
† A tree is an undirected connected graph without cycles.
‡ A simple path is a path that passes through each vertex at most once.
输入格式
Each test consists of multiple test cases. The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n ( 2≤n≤105 ) — the number of vertices in the tree.
The next (n−1) lines describe the edges of the tree. The i -th line contains two integers ui and vi ( 1≤ui,vi≤n , ui=vi ) — the numbers of the vertices connected by the i -th edge.
The next line contains a single integer k ( 1≤k≤20 ) — the number of pairs of vertices between which Sasha colored at least one edge on a simple path.
The next k lines describe pairs. The j -th line contains two integers aj and bj ( 1≤aj,bj≤n,aj=bj ) — the vertices in the j -th pair.
It is guaranteed that the sum of n over all test cases does not exceed 105 . It is guaranteed that the sum of 2k over all test cases does not exceed 220 .
输出格式
For each test case, output a single integer — the minimum number of edges Sasha could have colored.
输入输出样例
输入#1
3 4 1 2 2 3 2 4 2 1 3 4 1 6 1 2 3 1 6 1 5 2 4 2 3 3 1 3 6 2 6 5 1 2 2 3 3 4 4 5 4 1 2 2 3 3 4 4 5
输出#1
1 2 4
说明/提示
In the first test case, Sasha could have colored only one edge (1,2) . Then, there would be at least one colored edge on the simple path between vertices 1 and 3 , and vertices 4 and 1 .
In the second test case, Sasha could have colored the edges (1,6) and (1,3) .