本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

Description

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 andy). 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

解题报告:

  题目要求在一棵带权树上的两两路径上点权异或和的总和。

  对于这种位运算的题目,我们的想法必然是按位考虑的,因为位与位之间彼此独立,可以单独考虑。

  那么拆成每一位之后,题目变成了统计0和1两种情况的组合的数目,最后乘2的幂次即可。f[x][0、1]表示x这一位为0、1时的数量,考虑我们可以每次把之前做完的子树与其父亲节点合并,然后再与当前节点组合计算贡献。

  值得一提的是,有必要注意一下向上转移的细节。如果x的这一位是0,显然没有影响;但是如果这一位是1,就会导致往上异或之后改变一下,需要注意。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 100011;
const int MAXM = 200011;
int n,a[MAXN],first[MAXN],ecnt,to[MAXM],next[MAXM];
LL ans,f[MAXN][2],now; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa,int wei){
int pos=a[x]>>wei; pos=pos&1;
f[x][pos]=1; f[x][pos^1]=0;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
dfs(v,x,wei);
ans+=f[v][pos]*f[x][pos^1]*now;
ans+=f[v][pos^1]*f[x][pos]*now; if(pos==0) {
f[x][pos]+=f[v][pos];
f[x][pos^1]+=f[v][pos^1];
}
else {
f[x][pos^1]+=f[v][pos];
f[x][pos]+=f[v][pos^1];
}
}
} inline void work(){
n=getint(); for(int i=1;i<=n;i++) a[i]=getint(),ans+=a[i]; int x,y;
for(int i=1;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
}
for(int i=0;i<=20;i++) {
now=(1<<i);
dfs(1,0,i);
}
printf("%lld",ans);
} int main()
{
work();
return 0;
}

  

codeforces766E Mahmoud and a xor trip(按位统计+树形DP)的更多相关文章

  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

    地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...

  3. Codeforces 766E Mahmoud and a xor trip(树形DP)

    题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...

  4. 【codeforces 766E】Mahmoud and a xor trip

    [题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...

  5. 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个点 ...

  6. codeforces 766E Mahmoud and a xor trip

    题目链接:http://codeforces.com/problemset/problem/766/E 大意,给出一个$n$个点的树,每个点都有一个权值,令$Disx$为$u$到$v$路径上的异或和求 ...

  7. CF766 E. Mahmoud and a xor trip [预处理][树形dp]

    题解: 二营长!你他娘的意大利炮呢? dp[i][j][0]: 从i,跋涉到以i为根的子树的每一个节点,在第j个数位上一共产生了多少个0. dp[i][j][1]: 从i,跋涉到以i为根的子树的每一个 ...

  8. 【题解】警位安排( 树形 DP)

    [题目描述]一个重要的基地被分成了 n 个连通的区域 , 出于某种原因 , 这个基地以某一个区域为核心,呈一树形分布.在每个区域里安排警卫的费用是不同的,而每个区域的警卫都可以望见其相邻的区域 .如果 ...

  9. [HDU4867]Xor (线段树分治+类数位dp)

    [HDU4867]Xor (线段树分治+类数位dp) 提供一种\((m+n) log a log m\)带有常数约\(\frac{1}{log n}\)的算法 处理询问,将后来加入的数算进序列中,则每 ...

随机推荐

  1. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  2. 安装和配置jenkins

    1.首先准备java环境,安装JDK 2.下载jenkins至Linux服务器 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkin ...

  3. vs编译程序不能实现,“未能完成操作 未指定的错误”的解决办法

    1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2005右边的“解决方案”处右键 重新加载,就 ...

  4. 常用的数据库sql语句

    create table query_audit_log(Time varchar(255) default null,Id int(10) default null, Command varchar ...

  5. QChart 如何放大缩小?

    #if 0 //QChart 放大缩小 double z = 1.0; QPoint numDegrees = e->angleDelta()/8; double zi = qAbs(0.1*n ...

  6. centos7安装nodejs 和 yarn

    如何从EPEL库安装Node.js 另一个有效且简单的方法来安装Node.js就是从官方库.这同样确保您可以访问到EPEL库,你可以通过运行以下命令. sudo yum install epel-re ...

  7. 于win2008R2虽然激活,但是一个小时之后就会自动强制关机的问题

    写一个批处理文件: taskkill /f /im wlms.exeping -n  4 127.0.0.1shutdown -a 用记事本写下以上的命令,另存为.bat 批处理文件.双击运行,即可. ...

  8. Yii2 注册表单验证规则 手机注册时候使用短信验证码

    public function rules() { return [ ['username', 'filter', 'filter' => 'trim'], ['username', 'requ ...

  9. Android sdk manager加载缓慢或加载不出来

    1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...

  10. iOS KVC 和 KVO 的学习

    KVC  (NSKey Value Coding) :键值编码 KVO (Key Value Observing) :键值监听 前言:我曾经用过 监听 一个音频何时结束 监听视频播放 状态等 用了这种 ...