CF766E.Mahmoud and a xor trip

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Mahmoud and Ehab live in a country with nn cities numbered from 11 to nn and connected by n1n-1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number aia_{i} attached to it.

We define the distance from city xx to city yy as the xor of numbers attached to the cities on the path from xx to yy (including both xx and yy ). In other words if values attached to the cities on the path from xx to yy form an array pp of length ll 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.

输入格式

The first line contains integer nn ( 1<=n<=1051<=n<=10^{5} ) — the number of cities in Mahmoud and Ehab's country.

Then the second line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 0<=ai<=1060<=a_{i}<=10^{6} ) which represent the numbers attached to the cities. Integer aia_{i} is attached to the city ii .

Each of the next n1n-1 lines contains two integers uu and vv ( 1<=u,v<=n1<=u,v<=n , uvu≠v ), denoting that there is an undirected road between cities uu and vv . It's guaranteed that you can reach any city from any other using these roads.

输出格式

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 11 if only the first bit is 11 or only the second bit is 11 , but will be 00 if both are 00 or both are 11 . 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 11 to itself with a distance of 11 ,
  • city 22 to itself with a distance of 22 ,
  • city 33 to itself with a distance of 33 ,
  • city 11 to city 22 with a distance of ,
  • city 11 to city 33 with a distance of ,
  • city 22 to city 33 with a distance of .

The total distance between all pairs of cities equals 1+2+3+3+0+1=101+2+3+3+0+1=10 .

首页