【codeforces 766E】Mahmoud and a xor trip
【题目链接】:http://codeforces.com/contest/766/problem/E
【题意】
定义树上任意两点之间的距离为这条简单路径上经过的点;
那些点上的权值的所有异或;
求任意两点之间的距离和;
【题解】
权值最大为1e6
所以每个点的权值的二进制形式最多20位左右;
则我们可以对权值的二进制形式的每一位独立考虑;
我们枚举第i位;
并且在计算的时候只考虑这第i位;
可以做树形dp;
算出穿过当前这个节点的路径(并且以其为lca->最高点)
异或和的二进制形式在第i为上权值为1的路径的个数x;
则(1<<i)∗x就是答案了;
累加这个答案就好;
这里穿过当前这个节点且路径的距离(异或和)在第i位的权值为1;
也就是说剩余的节点,要么左边异或和为0且右边异或和为1或者是左边疑惑和为1右边疑惑和为0;同时为1或同时为0都不行;
记录每个节点下到该节点的异或和第i位为0和1的路径个数就好;这个很容易维护的;
当然因为有说起点和终点可以相同;所以一开始累加a[i]值;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;
int n,a[N],bit;
LL ans = 0,cnt[N][2];
vector <int> G[N];
void dfs(int x, int fa) {
int t = (a[x] >> bit) & 1;
cnt[x][t] = 1, cnt[x][1 - t] = 0;
for (int y : G[x]){
if (y == fa) continue;
dfs(y, x);
ans += ((cnt[x][0] * cnt[y][1] + cnt[x][1] * cnt[y][0])<<bit);
cnt[x][t ^ 1] += cnt[y][1];
cnt[x][t ^ 0] += cnt[y][0];
}
}
int main(){
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n) rei(a[i]),ans+=a[i];
rep1(i, 1, n - 1) {
int x, y;
rei(x), rei(y);
G[x].ps(y), G[y].ps(x);
}
for (bit = 0;bit <= 22;bit++) dfs(1, 0);
printf("%lld\n", ans);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 766E】Mahmoud and a xor trip的更多相关文章
- 【codeforces 766D】Mahmoud and a Dictionary
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766B】Mahmoud and a Triangle
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766C】Mahmoud and a Message
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 766E Mahmoud and a xor trip(树形DP)
题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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 242E】XOR on Segment
[原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...
随机推荐
- Android 自带Base64加密解密
Android项目引用不到以下两个java类 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; Android有自己的base ...
- 发布 Windows 服务
1. 如何新建 Windows 服务 打开VS,“新建项目”-->“windows 桌面”-->“windows 服务”: http://www.cnblogs.com/sorex/arc ...
- [转]Linux之ACL权限
转自:http://www.2cto.com/os/201110/108736.html 引言 前面的内容中,我们讲到传统的权限仅有三种身份(owner,group,others)搭配三种权限(r,w ...
- Mybatis的Dao向mapper传多个参数(三种解决方案)
第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...
- 关于java.util.properties的随笔
public class Propertiesextends Hashtable<Object,Object> Properties 类表示了一个持久的属性集.Properties 可保存 ...
- SQL Server应用模式之OLTP系统性能分析
OLTP系统的最大特点,是这类应用里有大量的,并发程度比较高的小事务,包括SELECT.INSERT.UPDATE和DELETE. 这些操作都比较简单,事务时间也不会很长,但是要求的返回时间很严格,基 ...
- js加减乘除在线计算器代码
js加减乘除在线计算器代码 在线演示本地下载
- Oracle12C用户创建、授权、登录
Oracle12C用户创建.授权.登录 1.以系统用户登录 C:\Users\LEI>sqlplus sys/dwh as sysdba; SQL*Plus: Release 12.1.0.2. ...
- [Android]异常5-throwable:java.lang.OutOfMemoryError: pthread_create
背景:线程初始化耗时任务 异常原因: 可能一>多个new Thread()嵌套 解决办法有: 解决一>使用Handler分离new Thread()嵌套 注: 06-30 09:12:26 ...
- WEB开发模式浅析
WEB技术随着互联网的崛起而崛起,又随着移动互联网的发展而呈现更加多样化的趋势. 黑暗时代:大约在2005年以前,所谓的WEB开发主要还是美工的活,HTML/CSS占主导,Dreamwaver做为页面 ...