地址:http://codeforces.com/contest/766/problem/E

题目:

E. Mahmoud and a xor trip
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ai 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.

Input

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 a1, a2, ..., an (0 ≤ ai ≤ 106) which represent the numbers attached to the cities. Integer ai is attached to the city i.

Each of the next n  -  1 lines contains two integers u and v (1  ≤  u,  v  ≤  nu  ≠  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.

Output

Output one number denoting the total distance between all pairs of cities.

Examples
input
3
1 2 3
1 2
2 3
output
10
input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
output
52
input
5
10 9 8 7 6
1 2
2 3
3 4
3 5
output
131
Note

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.

 
思路:思路很巧妙地树形dp+二进制拆位。
  每次考虑第k位对答案的贡献。
  dp[i][0]表示经过i节点的异或和为0的路径的数量,dp[i][1]同理。
  具体见代码:
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; int n,v[K],dp[K][];
LL ans;
vector<int>mp[K]; void dfs(int x,int f,int k)
{
int t=v[x]>>k&;//判断第k位是0还是1
LL sum=;
dp[x][t]=,dp[x][t^]=;//初始化
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=f)
{
int v=mp[x][i];
dfs(v,x,k);
sum+=dp[x][]*dp[v][]+dp[x][]*dp[v][];//只有0^1,1^0对答案有贡献
dp[x][t^]+=dp[v][];//很巧妙的更新状态,因为异或值,
dp[x][t^]+=dp[v][];//所以是t^0后的结果加上dp[v][0]
}
ans+=(sum<<k);
}
int main(void)
{
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",v+i),ans+=v[i];
for(int i=,u,v;i<n;i++)
scanf("%d%d",&u,&v),mp[u].PB(v),mp[v].PB(u);
for(int i=;i<=;i++)
dfs(,,i);
printf("%lld\n",ans);
return ;
}

Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip的更多相关文章

  1. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑

    E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...

  2. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP

      题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...

  3. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  4. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary

    地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...

  5. Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp

    C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...

  6. Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心

    B. Mahmoud and a Triangle 题目连接: http://codeforces.com/contest/766/problem/B Description Mahmoud has ...

  7. Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题

    A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...

  8. Codeforces Round #396 (Div. 2) C. Mahmoud and a Message

    地址:http://codeforces.com/contest/766/problem/C 题目: C. Mahmoud and a Message time limit per test 2 se ...

  9. Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle

    地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...

随机推荐

  1. JXTA 2: 具有高性能、海量伸缩性的 P2P 网络

    这是来自developerWorks的一篇文章,地址是 https://www.ibm.com/developerworks/cn/java/j-jxta2/ ———————————————————— ...

  2. php socket编程入门

    服务端 <?php /** * File name server.php * 服务器端代码 * * @author guisu.huang * @since 2012-04-11 * */ // ...

  3. 【Python】Webpy

    http://webpy.org/install.zh-cn 官网学习,对于No socket could be created 一般是默认的8080端口已经被某些服务占用,可以换一个端口.

  4. 【python】获取网页中中文内容并分词

    # -*- coding: utf-8 -*- import urllib2 import re import time import jieba url="http://www.baidu ...

  5. poj 1419(图的着色问题,搜索)

    题目链接:http://poj.org/problem?id=1419 思路:只怪数据太弱!直接爆搜,按顺序搜索即可. #include<iostream> #include<cst ...

  6. PHP编程经常容易记乱的知识

    PHP经常容易记乱的知识 1.echo和print的区别 PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.echo输出后没有返回值,但print有返回值,当其执行失败 ...

  7. 【转】Linux下mysql操作

    本文转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/18/2216279.html 1.linux下启动mysql的命令:my ...

  8. 调结者(Dispatcher)之执行action

    调结者的执行action StrutsExecuteFilter类的工作就是执行对应的action请求.StrutsExecuteFilter类的工作还需要有一个叫ExecuteOperations类 ...

  9. Entity Frameword 查询 sql func linq 对比

    Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便.今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更多在用Func形式,l ...

  10. .NET面试

    作者:最佳菜鸟链接:https://zhuanlan.zhihu.com/p/22224795来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1 .术语 面试出现频率: ...