#P11018. Monochrome Tree

Monochrome Tree

Problem Description

You are given a tree rooted at node 11. Each node uu can only have one of two colors: black or white. The initial color of each node uu is known and is denoted as coloru\mathrm{color}_u.

You have two operations:

  • Operation 11: Flip the colors of all nodes on the path from any node uu to the root (the path includes uu and the root).
  • Operation 22: Flip the colors of all nodes in the subtree rooted at any node uu (the subtree of uu includes uu).

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 nn, the number of nodes.

The second line contains nn integers colori\mathrm{color}_i. colori=0\mathrm{color}_i=0 means node ii is initially white, and colori=1\mathrm{color}_i=1 means node ii is initially black.

The next n1n-1 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 1n2×1051 \le n \le 2\times 10^5, and 0colori10\le \mathrm{color}_i\le 1.

Subtask\text{Subtask} nn\leq Score Special Properties
00 55 33 None
11 1010 77
22 2×1032\times 10^3 2929
33 2×1052\times 10^5 6161

Translated by ChatGPT 5