#P11562. 【MX-X7-T3】[LSOT-3] 寄存器

    ID: 11723 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心并查集O2优化树的直径梦熊比赛

【MX-X7-T3】[LSOT-3] 寄存器

Background

Original link: https://oier.team/problems/X7D.

This is not APIO, so this problem is not asking you to build a CPU by hand.

Problem Description

There are nn registers, numbered 1n1 \sim n. These registers are connected by n1n - 1 wires with switches. To ensure smooth information exchange, it is guaranteed that every pair of registers can be connected through some wires.

Initially, the information stored in every register is 00. Each time, Xiao H can independently toggle the switches of all wires, and then choose one register to power on. If a register is connected to a powered register by an enabled wire, then this register will also be powered on. All powered registers will flip their stored information (00 becomes 11, and 11 becomes 00). Xiao H wants the registers to store the information he wants, and he wants you to tell him the minimum number of power-on operations needed.

Input Format

The first line contains a positive integer nn, representing the number of registers.

The second line contains nn non-negative integers a1,,ana_1, \ldots, a_n, where aia_i means Xiao H wants register ii to store aia_i. It is guaranteed that aia_i is either 00 or 11.

The next n1n - 1 lines each contain two positive integers u,vu, v, indicating that there is a wire between registers uu and vv. It is guaranteed that every pair of registers can be connected through some wires.

Output Format

Output a single line containing a non-negative integer, representing the minimum number of power-on operations.

5
1 0 0 1 0
1 2
2 3
2 4
3 5

2

15
1 0 0 0 0 1 0 1 1 1 0 0 1 1 0
10 2
1 7
1 5
9 7
14 2
4 11
6 5
9 15
4 5
5 3
5 14
13 5
5 8
5 12

4

Hint

Sample Explanation #1

First, turn off wire (1,2)(1, 2) and turn on all the others, then power on register 11. At this time, the information in 11 is flipped, and the information stored in all registers becomes 1 0 0 0 0.

Then, turn off wire (2,4)(2, 4) and turn on all the others, then power on register 44. At this time, the information in 44 is flipped, and the information stored in all registers becomes 1 0 0 1 0, which meets the requirement.

It can be proven that there is no better solution.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (20 points): n5n \le 5.
  • Subtask 2 (20 points): For the ii-th wire, u=iu=i, v=i+1v=i+1.
  • Subtask 3 (30 points): There does not exist a pair of adjacent registers that want to store the same information.
  • Subtask 4 (30 points): No special properties.

For all testdata: 1n1061 \le n \le 10^6, 1u,vn1 \le u, v \le n, 0ai10 \le a_i \le 1, and every pair of registers can be connected through some wires.

Translated by ChatGPT 5