#P11018. Monochrome Tree
Monochrome Tree
Problem Description
You are given a tree rooted at node . Each node can only have one of two colors: black or white. The initial color of each node is known and is denoted as .
You have two operations:
- Operation : Flip the colors of all nodes on the path from any node to the root (the path includes and the root).
- Operation : Flip the colors of all nodes in the subtree rooted at any node (the subtree of includes ).
You are asked: what is the minimum number of operations needed to make the entire tree black.
Input Format
The first line contains an integer , the number of nodes.
The second line contains integers . means node is initially white, and means node is initially black.
The next lines each contain two integers describing an edge connecting the two nodes.
Output Format
Output one integer, the minimum number of operations.
6
0 1 1 1 0 0
1 2
1 3
2 5
5 4
5 6
3
7
0 0 1 0 0 1 1
6 4
3 4
3 5
1 5
7 3
2 7
3
Hint
Constraints
For all testdata, it is guaranteed that , and .
| Score | Special Properties | ||
|---|---|---|---|
| None | |||
Translated by ChatGPT 5