题意:给一棵树(1e5),每个节点上有对应权值(0<=ai<=1e6)定义树上两个节点间的距离为路径中节点的异或,求所有节点对间的距离和(包括节点自身也作为节点对,距离为节点权值)。

解题思路:

  做了771C后这道题就有感觉了。关键在于将每个权值的二进制位拆开计算,以dp[u][i][0]和dp[u][i][1]记录到达节点u的子树上的节点的距离的第 i 位为0和1的个数有多少,维护计算就可以了。比较僵的是开始写的时候没有考虑单独一个节点作为一个节点对,所以在每个递归的最后面单独加上到全局变量res中。

  每一个递归的最开始,用局部变量num数组记录u的权值的二进制形式,同时初始化dp[u]数组。对每个子节点的遍历转移:

  先递归子节点,返回后则已计算完毕。则有如果u的第 i 位为1,dp[u][i][1]+=dp[v][i][0], dp[u][i][0]+=dp[v][i][1]; 否则dp[u][i][1]+=dp[v][i][1], dp[u][i][0]+=dp[v][i][0]; 这里应该不难理解。

  至于统计答案,只计算为1的即可,即int cnt1=dp[u][i][1]*dp[v][i][0]+dp[u][i][0]*dp[v][i][1]; res+=1LL*cnt1*(1<<i);

  见代码:(转移其实比771C好写一点感觉)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
const int N=1e5+,M=;
int head[N],nxt[N<<],to[N<<],cnt;
int n,a[N],dp[N][M][];
ll res;
void init(){
memset(head,-,sizeof(head));
res=cnt=;
}
void addEdge(int u,int v){
nxt[cnt]=head[u];
to[cnt]=v;
head[u]=cnt++;
}
void dfs(int u,int pre){
int num[M];
for(int i=;i<M;i++){
if(a[u]&(<<i)) num[i]=,dp[u][i][]=,dp[u][i][]=;
else num[i]=,dp[u][i][]=,dp[u][i][]=;
}
for(int e=head[u];~e;e=nxt[e]){
int v=to[e];
if(v==pre) continue;
dfs(v,u);
for(int i=;i<M;i++){
int cnt1=dp[u][i][]*dp[v][i][]+dp[u][i][]*dp[v][i][];
res+=1LL*cnt1*(<<i);
if(num[i]){
dp[u][i][]+=dp[v][i][];
dp[u][i][]+=dp[v][i][];
}else{
dp[u][i][]+=dp[v][i][];
dp[u][i][]+=dp[v][i][];
}
}
}
res+=a[u];
}
int main(){
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
for(int i=;i<=n;i++)
scanf("%d",a+i);
init();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
dfs(,);
printf("%I64d\n",res);
}
return ;
}

Codeforces 766E的更多相关文章

  1. codeforces 766E Mahmoud and a xor trip

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

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

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

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

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

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. PAT 1142 Maximal Clique

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  2. [bzoj4300][绝世好题] (动规)

    Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...

  3. Spring security 5 Authorize Configuration

    1. Spring Security 核心请求,认证配置类 WebSecurityConfigurerAdapter protected void configure(HttpSecurity htt ...

  4. 清北学堂模拟赛d1t1 位运算1(bit)

    题目描述LYK拥有一个十进制的数N.它赋予了N一个新的意义:将N每一位都拆开来后再加起来就是N所拥有的价值.例如数字123拥有6的价值,数字999拥有27的价值.假设数字N的价值是K,LYK想找到一个 ...

  5. 2.1 shuffle sort(洗牌)

    1.目的:将数组以随机的顺序重新排序,类似洗牌的过程 2.用途用于快速排序或者任何以划分为基础的排序中,目的是减少最坏可能性发生的概率. 3.想法1:给数组的每一个元素产生一个随机的数字作为键,然后使 ...

  6. bitset优化FLOYD HDU 3275

    Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...

  7. T5090 众数 codevs

    http://codevs.cn/problem/5090/ 时间限制: 1 s  空间限制: 1000 KB  题目等级 : 青铜 Bronze 题目描述 Description 由文件给出N个1到 ...

  8. mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

    mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 今天在linux中安装了mys ...

  9. 1. PermCheck 桃花顺检验 Check whether array A is a permutation.

    package com.code; import java.util.Arrays; public class Test04_2 { public static int solution(int[] ...

  10. 21行python代码实现拼写检查器

    引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供很好的拼写检查,比方你输入 speling,谷歌会立即返回 spelling. 前几天,看到http://norvig.com/spe ...