CF1929E.Sasha and the Happy Tree Cutting
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
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.
萨沙因赢得另一场比赛而获得了一棵含有 n 个顶点的树† 作为奖品。然而,在庆祝胜利回家后,他发现这棵树缺失了一些部分。萨沙记得他曾给该树中的一些边染过色。他确信:对于给定的 k 对顶点 (a1,b1),…,(ak,bk) 中的任意一对 (ai,bi),连接 ai 与 bi 的简单路径‡ 上至少有一条边被他染过色。
萨沙不记得自己确切染了多少条边,因此他请你告诉他:为满足上述条件,他最少需要染多少条边?
† 树是一个无向、连通且无环的图。
‡ 简单路径是指至多经过每个顶点一次的路径。
输入格式
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.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤105),表示树中顶点的数量。
接下来 (n−1) 行描述树的边。第 i 行包含两个整数 ui 和 vi(1≤ui,vi≤n,ui=vi),表示第 i 条边所连接的两个顶点的编号。
接下来一行包含一个整数 k(1≤k≤20),表示 Sasha 至少对其中某条简单路径上的边进行染色的顶点对的数量。
接下来 k 行描述这些顶点对。第 j 行包含两个整数 aj 和 bj(1≤aj,bj≤n,aj=bj),表示第 j 对顶点。
保证所有测试用例的 n 之和不超过 105。保证所有测试用例的 2k 之和不超过 220。
输出格式
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). 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).

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

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

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