Codeforces 766E Mahmoud and a xor trip(树形DP)
树形DP。先考虑每个点到他本身的距离和,再算所有点两两距离和。
做的时候考虑二进制拆位即可。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i(0); i < (n); ++i)
#define rep(i, a, b) for(int i(a); i <= (b); ++i)
#define LL long long const int N = 100000 + 10; int a[N];
int n, x, y;
LL dp[2][20][N];
LL ans;
vector <int> v[N]; void dfs(int x, int fa){
REP(i, 20) dp[(a[x] >> i) & 1][i][x] = 1; for (auto it : v[x]){
if (it == fa) continue;
dfs(it, x); REP(i, 20) ans += (1LL << i) * (dp[0][i][x] * dp[1][i][it] + dp[1][i][x] * dp[0][i][it]);
REP(i, 20){
dp[((a[x] >> i) & 1)][i][x] += dp[0][i][it];
dp[(((a[x] >> i) & 1)) ^ 1][i][x] += dp[1][i][it];
}
}
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i), ans += (LL)a[i];
rep(i, 1, n - 1){
scanf("%d%d", &x, &y);
v[x].push_back(y); v[y].push_back(x);
} dfs(1, 1);
printf("%lld\n", ans); return 0; }
Codeforces 766E Mahmoud and a xor trip(树形DP)的更多相关文章
- codeforces 766E Mahmoud and a xor trip
题目链接:http://codeforces.com/problemset/problem/766/E 大意,给出一个$n$个点的树,每个点都有一个权值,令$Disx$为$u$到$v$路径上的异或和求 ...
- 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个点 ...
- 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) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- 【codeforces 766E】Mahmoud and a xor trip
[题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...
- codeforces766E Mahmoud and a xor trip(按位统计+树形DP)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]
洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...
- hdu 3660 Alice and Bob's Trip(树形DP)
Alice and Bob's Trip Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- codeforces 816 E. Karen and Supermarket(树形dp)
题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...
随机推荐
- Hi3518EV300编译U-Boot和内核报错:loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed. Aborted (core dumped)
下载Hi3518EV300的SDK后编译内核和U-boot,发现爆出如下错误: scripts/kconfig/conf --silentoldconfig Kconfig Aborted (core ...
- 工具类commons-io的Tailer用来监控文件
一.前言:在Linux下有使用tail命令 在Commons-io中也提供这种方法 二.他采用的是线程方式来监控文件内容的变化 1.Tailer类(采用线程的方式进行文件的内容变法) 2.Tailer ...
- 第八届蓝桥杯C/C++ B组省赛----分巧克力
分巧克力 问题描述 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这 N 块巧 ...
- TypeError: cannot perform reduce with flexible type
想要解决这个错误,最好先明白numpy数据类型的dtype转换 生成一个浮点数组 a=np.random.random(4) 输出 a array([0.0945377,0.52199916,0.62 ...
- 洛谷P1540 机器翻译
题目链接:https://www.luogu.org/problemnew/show/P1540
- leetcode 【 Subsets 】python 实现
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...
- MFC定时关机程序的实现3-最小化到托盘栏
这个定时关机运行过后默认最小化到托盘栏最好了,不用每次都去点了. 现在来看看如何将程序显示在托盘栏. 首先在头文件里声明一个变量和一个消息响应函数 //最小化到托盘栏 //第一步,生成一个成员变量,或 ...
- adb命令模拟按键事件
//这条命令相当于按了设备的Backkey键 adb shell input keyevent 4 //可以解锁屏幕 adb shell input keyevent 82 //在屏幕上做划屏操作, ...
- centos php环境搭建
CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境 准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A ...
- Solr linux安装
1.下载安装(自行官网下载) 2.解压安装包 3.进入解压后的bin目录执行命令: ./solr start 出现如下警告: 根据提示修改solr.in.sh中的SOLR_ULIMIT_CHECKS属 ...