Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
来源:互联网 发布:淘宝开情趣用品店 编辑:程序博客网 时间:2024/06/03 17:54
题目连接:http://codeforces.com/contest/766/problem/E
——————————————————————————.
——————————————————————————.
题目大意:
就是在一个生成树上求任意两点的距离的和,距离定义为两点间路上所有点权值的异或和,
解题思路:
想法就是将结果的异或和拆分成每一位的,就是对于二进制数上对于第i位,有多少个距离为1的路径,然后在总结果上
cnt[][0]是以u为根0的个数,cnt[][1]同理.
听闻这题还有树分治和树dp的做法,有兴趣可以百度一发.
附本题代码
——————————————————————————.
int a[N],b[N],cnt[N][2];vector<int >E[N];LL sum ;void dfs(int u,int fa){ cnt[u][0]=cnt[u][1]=0; cnt[u][b[u]]++; sum+=b[u]; int v; for(int i=0;i<E[u].size();i++){ v = E[u][i]; if(v==fa) continue; dfs(v,u); sum+=cnt[u][0]*cnt[v][1]+cnt[u][1]*cnt[v][0]; cnt[u][b[u]]+=cnt[v][0]; cnt[u][b[u]^1]+=cnt[v][1]; }}int main(){ int n,u,v; n = read(); Rep(i,1,n) a[i]=read(),E[i].clear(); Rep(i,2,n){ u=read(),v=read(); E[u].pb(v); E[v].pb(u); } LL ans = 0ll; Rep(i,0,20){ Rep(j,1,n){ if(a[j]&(1<<i)) b[j]=1; else b[j]=0; } sum = 0ll; dfs(1,-1); ans += sum*(1<<i); } printf("%I64d\n",ans); return 0;}
对于对树、图进行dfs的要好好练练 总是搜不明白.
0 0
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- 【Codeforces 766E】 Mahmoud and a xor trip 【二进制计数】
- Codeforces Round #396(Div. 2)E. Mahmoud and a xor trip【二进制拆位+Dfs处理】
- 766E Mahmoud and a xor trip[二进制拆位][dfs]
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip(树形dp+二进制按位拆分)
- Codeforces Round #396(Div. 2)E. Mahmoud and a xor trip
- codeforces round 396 div2 E Mahmoud and a xor trip 按位操作+dp子树内外
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
- E. Mahmoud and a xor trip----树形DP
- Mahmoud and a xor trip——766E 树形dp
- 【题解】codeforces766E Mahmoud and a xor trip
- CF766E:Mahmoud and a xor trip(树形dp)
- Codeforces766E Mahmoud and a xor trip(按位+树形DP)
- Mahmoud and Ehab and the xor CodeForces
- Mahmoud and Ehab and the xor CodeForces
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor 【位运算+思维】
- Codeforces Round #435 (Div. 2)-异或规律&思维-Mahmoud and Ehab and the xor
- data imputation —— Python Data Science Cookbook
- fzuoj 2150 Fire Game BFS
- 496. Next Greater Element I \ 503. Next Greater Element II
- CentOS6下通过OwnCloud搭建个人云盘
- DB9 公头母头引脚定义及连接
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- qt学习之路二(一个可切换的界面)
- codeforces 766C Mahmoud and a Message(基础dp)
- 杭电2000——ASCII码排序
- linux学习记录02
- 使用Cordova进行iOS开发 (第三方插件的使用:Camera插件)
- 快速排序
- Picasso图片加载框架的使用及加载网络图片等比例缩放
- Mini2440 裸机和带系统实验同时进行