Mahmoud and a xor trip——766E 树形dp
来源:互联网 发布:光伏模拟软件 编辑:程序博客网 时间:2024/06/02 01:37
E. Mahmoud and a xor trip
第一眼好假啊。。
题意:给定一棵树,树上的点有权值,求所有无序点对(x,y)间路径的和,a->b的路径定义为a到b经过的所有的权值异或值
第一眼还以为可以n^2*lg跑啊。。然而数据范围打破了我美好的梦想- -
N^2*lg就是枚举两个点然后算一个lca,把Lca的贡献算上,然后1->x的路径xor1->y的路径嘛
然而n=100000???
怎么做
不会。
嗯。。看了题解。。好神奇啊
我们考虑每一个二进制位的影响,进行树形dp
每次dp只处理这一位
然后怎么dp呢?
我们用num[x][1]表示从x出发,向下走,共有多少条异或为1的路径,num[x][0]同理
因为我们只要找到x出发的两条路径,一条为1,一条为0,则该位对答案的贡献有2^i,i表示枚举第i位
所以树形dp下去,乘法原理算一下就可以了
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <string>#include <map>#include <cstring>#include <ctime>#include <vector>#define inf 1e9#define ll long long#define For(i,j,k) for(ll i=j;i<=k;i++)#define Dow(i,j,k) for(ll i=k;i>=j;i--)using namespace std;inline void read(ll &tx){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} tx=x*f; }inline void write(ll x){ if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10); putchar(x%10+'0'); }inline void writeln(ll x){write(x);puts("");}using namespace std;ll ans;ll num[1000001][2];ll n,a[1000001],x,y,poi[1000001],nxt[1000001],f[1000001],cnt;inline void add(ll x,ll y){poi[++cnt]=y;nxt[cnt]=f[x];f[x]=cnt;}inline void dfs(ll x,ll fa,ll now){ll one=(a[x]>>now)&1;num[x][one]=1;num[x][one^1]=0;ll tmp=0;for(ll i=f[x];i;i=nxt[i]){ll t=poi[i];if(t==fa)continue;dfs(t,x,now);tmp+=num[t][0]*num[x][1]+num[t][1]*num[x][0];num[x][one^1]+=num[t][1];num[x][one]+=num[t][0];}ans+=(ll)tmp*(1LL<<now);}int main(){read(n);For(i,1,n)read(a[i]),ans+=(ll)a[i];For(i,1,n-1)read(x),read(y),add(x,y),add(y,x);For(i,0,31)dfs(1,0,i);writeln(ans);}
阅读全文
2 0
- Mahmoud and a xor trip——766E 树形dp
- E. Mahmoud and a xor trip----树形DP
- CF766E:Mahmoud and a xor trip(树形dp)
- 【Codeforces 766E】 Mahmoud and a xor trip 【二进制计数】
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip(树形dp+二进制按位拆分)
- Codeforces766E Mahmoud and a xor trip(按位+树形DP)
- codeforces round 396 div2 E Mahmoud and a xor trip 按位操作+dp子树内外
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- 766E Mahmoud and a xor trip[二进制拆位][dfs]
- 【题解】codeforces766E Mahmoud and a xor trip
- Codeforces Round #396(Div. 2)E. Mahmoud and a xor trip【二进制拆位+Dfs处理】
- Codeforces Round #396(Div. 2)E. Mahmoud and a xor trip
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
- 【DP】 Mahmoud and a Message
- Codeforces 766C Mahmoud and a Message 基础DP
- codeforces 766 c Mahmoud and a Message(dp)
- 【Codeforces 766 C Mahmoud and a Message】 + dp
- 每天一个Linux命令(58):telnet命令
- Coderforces Taxi
- 用Composer搭建PHP框架(一)
- 【matlab 圆周率计算】matlab 求圆周率的两种算法实现比较
- 如何在windows 7右下角的时间中同时显示星期
- Mahmoud and a xor trip——766E 树形dp
- No keyword with name 'Open Browser' found(解决办法)
- 4.工作流学习——Activiti流程实例、任务管理四步曲
- C#的静态构造函数(主要说的是编译器为Lambda表达式生成的类)
- md5`加密1:java简单实现
- mysql使用kill命令解决死锁问题,杀死某条正在执行的sql语句
- 每天一个Linux命令(59):rcp命令
- linux常用命令
- FastDFS学习笔记-- day01 介绍