题目链接 Mahmoud and a xor trip

树形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)的更多相关文章

  1. codeforces 766E Mahmoud and a xor trip

    题目链接:http://codeforces.com/problemset/problem/766/E 大意,给出一个$n$个点的树,每个点都有一个权值,令$Disx$为$u$到$v$路径上的异或和求 ...

  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) 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 ...

  4. 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 ...

  5. 【codeforces 766E】Mahmoud and a xor trip

    [题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...

  6. codeforces766E Mahmoud and a xor trip(按位统计+树形DP)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]

    洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k​\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...

  8. 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 ...

  9. codeforces 816 E. Karen and Supermarket(树形dp)

    题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...

随机推荐

  1. [译]Exactly once is NOT exactly the same

    近日学习Pulsar文档时,注意到Pulsar提到其提供的是effectively-once语义,而不是其它流计算引擎announce的exactly-once语义,并引用了Exactly once ...

  2. 【ELK】ELK安装与配置

    一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...

  3. 自动检测ARouter路由地址分组使用冲突问题

    背景 项目中使用ARouter进行路由,由于不同上层业务模块都可能会使用到同一目标的路由地址,因此,将所有业务模块的路由地址以一种类似静态常量的方式设置在Base模块中.这样,在实际目前上加上对应此地 ...

  4. Apache shiro学习总结

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  5. Asp.net自定义控件开发任我行(8)-数据集绑定

    摘要 已经有好几天没有写博客了,今天继续,前几天写到了注册自定义事件,今天我们来讲数据集绑定. 先把运行效果截个图给大家看,让大家心里也有个底.(大家要从第一章开始看起,我们每一章都是接着前面没做完的 ...

  6. 【Clone Graph】cpp

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  7. 学习python3之路的第一个小代码-----------9*9乘法表

    这个编写的简单,用两个循环迭代就行.下面就是我写的编码以及输出的结果 1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 i = 1 5 6 while i ...

  8. [oldboy-django][5python基础][内置函数]zip

    python3中,把两个或两个以上的迭代器封装成生成器,在循环遍历生成器中,不断产生元组. 如果提供的迭代器长度不对等,生成器的长度为最短迭代器的长度. # coding= utf-8 # zip 多 ...

  9. [oldboy-django][2深入django]django模板使用函数

    1 模板引入子html--include 模板引擎 - 母版 - include,导入公共的html a. 用法:{% include "pub.html" %}, pub.htm ...

  10. Block Nested-Loop 和 Batched Key Access

    官方文档:https://dev.mysql.com/doc/refman/5.7/en/bnl-bka-optimization.html BNL和BKA是MySQL 表关联的两种关联算法 比如t1 ...