A13941 | Black, White and Grey Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree with each vertex coloured white, black or grey. You can remove elements from the tree by selecting a subset of vertices in a single connected component and removing them and their adjacent edges from the graph. The only restriction is that you are not allowed to select a subset containing a white and a black vertex at once.
What is the minimum number of removals necessary to remove all vertices from the tree?
What is the minimum number of removals necessary to remove all vertices from the tree?
输入格式
Each test contains multiple test cases. The first line contains an integer $t$ ( $1 \le t \le 100\,000$ ), denoting the number of test cases, followed by a description of the test cases.
The first line of each test case contains an integer $n$ ( $1 \le n \le 200\,000$ ): the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_v$ ( $0 \le a_v \le 2$ ): colours of vertices. Gray vertices have $a_v=0$ , white have $a_v=1$ , black have $a_v=2$ .
Each of the next $n-1$ lines contains two integers $u, v$ ( $1 \le u, v \le n$ ): tree edges.
The sum of all $n$ throughout the test is guaranteed to not exceed $200\,000$ .
The first line of each test case contains an integer $n$ ( $1 \le n \le 200\,000$ ): the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_v$ ( $0 \le a_v \le 2$ ): colours of vertices. Gray vertices have $a_v=0$ , white have $a_v=1$ , black have $a_v=2$ .
Each of the next $n-1$ lines contains two integers $u, v$ ( $1 \le u, v \le n$ ): tree edges.
The sum of all $n$ throughout the test is guaranteed to not exceed $200\,000$ .
输出格式
For each test case, print one integer: the minimum number of operations to solve the problem.
输入输出样例
输入 #1
4 2 1 1 1 2 4 1 2 1 2 1 2 2 3 3 4 5 1 1 0 1 2 1 2 2 3 3 4 3 5 8 1 2 1 2 2 2 1 2 1 3 2 3 3 4 4 5 5 6 5 7 5 8
输出 #1
1 3 2 3

In the first test case, both vertices are white, so you can remove them at the same time.

In the second test case, three operations are enough. First, we need to remove both black vertices (2 and 4), then separately remove vertices 1 and 3. We can't remove them together because they end up in different connectivity components after vertex 2 is removed.

In the third test case, we can remove vertices 1, 2, 3, 4 at the same time, because three of them are white and one is grey. After that, we can remove vertex 5.

In the fourth test case, three operations are enough. One of the ways to solve the problem is to remove all black vertices at once, then remove white vertex 7, and finally, remove connected white vertices 1 and 3.
In the first test case, both vertices are white, so you can remove them at the same time.

In the second test case, three operations are enough. First, we need to remove both black vertices (2 and 4), then separately remove vertices 1 and 3. We can't remove them together because they end up in different connectivity components after vertex 2 is removed.

In the third test case, we can remove vertices 1, 2, 3, 4 at the same time, because three of them are white and one is grey. After that, we can remove vertex 5.

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