CF1929E.Sasha and the Happy Tree Cutting

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Sasha was given a tree ^{\dagger} with nn 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 kk pairs of vertices (a1,b1),,(ak,bk)(a_1, b_1), \ldots, (a_k, b_k) , he colored at least one edge on the simple path ^{\ddagger} between vertices aia_i and bib_i .

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.

^{\dagger} A tree is an undirected connected graph without cycles.

^{\ddagger} 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 tt ( 1t1041 \le t \le 10^4 ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn ( 2n1052 \leq n \leq 10^5 ) — the number of vertices in the tree.

The next (n1)(n - 1) lines describe the edges of the tree. The ii -th line contains two integers uiu_i and viv_i ( 1ui,vin1 \leq u_i, v_i \leq n , uiviu_i \ne v_i ) — the numbers of the vertices connected by the ii -th edge.

The next line contains a single integer kk ( 1k201 \leq k \leq 20 ) — the number of pairs of vertices between which Sasha colored at least one edge on a simple path.

The next kk lines describe pairs. The jj -th line contains two integers aja_j and bjb_j ( 1aj,bjn,ajbj1 \leq a_j, b_j \leq n, a_j \neq b_j ) — the vertices in the jj -th pair.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5 . It is guaranteed that the sum of 2k2^k over all test cases does not exceed 2202^{20} .

输出格式

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)(1, 2) . Then, there would be at least one colored edge on the simple path between vertices 11 and 33 , and vertices 44 and 11 .

In the second test case, Sasha could have colored the edges (1,6)(1, 6) and (1,3)(1, 3) .

首页