E. Mahmoud and a xor trip(树DP)
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E. Mahmoud and a xor trip(树DP)相关的知识,希望对你有一定的参考价值。
E. Mahmoud and a xor trip(树DP)
给定点权树,求所有异或路径和, p ( u , v ) , p ( v , u ) p(u,v),p(v,u) p(u,v),p(v,u)只算一次。
按位计算贡献,树 d p dp dp即可。
// Problem: E. Mahmoud and a xor trip
// Contest: Codeforces - Codeforces Round #396 (Div. 2)
// URL: https://codeforces.ml/contest/766/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Date: 2021-07-27 17:02:22
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define ios ios::sync_with_stdio(false),cin.tie(0)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\\n",a[n]);
}
int n,h[N],cnt,f[N][2],a[N],k;
ll ans;
struct edge{
int to,nt;
}e[N<<1];
void add(int u,int v){
e[++cnt]={v,h[u]},h[u]=cnt;
e[++cnt]={u,h[v]},h[v]=cnt;
}
void dfs(int u,int fa){
int w=a[u]>>k&1;
f[u][w]=1,f[u][w^1]=0;
for(int i=h[u];i;i=e[i].nt){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
ans+=(1LL*f[v][1]*f[u][0]+1LL*f[v][0]*f[u][1])<<k;
f[u][1]+=f[v][w^1],f[u][0]+=f[v][w];
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),ans+=a[i];
for(int i=1;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
add(u,v);
}
for(k=0;k<20;k++) dfs(1,0);
printf("%lld\\n",ans);
return 0;
}
以上是关于E. Mahmoud and a xor trip(树DP)的主要内容,如果未能解决你的问题,请参考以下文章
E. Mahmoud and Ehab and the xor-MST(结论&Xor)
Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(预处理+二分)
codeforces 862C. Mahmoud and Ehab and the xor
CF 862C Mahmoud and Ehab and the xor(异或)
E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)