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的优惠券需要被 ...
随机推荐
- 版本控制之GitHub — — 第一步的理解
GitHub是时下最流行的版本控制的一门“技术”,此之前svn(subversion)也是同样的作用. 至于版本控制:Git是分布式的,而svn是中心式的(或者叫集中式的)版本控制系统,这是两者之间理 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- SparkSQL查询程序的两种方法,及其对比
import包: import org.apache.spark.{SparkConf, SparkContext}import org.apache.spark.rdd.RDDimport org. ...
- laravel5.2总结--csrf保护
CSRF攻击: CSRF 顾名思义,是伪造请求,冒充用户在站内的正常操作.我们知道,绝大多数网站是通过 cookie 等方式辨识用户身份(包括使用服务器端 Session 的网站,因为 S ...
- 如何使用PowerShell管理Windows服务
[TechTarget中国原创] 作为一名系统管理员,最常见的任务之一就是学会管理Windows服务,这是保证Windows服务器和客户端正常运行的重要内容. 许多操作系统和应用程序都依赖于这些服务. ...
- UNIX 系统中 wc 程序的主要部分
以下代码为 UNIX 系统中 wc 程序的骨干部分 #include <stdio.h> #define IN 1 #define OUT 0 int main(int argc, cha ...
- Spring AOP Example 文件下载:
文件下载:http://files.cnblogs.com/wucg/spring_aop_excise.zip P:124 spring核心技术 P225: spring doc 可以把Advi ...
- oracle存储过程、声明变量、for循环
oracle存储过程.声明变量.for循环 1.创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out t ...
- ip地址正则表达式
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$') if p.match(dom ...
- 解决IIS无法访问远程映射目录
由于影像系统(PACS)需要存储大量影像文件动辄几十T,所以经常会用到远程影像的独立文件盘, 挂载了远程映射盘后,本机可以直接访问 IIS中就直接报错了 . A机器:IIS WEB服务器 B机器:文件 ...