CF766E.Mahmoud and a xor trip
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number a__i attached to it.
We define the distance from city x to city y as the xor of numbers attached to the cities on the path from x to y (including both x and y). In other words if values attached to the cities on the path from x to y form an array p of length l then the distance between them is
, where
is bitwise xor operation.
Mahmoud and Ehab want to choose two cities and make a journey from one to another. The index of the start city is always less than or equal to the index of the finish city (they may start and finish in the same city and in this case the distance equals the number attached to that city). They can't determine the two cities so they try every city as a start and every city with greater index as a finish. They want to know the total distance between all pairs of cities.
马哈茂德和艾哈布生活在一个拥有 n 座城市(编号从 1 到 n)的国家,这些城市由 n−1 条无向道路连接。保证任意两座城市之间均可通过这些道路相互到达。每座城市 i 都有一个关联的数值 ai。
我们定义从城市 x 到城市 y 的“距离”为:路径上所有城市(包括 x 和 y)所关联数值的异或(XOR)结果。换言之,若从 x 到 y 的路径上所有城市的关联数值构成一个长度为 l 的数组 p,则它们之间的距离为
,
其中
表示按位异或运算。
马哈茂德和艾哈布希望选择两座城市,并从其中一座出发前往另一座。出发城市的编号始终小于等于终点城市的编号(他们可能从某座城市出发并回到同一座城市,此时距离即为该城市所关联的数值 ai)。由于无法预先确定具体是哪两座城市,他们尝试将每座城市作为起点,再将所有编号更大的城市作为终点。他们希望知道:对所有满足起点编号 ≤ 终点编号的城市对,其“距离”的总和是多少。
输入格式
The first line contains integer n (1 ≤ n ≤ 105) — the number of cities in Mahmoud and Ehab's country.
Then the second line contains n integers _a_1, _a_2, ..., a__n (0 ≤ a__i ≤ 106) which represent the numbers attached to the cities. Integer a__i is attached to the city i.
Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), denoting that there is an undirected road between cities u and v. It's guaranteed that you can reach any city from any other using these roads.
第一行包含一个整数 $ n ( 1 \leq n \leq 10^5 $)—— 表示马哈茂德和埃哈卜所在国家的城市数量。
第二行包含 $ n $ 个整数 $ a_1, a_2, \dots, a_n ( 0 \leq a_i \leq 10^6 $),表示分配给各城市的数字。其中,整数 $ a_i $ 分配给第 $ i $ 座城市。
接下来的 $ n - 1 $ 行中,每行包含两个整数 $ u $ 和 $ v ( 1 \leq u, v \leq n $,且 $ u \neq v $),表示城市 $ u $ 与城市 $ v $ 之间存在一条无向道路。保证通过这些道路可以从任意一座城市到达其他任意一座城市。
输出格式
Output one number denoting the total distance between all pairs of cities.
输出一个数字,表示所有城市对之间的总距离。
输入输出样例
输入#1
3 1 2 3 1 2 2 3
输出#1
10
输入#2
5 1 2 3 4 5 1 2 2 3 3 4 3 5
输出#2
52
输入#3
5 10 9 8 7 6 1 2 2 3 3 4 3 5
输出#3
131
说明/提示
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.
In the first sample the available paths are:
- city 1 to itself with a distance of 1,
- city 2 to itself with a distance of 2,
- city 3 to itself with a distance of 3,
- city 1 to city 2 with a distance of
, - city 1 to city 3 with a distance of
, - city 2 to city 3 with a distance of
.
The total distance between all pairs of cities equals 1 + 2 + 3 + 3 + 0 + 1 = 10.
按位异或(XOR)运算对两个等长的二进制整数执行逻辑异或操作,即对其每一对对应位分别进行异或运算。在每一位上,若仅第一个位为 1 或仅第二个位为 1,则结果为 1;若两个位均为 0 或均为 1,则结果为 0。关于按位异或运算的更多内容,请参见:https://en.wikipedia.org/wiki/Bitwise_operation#XOR。
在第一个样例中,所有可能的路径如下:
- 城市 1 到其自身的距离为 1,
- 城市 2 到其自身的距离为 2,
- 城市 3 到其自身的距离为 3,
- 城市 1 到城市 2 的距离为
, - 城市 1 到城市 3 的距离为
, - 城市 2 到城市 3 的距离为
。
所有城市对之间的总距离为 1+2+3+3+0+1=10。
输入解题思路,AI测评打分。不知道怎么写?