CF1929E.Sasha and the Happy Tree Cutting

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

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.

萨沙因赢得另一场比赛而获得了一棵含有 nn 个顶点的树†^{\dagger} 作为奖品。然而,在庆祝胜利回家后,他发现这棵树缺失了一些部分。萨沙记得他曾给该树中的一些边染过色。他确信:对于给定的 kk 对顶点 (a1,b1),…,(ak,bk)(a_1, b_1), \ldots, (a_k, b_k) 中的任意一对 (ai,bi)(a_i, b_i),连接 aia_i 与 bib_i 的简单路径‡^{\ddagger} 上至少有一条边被他染过色。

萨沙不记得自己确切染了多少条边,因此他请你告诉他:为满足上述条件,他最少需要染多少条边?

†^{\dagger} 树是一个无向、连通且无环的图。

‡^{\ddagger} 简单路径是指至多经过每个顶点一次的路径。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \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 (2≤n≤1052 \leq n \leq 10^5) — the number of vertices in the tree.

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

The next line contains a single integer kk (1≤k≤201 \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 (1≤aj,bj≤n,aj≠bj1 \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}.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5),表示树中顶点的数量。

接下来 (n−1)(n - 1) 行描述树的边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ui≠viu_i \ne v_i),表示第 ii 条边所连接的两个顶点的编号。

接下来一行包含一个整数 kk(1≤k≤201 \leq k \leq 20),表示 Sasha 至少对其中某条简单路径上的边进行染色的顶点对的数量。

接下来 kk 行描述这些顶点对。第 jj 行包含两个整数 aja_j 和 bjb_j(1≤aj,bj≤n1 \leq a_j, b_j \leq n,aj≠bja_j \neq b_j),表示第 jj 对顶点。

保证所有测试用例的 nn 之和不超过 10510^5。保证所有测试用例的 2k2^k 之和不超过 2202^{20}。

输出格式

For each test case, output a single integer — the minimum number of edges Sasha could have colored.

对于每个测试用例,输出一个整数——Sasha 可能涂色的最少边数。

输入输出样例

  • 输入#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).

在第一个测试用例中,萨沙本可以只给边 (1,2)(1, 2) 染色。此时,在顶点 11 与 33 之间的简单路径上,以及顶点 44 与 11 之间的简单路径上,均至少存在一条被染色的边。

在第二个测试用例中,萨沙可以给边 (1,6)(1, 6) 和 (1,3)(1, 3) 染色。

输入解题思路,AI测评打分。不知道怎么写?

首页