#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 nn 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 nn, representing the number of nodes in the tree.

The second line contains nn non-negative integers a1,a2,,ana_1,a_2,\ldots,a_n. If ai=0a_i=0, then node ii is white; otherwise, it is black.

Then follow n1n-1 lines. Each line contains two positive integers xi,yix_i,y_i, indicating that there is an edge connecting node xix_i and node yiy_i.

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 11 and 33 to black is enough to make the tree a beautiful tree. At this time, after deleting the white node 55, the remaining black nodes still form a tree.

Constraints

Subtask ID Percentage of testdata nn aia_i Special condition
11 30%30\% 105\leq 10^5 0ai10\leq a_i\leq 1 The tree is a chain.
22 Only two nodes are black.
33 40%40\% None.

For all testdata, it is guaranteed that 1n1051\leq n\leq 10^5 and 0ai10\leq a_i\leq 1.

Translated by ChatGPT 5