地址: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. CentOS安装python setuptools and pip

    安装setup-tools wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --n ...

  2. Timer类与TimerTask类

    有个schedule方法,可以指定过多长时间定期的执行某个程序或某段代码,或者过多长时间启动一个线程等. TimerTask类实现了Runnable接口,要执行的类由它里面实现的run方法来完成. 编 ...

  3. 读取csv格式的数据

    1.直接上代码,关键是会用 2.代码如下: <?php #添加推荐到英文站 $file = fopen('code.csv','r'); while ($data = fgetcsv($file ...

  4. ActiveMQ安装优化

    ActiveMQ性能測试 http://m.blog.csdn.net/blog/brushli/41750615 1.下载ActiveMQ 官网:http://activemq.apache.org ...

  5. 线程池ThreadPool详解

    http://www.cnblogs.com/kissdodog/archive/2013/03/28/2986026.html 一.CLR线程池 管理线程开销最好的方式: 尽量少的创建线程并且能将线 ...

  6. mysql如何判断一个字符串是否包含另外一个字符串?

    转自:http://blog.csdn.net/hechurui/article/details/49278493 判断子字符串在父字符串当中的索引: SELECT LOCATE("b&qu ...

  7. Tika a content analysis toolkit

    Apache Tika - a content analysis toolkit The Apache Tika™ toolkit detects and extracts metadata and ...

  8. buffer/interger overflow /return-to-libc攻击实验

    buffer/interger overflow /return-to-libc攻击实验 http://blog.sina.com.cn/s/blog_70dd16910100rdgn.html ht ...

  9. FW: AMD, CMD, CommonJS和UMD

    javascript 我是豆腐不是渣 4月5日发布 推荐 2 推荐 收藏 32 收藏,486 浏览 今天由于项目中引入的echarts的文件太大,requirejs经常加载超时,不得不分开来加载ech ...

  10. IO 流的操作基本规律

    想要知道开发时,使用哪个流对象, 只要通过四个明确即可. 明确源和目的(数据汇) 源: InputStream 或 Reader 目的: OutPutStream 或 Writer 明确数据是否是纯文 ...