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个点的树,每个点有点权,求所有不同的路径异或和的总和
题解:
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 第4节 hive调优:1、2、fetch抓取和表的优化
hive的调优:第一个调优:fetch抓取,能够避免使用mr的,就尽量不要用mr,因为mr太慢了 set hive.fetch.task.conversion=more 表示我们的全局查找,字段查找, ...
- IDEA、Eclipse快捷键对比
IDEA.Eclipse快捷键对比 序号 功能 IDEA Eclipse 1 很多功能:导入包,处理异常,强转cast Alt+Enter 2 导入包,自动修正??? Ctrl+Enter 3 ...
- H5实现调用本地摄像头实现实时视频以及拍照功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- [JOYOI] 1051 选课
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校 ...
- <Redis> 入门六 主从复制方式的集群
1.集群如何操作 现在有三台虚拟机,ip分别为100,105,106,将100作为master,其他两台作为slave 1.vim redis.conf 以前的版本是 slaveof <mast ...
- mac apache 配置
mac系统自带apache这无疑给广大的开发朋友提供了便利,接下来是针对其中的一些说明 一.自带apache相关命令 1. sudo apachectl start 启动服务,需要权限,就是你计算机的 ...
- php使用命名空间时自动加载机制
命名空间主要为了解决用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲突.不过并不是你定义了使用命名空间的类,就可以在任何地方随意使用了,需要在程序运行时将定义命名空间的类文 ...
- [Python3网络爬虫开发实战] 1.5.2-PyMongo的安装
在Python中,如果想要和MongoDB进行交互,就需要借助于PyMongo库,这里就来了解一下它的安装方法. 1. 相关链接 GitHub:https://github.com/mongodb/m ...
- Python之函数作业
Python之函数作业 爬页面 #爬虫页面,send一次爬一次 from urllib.request import urlopen def get(): while True: url = yiel ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...