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的优惠券需要被 ...
随机推荐
- [BSOJ2684]锯木厂选址(斜率优化)
Description 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂.木材只能按照一个方向运输:朝山下运.山脚下有一个锯木厂 ...
- 用NPOI完成公司任务(主要就是导入导出操作)
注意要先添加程序集的引用 导出到excel: public override IWorkbook writeExecl(string filePath, DataTable dt) { if (Fil ...
- Java文件 ---文件相关操作
创建文件 file.createNewFile() 注:若该文件对象未指定文件路径,则文件创建于相对路径中,即工程目录下.(“../”表示上级文件目录,相对路径前面不加“/”,eg:bin/text. ...
- OpenCV学习笔记(一) 环境配置
Visual Studio 2010 VS2010对应的OpenCV的lib文件(build\x86\vc10\lib)分为debug模式和release模式两种:debug模式牺牲速度,但能提供更多 ...
- [转]jQuery DOM Ready
一直以来,各种JS最佳实践都会告诉我们,将JS放在HTML的最后,即</body>之前,理由就是:JS会阻塞下载,而且,在JS中很有可能有对DOM的操作,放在HTML的最后,可以尽可能的保 ...
- c语言入门-02-第一个c程序开始
我们来开我们第一个c代码 #include<stdio.h> int main(){ // print num int num; num = 1; printf("%d\n&qu ...
- PostgreSQL drop database 显示会话没有关闭 [已解决]
错误重现 有时候需要删除某个数据库时,会报如下错误,显示有一个连接正在使用数据库,无法删除 ERROR: database "pilot" is being accessed by ...
- CentOS7 'Username' is not in the sudoers file. This incident will be reported
新装的 CentOS 需要安装许多软件,但是如果一开始你不是以 root 登入的话,就需要使用 sudo 进行切换,但是通常会报错如下图: 解决方法: 先用 root 用户登入系统, 打开文件 vi ...
- Leetcode 525.连续数组
连续数组 给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组. 示例 1: 输入: [0,1] 输出: 2 说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2: ...
- linux命令之grep、cut
输入: ifconfig eth0 eth0表示主机的第一块网卡. 输出: eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu inet 19 ...