Description

In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:
http://poj.org/problem?id=3764
⊕ is the xor operator.

We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?

Input
The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.
Output
For each test case output the xor-length of the xor-longest path.
Sample Input
4
0 1 3
1 2 4
1 3 6
Sample Output
7
Hint
The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

求出D[x]:根节点到x结点路径边权异或和
x->y路径边权异或和为D[x]^D[y]
01Trie,找最大值

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
const int N=2e5+5,M=30;
int T[N*31][2],tot;
ll End[N];
inline void Insert(ll x){int p=0,id;for(int i=M;~i;--i,p=T[p][id])if(!T[p][id=(x>>i)&1])T[p][id]=++tot;End[p]=x;
}
inline ll Find(ll x){int p=0,id;for(int i=M;~i;--i){id=(x>>i)&1;if(T[p][id^1])p=T[p][id^1];else p=T[p][id];}return End[p];
}
struct Edge{int to,next;ll val;Edge(){}Edge(int to,int next,ll val):to(to),next(next),val(val){}
}E[N*3];
int tol,head[N*3];
inline void addedge(int u,int v,ll w){E[tol]=Edge(v,head[u],w);head[u]=tol++;
}
int d[N];
void dfs(int u,int f){for(int i=head[u];~i;i=E[i].next){int v=E[i].to;if(v^f)d[v]=d[u]^E[i].val,dfs(v,u);}
}
int n,u,v;
ll ans,w;
inline void init(){memset(head,-1,sizeof(head));memset(T,0,sizeof(T));ans=tot=tol=0;
}
int main(){while(~scanf("%d",&n)){init();for(int i=1;i<n;++i){scanf("%d%d%lld",&u,&v,&w);addedge(u,v,w),addedge(v,u,w);}dfs(0,0);for(int i=0;i<n;++i)Insert(d[i]),ans=max(ans,d[i]^Find(d[i]));printf("%lld\n",ans);}return 0;
}

POJ-3764 01-Trie相关推荐

  1. Trie:hdu 4825、1251、1247、Poj 3764

    hdu 4825链接 题目意思很简单,就是要求最大异或值的数. 我们可以从二进制的最高位开始选择,不断的排除一些数.我们先假设存在某些数字的二进制数是与当前查找的数不一样的,我们进入这一部分数进行查找 ...

  2. POJ 3764 Language: The xor-longest Path (01字典树+DFS)

    传送门:POJ 3764 题目大意: 在树上找一段路径(连续)使得边权相异或的结果最大. 前置技能: 1.用链式前向星建图. 2. 01字典树的应用. 思路: 本题用 vector数组建图是会超时的, ...

  3. poj 3321 Apple Trie

    /*poj 3321 Apple Trie这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目所以我 ...

  4. POJ - 3764 The xor-longest Path(字典树性质)

    题目链接:点击查看 题目大意:给出一棵树,每条边上都有一个边权,现在问能否选择两个点,使得其间路径上的异或和最大 题目分析:直接求肯定是比较复杂的,我们可以转换一下题意,因为是一棵树,所以n个点肯定互 ...

  5. POJ 1625 Censored ( Trie图 DP 高精度 )

    题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 : 与 POJ 2778 非常相似的一道题目,如果没有做过就尝试去了解 ...

  6. POJ 2728 01分数规划

    题意: 最优比率生成树,要求生成树中的所有边的花费与所有边的长度的比值最小 题解: 01分数规划,详见http://www.cnblogs.com/proverbs/archive/2013/01/0 ...

  7. POJ 2976 01分数规划基础题目

    题意:       给你一组"数",一共n个,每个数有两个权值,价钱a[i],代价b[i],让你选择n - k使得 sigma(a[i]) / sigma(b[i]) * 100 ...

  8. E. Beautiful Subarrays(思维 01 trie 树)

    E. Beautiful Subarrays 思路 显然有ai⨁ai+1⨁--⨁an=(a1⨁a2⨁--⨁an)⨁(a1⨁a2⨁--⨁ai−1)a_i\bigoplus a_{i + 1} \bigo ...

  9. 字典树,01字典树,可持续化01字典树(总结+例题)

    目录 字典树 01字典树 字典树例题: power oj 2390: 查单词 HDU 1671 Phone List HDU 1004Let the Balloon Rise HDU 1075 Wha ...

  10. 字典树01字典树算法笔记

    1]学习了字典树之后,觉得它很明显的就是用空间来换时间,空间复杂度特别大,比如字典数单单存26个小写字母,那么每个节点的孩子节点都有26个孩子节点,字典树中的每一层都保留着不同单词的相同字母. 2]0 ...

最新文章

  1. 关于代码评审的微博讨论汇集
  2. hive 时间转字符串_2. HIVE 基本操作
  3. sap 标准委外和工序委外_SAP FICO零基础学习_0035_标准成本估算-主数据-物料主数据...
  4. 政史系列:《社会契约论》读书笔记
  5. C++数据结构之图的储存结构——十字链表
  6. 使用vbs脚本实现自动化安装GUI程序
  7. Windebug 专题
  8. VM 虚拟机 分辨率问题
  9. android+ios打印机,Android/iOS手机安装HP打印机的详细方法和操作步骤
  10. VS无法定位程序输入点于动态链接库
  11. 指定Web打印的打印机
  12. 查计算机主板,怎么查看自己电脑的主板型号是什么?主板型号查询检查方法
  13. WiFi开启热点冲突
  14. 一个文字类RPG游戏框架(走过路过别错过)C++
  15. L1-058 6翻了 (15 分)循环的妙用
  16. 计算机网络期末复习资料(一)单选题
  17. php db mssql 2008,php mssql 不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版
  18. js实现键盘数字输入
  19. Redis 基础 - 优惠券秒杀《初步优化(异步秒杀)》
  20. MM01 物料主数据批导

热门文章

  1. bootstrap之文字排版
  2. 解决微信公众号注册提示“邮箱已被占用”(亲测)
  3. 作为技术面试官,我在面试时考虑什么?
  4. 时间同步软件和相关网站
  5. 上海航芯 | 从STM32F103到ACM32F403的U盘程序移植工程
  6. Unable to load library 'xxx': Native library (linux-x86-64/xxx.so) not found in resourc 问题解决
  7. 字符串ucfirst解析
  8. 使用CreatePen()创建自定义画笔
  9. excelJs 单元格背景颜色填充
  10. 如何求100万长度的数组的中间值元素,采用定向数组只需6个毫秒的算法