CF1824C.LuoTianyi and XOR-Tree

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

LuoTianyi gives you a tree with values in its vertices, and the root of the tree is vertex 11.

In one operation, you can change the value in one vertex to any non-negative integer.

Now you need to find the minimum number of operations you need to perform to make each path from the root to leaf†^{\dagger} has a bitwise XOR value of zero.

†^{\dagger}A leaf in a rooted tree is a vertex that has exactly one neighbor and is not a root.

洛天依给你一棵顶点带权的树,树的根节点为顶点 11。

在一次操作中,你可以将任意一个顶点的权值修改为任意非负整数。

现在你需要求出:为使从根节点到每个叶节点†^{\dagger}的每条路径的按位异或(XOR)值均为零,所需的最少操作次数。

†^{\dagger} 在有根树中,叶节点指恰好有一个邻接顶点且不是根节点的顶点。

输入格式

The first line contains a single integer nn (2≤n≤1052 \le n \le 10^5) — the number of vertices in the tree.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9), the ii-th number represents the value in the ii-th vertex.

Next n−1n−1 lines describe the edges of the tree. The ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n,ui≠vi1 \le u_i,v_i \le n, u_i \neq v_i) — the vertices connected by an edge of the tree. It's guaranteed that the given edges form a tree.

第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 树中顶点的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9),其中第 ii 个数表示第 ii 个顶点上的值。

接下来的 n−1n-1 行描述树的边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n,  ui≠vi1 \le u_i,v_i \le n,\; u_i \neq v_i)—— 表示由一条树边相连的两个顶点。保证所给的边构成一棵树。

输出格式

Print a single integer — the minimum number of operations.

输出一个整数——最小操作次数。

输入输出样例

  • 输入#1

    6
    3 5 7 5 8 4
    1 2
    1 3
    1 4
    3 5
    4 6

    输出#1

    3
  • 输入#2

    8
    7 10 7 16 19 9 16 11
    1 5
    4 2
    6 5
    5 2
    7 2
    2 3
    3 8

    输出#2

    3
  • 输入#3

    4
    1 2 1 2
    1 2
    2 3
    4 3

    输出#3

    0
  • 输入#4

    9
    4 3 6 1 5 5 5 2 7
    1 2
    2 3
    4 1
    4 5
    4 6
    4 7
    8 1
    8 9

    输出#4

    2

说明/提示

The tree in the first example:

If we change the value in the vertex 22 to 33, the value in the vertex 55 to 44, and the value in the vertex 66 to 66, then the tree will be ok.

The bitwise XOR from the root to the leaf 22 will be 3⊕3=03 \oplus 3=0.

The bitwise XOR from the root to the leaf 55 will be 4⊕7⊕3=04 \oplus 7 \oplus 3=0.

The bitwise XOR from the root to the leaf 66 will be 6⊕5⊕3=06 \oplus 5 \oplus 3=0.

The tree in the second example:

If we change the value in the vertex 22 to 44, the value in the vertex 33 to 2727, and the value in the vertex 66 to 2020, then the tree will be ok.

The bitwise XOR from the root to the leaf 66 will be 20⊕19⊕7=020 \oplus 19 \oplus 7=0.

The bitwise XOR from the root to the leaf 88 will be 11⊕27⊕4⊕19⊕7=011 \oplus 27 \oplus 4 \oplus 19 \oplus 7=0.

The bitwise XOR from the root to the leaf 44 will be 16⊕4⊕19⊕7=016 \oplus 4 \oplus 19 \oplus 7=0.

The bitwise XOR from the root to the leaf 77 will be 16⊕4⊕19⊕7=016 \oplus 4 \oplus 19 \oplus 7=0.

In the third example, the only leaf is the vertex 44 and the bitwise XOR on the path to it is 1⊕2⊕1⊕2=01 \oplus 2 \oplus 1 \oplus 2 = 0, so we don't need to change values.

In the fourth example, we can change the value in the vertex 11 to 55, and the value in the vertex 44 to 00.

Here ⊕\oplus denotes the bitwise XOR operation.

第一个示例中的树:

如果我们把顶点 22 的值改为 33,顶点 55 的值改为 44,顶点 66 的值改为 66,则该树即满足要求。

从根节点到叶子节点 22 的路径上所有节点值的按位异或(XOR)结果为 3⊕3=03 \oplus 3 = 0。

从根节点到叶子节点 55 的路径上所有节点值的按位异或结果为 4⊕7⊕3=04 \oplus 7 \oplus 3 = 0。

从根节点到叶子节点 66 的路径上所有节点值的按位异或结果为 6⊕5⊕3=06 \oplus 5 \oplus 3 = 0。

第二个示例中的树:

如果我们把顶点 22 的值改为 44,顶点 33 的值改为 2727,顶点 66 的值改为 2020,则该树即满足要求。

从根节点到叶子节点 66 的路径上所有节点值的按位异或结果为 20⊕19⊕7=020 \oplus 19 \oplus 7 = 0。

从根节点到叶子节点 88 的路径上所有节点值的按位异或结果为 11⊕27⊕4⊕19⊕7=011 \oplus 27 \oplus 4 \oplus 19 \oplus 7 = 0。

从根节点到叶子节点 44 的路径上所有节点值的按位异或结果为 16⊕4⊕19⊕7=016 \oplus 4 \oplus 19 \oplus 7 = 0。

从根节点到叶子节点 77 的路径上所有节点值的按位异或结果为 16⊕4⊕19⊕7=016 \oplus 4 \oplus 19 \oplus 7 = 0。

在第三个示例中,唯一的叶子节点是顶点 44,而其路径上的按位异或值为 1⊕2⊕1⊕2=01 \oplus 2 \oplus 1 \oplus 2 = 0,因此无需修改任何值。

在第四个示例中,我们可以将顶点 11 的值改为 55,并将顶点 44 的值改为 00。

其中 ⊕\oplus 表示按位异或(bitwise XOR)运算。

输入解题思路,AI测评打分。不知道怎么写?

首页