【题目链接】: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的更多相关文章

  1. 【codeforces 766D】Mahmoud and a Dictionary

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 766B】Mahmoud and a Triangle

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 766C】Mahmoud and a Message

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Codeforces 766E Mahmoud and a xor trip(树形DP)

    题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...

  6. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

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

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

  9. 【codeforces 242E】XOR on Segment

    [原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...

随机推荐

  1. 深度学习的seq2seq模型——本质是LSTM,训练过程是使得所有样本的p(y1,...,yT‘|x1,...,xT)概率之和最大

    from:https://baijiahao.baidu.com/s?id=1584177164196579663&wfr=spider&for=pc seq2seq模型是以编码(En ...

  2. Hadoop之父Doug Cutting:Lucene到Hadoop的开源之路

    Hadoop之父Doug Cutting:Lucene到Hadoop的开源之路 Doug Cutting,凭借自己对工作的热情和脚踏实地的态度,开创了Lucene和Nutch两个成功的开源搜索引擎项目 ...

  3. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

  4. 洛谷P1193 洛谷团队训练VS传统团队训练

    题目背景 "在中学的信息学教育领域,洛谷无疑是一个相当受欢迎的辅助网站.同时有百余所学校正在通过洛谷进行信息学竞赛(以后简称OI)的教育.洛谷之所以如此受欢迎,是因为洛谷创新的将OI教育的几 ...

  5. HDU2564 词组缩写

    2019-06-03 15:00:38 感觉有有种被坑了的感觉,这道题不难,就是一再的W,

  6. Testng1

    Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations  注释,如 @test ...

  7. 如何获取<a>标签的Id

    案例: <a id='213' href='javascript:void(0);'onclick=DealFun(this.Id);>删除</a> 解决方案: functio ...

  8. excel另存为csv

    # -*- coding: utf-8 -*- """ Created on Tue Jul 11 21:25:57 2017 import pandas as pd i ...

  9. JVM 内存区域划分

    一.运行时数据区包括哪几部分? 根据<Java虚拟机规范>的规定,运行时数据区通常包括这几个部分:程序计数器(Program Counter Register).Java栈(VM Stac ...

  10. 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...