题目链接:http://codeforces.com/contest/766/problem/E

Examples

input

3

1 2 3

1 2

2 3

out

10

题意:

  给你一棵n个点的树,每个点有点权,求所有不同的路径异或和的总和

题解:

  DP

  任选一个root,设定dp[i][j][0]为以i为最高点的路径异或和中,二进制上第j为分别是0,1的数量

  记录答案

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e17+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e3+, mod = 1e9+, inf = 2e9+;
int n,a[N];
LL dp[N][][],ans;
vector<int > G[N];
void dfs(int u,int fa) {
for(int i = ; i < ; ++i)
dp[u][i][(a[u]>>i)&] = ;
ans+=a[u];
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == fa) continue;
dfs(to,u);
for(int j = ; j < ; ++j) {
ans += 1LL*(<<j)*(dp[u][j][]*dp[to][j][] + dp[u][j][]*dp[to][j][]);
}
for(int j = ; j < ; ++j) {
int p = ((a[u]>>j)&);
dp[u][j][] += dp[to][j][p^],
dp[u][j][] += dp[to][j][p^];
}
}
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i < n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(,-);
cout<<ans<<endl;
return ;
}

Codeforces Round #396 (Div. 2) E. 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 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. Lua表(table)的用法_个人总结

    Lua表(table)的用法_个人总结 1.表的创建及表的介绍 --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意 ...

  2. django 使用框架下auth.models自带的User进行扩展增加字段

    需要改动三个地方: 1.models.py   创建模型User,并继承原模型类AbstraUser(在此处我增加了一个新的字段手机号) from django.db import models # ...

  3. Python之条件判断

    Python之条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age ...

  4. auto_ptr 实现

    #ifndef MYAUTOPTR_H #define MYAUTOPTR_H template<typename _T> class MyAutoPtr { private: _T* _ ...

  5. POJ1068 Parencodings 解题报告

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  6. codevs——1385 挤牛奶

    1385 挤牛奶 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果     题目描述 Description 三个农民每天清 ...

  7. Ubuntu 16.04安装SwitchHosts

    下载: https://github.com/oldj/SwitchHosts/releases 解压: unzip SwitchHosts-linux-x64_v3.3.6.5287.zip 移动: ...

  8. MongoDB使用教程收集(语法教程)

    https://www.tutorialspoint.com/mongodb/index.htm https://wizardforcel.gitbooks.io/w3school-mongodb/c ...

  9. iphone的ibooks如何导入pdf?

    使用QQ把pdf文档从电脑上发到手机上,使用手机的QQ打开文档,在手机QQ上,用其他应用打开文档,选择‘拷贝’到ibooks

  10. 原想着mysql里放些文件什么的,查完资料还是算了

    三种东西永远不要放到数据库里 1.图片,文件,二进制数据,文件还是放入文件服务器吧或者分布式文件系统 2.短生命期数据 3.日志文件 mysql中一张表的数据是全部在一个数据文件中的.如果大字段的数据 ...