Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
来源:互联网 发布:网络交换机的安装 编辑:程序博客网 时间:2024/06/03 19:29
将数拆分成二进制形式,对于节点i,用cnt[x][i][1]和cnt[x][i][0]记录以x为根节点的所有路径权值在第i位上为0和为1的个数。那么以节点i为根的树上的所有节点相互之间的路径和就可以通过cnt数组进行dp,将子树v合并的操作是通过ans[i][1] += cnt[x][i][1]*cnt[v][i][0]+cnt[x][i][0]*cnt[v][i][1]进行转移。
#include<bits/stdc++.h>using namespace std;const int N = 1e5+100;long long cnt[N][20][2];long long ans[N][20][2];int num[N];const long long MOD = 1e9+7;vector<int> G[N];int n;void qiuhe(int x){long long cns = 0;for(int i= 0;i < 20;i ++){int sum = 0;for(int j= 1;j <= n;j ++){sum += ans[j][i][1];}cns = (cns + 1ll*sum*(1ll<<i))%MOD;}cout <<x << ' '<< cns << endl;}void dfs(int x,int fa){for(int i= 0;i < G[x].size();i ++){int now = G[x][i];if(now ==fa) continue;dfs(now,x);for(int j= 0;j < 20;j ++){int a0 = ((1<<j)&num[x]) == 0;int a1 = a0^1;ans[x][j][1] += cnt[x][j][1]*cnt[now][j][0]+cnt[x][j][0]*cnt[now][j][1];cnt[x][j][1] += a0*cnt[now][j][1]+a1*cnt[now][j][0];cnt[x][j][0] += a0*cnt[now][j][0]+a1*cnt[now][j][1];}}}int main(){cin >> n;for(int i= 1;i <= n;i ++){int nn;scanf("%d",&nn);num[i] = nn;for(int j= 0;j < 20;j ++){if((1<<j)&nn) ans[i][j][1] = cnt[i][j][1] = 1;else cnt[i][j][0] = 1;}}for(int i= 1;i < n;i ++){int a,b;scanf("%d %d",&a,&b);G[a].push_back(b);G[b].push_back(a);}dfs(1,-1);long long cns = 0;for(int i= 0;i < 20;i ++){long long sum = 0;for(int j= 1;j <= n;j ++){sum += ans[j][i][1];}cns = (cns + 1ll*sum*(1ll<<i));}cout <<cns << endl;return 0;}
阅读全文
0 0
- 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 按位考虑
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip(树形dp+二进制按位拆分)
- codeforces round 396 div2 E Mahmoud and a xor trip 按位操作+dp子树内外
- 【Codeforces 766E】 Mahmoud and a xor trip 【二进制计数】
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- Codeforces Round #396 (Div. 2)Mahmoud and a Message
- Codeforces Round #396 (Div. 2)Mahmoud and a Dictionary
- Codeforces Round #396 (Div. 2)C. Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C.Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C.Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
- E. Mahmoud and a xor trip----树形DP
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor(xor运算应用)
- 【题解】codeforces766E Mahmoud and a xor trip
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- 02_Servlet的执行流程
- ajax编码及JSON使用
- 使用sql标签来实现对数据库当中记录增删改查操作
- python 类中@staticmethod,@classmethod和实例方法
- JS中为false的几种情况和typeof的返回值
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
- Android平台美颜相机/Camera实时滤镜/视频编解码/影像后期/人脸技术探索——2.1 GPUImage结构简析
- 使用ServletContextListener完成定时任务(固定频率执行)
- 多态陷阱---域与静态方法
- Docker——版本升级(三)
- JAVA 8函数式编程(三):柯里化与惰性求值
- MPEG-1 Audio 编码器
- Unity Shader入门精要笔记(六):由一个简单的顶点/片元着色器谈起
- Centos7 安装redis及其入门使用