bzoj1954: Pku3764 The xor-longest Path
来源:互联网 发布:海岛战舰升级数据 编辑:程序博客网 时间:2024/06/07 03:53
传送门
首先假设1为根,求出根到当前节点的xor距离。
然后将xor距离转化为二进制串,建立Trie树。
强制一些点在答案上,然后在Trie树上跑贪心。
显然答案是正确的。
时间复杂度O(n*32)
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<cstdlib>#define N 100005using namespace std;struct edge{int to,next,v;}e[N*2];int head[N],d[N],a[N][32],c[N*32][2],bin[32];int n,x,y,z,tot,cnt,ans,top,sum;void add(int x,int y,int v){ e[++tot]=(edge){y,head[x],v}; head[x]=tot;}void dfs(int x,int fa,int v){ d[x]=v; for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) dfs(e[i].to,x,v^e[i].v); }int main(){ scanf("%d",&n); for (int i=1;i<n;i++){ scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } dfs(1,-1,0); cnt=1; ans=0; for (int i=bin[0]=1;i<=30;i++) bin[i]=bin[i-1]*2; for (int i=1;i<=n;i++){ x=1; top=0; for (;d[i];d[i]/=2,top++) a[i][top]=d[i]%2; for (int j=30;j>=0;j--){ if (!c[x][a[i][j]]) c[x][a[i][j]]=++cnt; x=c[x][a[i][j]]; } } for (int i=1;i<=n;i++){ sum=0,x=1; for (int j=30;j>=0;j--){ if (c[x][a[i][j]^1]!=0) sum+=bin[j],x=c[x][a[i][j]^1]; else x=c[x][a[i][j]]; } ans=max(ans,sum); } printf("%d",ans);}
2 0
- [BZOJ1954]Pku3764 The xor-longest Path
- BZOJ1954 Pku3764 The xor-longest Path
- bzoj1954: Pku3764 The xor-longest Path
- 【bzoj1954】【The xor-longest Path】【trie树】
- trie-bzoj1954-poj3764-The xor-longest Path
- BZOJ P1954: Pku3764 The xor-longest Path
- 【BZOJ】【P1954】【Pku3764 The xor-longest Path】【题解】【Trie】
- 【BZOJ 1954】Pku3764 The xor-longest Path trie+贪心
- 1954: Pku3764 The xor-longest Path 0-1字典树
- The xor-longest Path
- POJ3764:The xor-longest Path
- POJ3764 The xor-longest Path
- poj3764 The xor-longest Path
- 【poj3764】 The xor-longest Path
- poj3764 The xor-longest Path
- The xor-longest Path POJ
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- JPA中分页查询的bug发现
- 配置git推送无需多次输入账号密码
- 自定义TopBar和属性封装
- 本杰明vesc电调
- Solder Mask 和Paste Mask
- bzoj1954: Pku3764 The xor-longest Path
- 《ACM程序设计》书中题目 B-02 FatMouse word
- mysql mysqldump 导出 建表sql到文本文档
- 练手——果壳网模仿
- java中list()和listfile()
- 【第一课】Java se概述 1
- json数组转化为java对象、忽视不需要转化的json字段
- 背景建模或前景检测(Background Generation And Foreground Detection) 二
- HTML5 + JS 调取摄像头拍照下载