#P10723. [GESP202406 七级] 黑白翻转
[GESP202406 七级] 黑白翻转
Background
Related multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1155.
Problem Description
Xiao Yang has a tree with nodes. Each node in the tree is either white or black. Xiao Yang considers a tree to be a beautiful tree if and only if, after deleting all white nodes, the remaining nodes still form a tree.
In each operation, Xiao Yang can choose one white node and change its color to black. He wants to know the minimum number of operations needed to make the tree become a beautiful tree.
Input Format
The first line contains a positive integer , representing the number of nodes in the tree.
The second line contains non-negative integers . If , then node is white; otherwise, it is black.
Then follow lines. Each line contains two positive integers , indicating that there is an edge connecting node and node .
Output Format
Output one integer, representing the minimum number of operations needed.
5
0 1 0 1 0
1 2
1 3
3 4
3 5
2
Hint
Sample Explanation
Changing nodes and to black is enough to make the tree a beautiful tree. At this time, after deleting the white node , the remaining black nodes still form a tree.
Constraints
| Subtask ID | Percentage of testdata | Special condition | ||
|---|---|---|---|---|
| The tree is a chain. | ||||
| Only two nodes are black. | ||||
| None. |
For all testdata, it is guaranteed that and .
Translated by ChatGPT 5