A11327 | Sasha and the Happy Tree Cutting
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Sasha was given a tree $^{\dagger}$ 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 $(a_1, b_1), \ldots, (a_k, b_k)$ , he colored at least one edge on the simple path $^{\ddagger}$ between vertices $a_i$ and $b_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.
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 $t$ ( $1 \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 $n$ ( $2 \leq n \leq 10^5$ ) — 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 $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \ne v_i$ ) — the numbers of the vertices connected by the $i$ -th edge.
The next line contains a single integer $k$ ( $1 \leq k \leq 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 $a_j$ and $b_j$ ( $1 \leq a_j, b_j \leq n, a_j \neq b_j$ ) — the vertices in the $j$ -th pair.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ . It is guaranteed that the sum of $2^k$ over all test cases does not exceed $2^{20}$ .
The first line of each test case contains a single integer $n$ ( $2 \leq n \leq 10^5$ ) — 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 $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \ne v_i$ ) — the numbers of the vertices connected by the $i$ -th edge.
The next line contains a single integer $k$ ( $1 \leq k \leq 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 $a_j$ and $b_j$ ( $1 \leq a_j, b_j \leq n, a_j \neq b_j$ ) — the vertices in the $j$ -th pair.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ . It is guaranteed that the sum of $2^k$ over all test cases does not exceed $2^{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)$ . 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)$ .

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

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted