Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5646   Accepted: 1226

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:

⊕ 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

  1. 4
  2. 0 1 3
  3. 1 2 4
  4. 1 3 6

Sample Output

  1. 7

Hint

The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

题意:给出一颗n个节点的边权树,求一条路径(u,v),使得路径上的边的权值异或值最大

思路:我们可以先0为根,求出其他节点i到根的路径的边权异或值d[i],对于u,v之间路径的边权异或结果就是d[u]^d[v], 那么问题转化为给出n个数,求任意两个异或的最大值

把每一个数以二进制形式从高位到低位插入trie中,然后依次枚举每个数,在trie中贪心,即当前为0则向1走,为1则向0走。

一开始写动态分配节点的trie一直tle。。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <vector>
  7. using namespace std;
  8. typedef long long ll;
  9. const int N = ;
  10.  
  11. int n;
  12. struct Edge {
  13. int v, w, nex;
  14. Edge() {}
  15. Edge(int v, int w, int nex) : v(v), w(w), nex(nex) {}
  16. };
  17. Edge e[N << ];
  18. int head[N], tot;
  19. void add(int u, int v, int w) {
  20. e[tot] = Edge(v, w, head[u]);
  21. head[u] = tot++;
  22. }
  23. void read() {
  24. memset(head, -, sizeof head);
  25. tot = ;
  26. int u, v, w;
  27. for(int i = ; i < n; ++i) {
  28. scanf("%d%d%d", &u, &v, &w);
  29. add(u, v, w);
  30. add(v, u, w);
  31. }
  32. }
  33. int d[N], vis[N];
  34. void bfs() {
  35. queue<int> que;
  36. que.push();
  37. d[] = ;
  38. memset(vis, , sizeof vis);
  39. int u, v, w;
  40. vis[] = ;
  41. while(!que.empty()) {
  42. u = que.front(); que.pop();
  43. for(int i = head[u]; ~i; i = e[i].nex) {
  44. v = e[i].v; w = e[i].w;
  45. if(vis[v]) continue;
  46. d[v] = d[u] ^ w;
  47. vis[v] = ;
  48. que.push(v);
  49. }
  50. }
  51. }
  52. int ch[N * ][];
  53. struct Trie {
  54. int sz;
  55. Trie() { sz = ; memset(ch[], , sizeof ch[]); }
  56. void _insert(int bs[]) {
  57. int u = ;
  58. for(int i = ; i >= ; --i) {
  59. int c = bs[i];
  60. if(!ch[u][c]) {
  61. memset(ch[sz], , sizeof ch[sz]);
  62. ch[u][c] = sz++;
  63. }
  64. u = ch[u][c];
  65. }
  66. }
  67. int _search(int bs[]) {
  68. int u = , ans = ;
  69. for(int i = ; i >= ; --i) {
  70. int c = bs[i];
  71. if(c == ) {
  72. if(ch[u][]) { ans += ( << (i)); u = ch[u][]; }
  73. else u = ch[u][];
  74. }else {
  75. if(ch[u][]) { ans += ( << (i)); u = ch[u][]; }
  76. else u = ch[u][];
  77. }
  78. }
  79. return ans;
  80. }
  81. };
  82.  
  83. int ans;
  84. int b[];
  85. void get(int x) {
  86. int ls = ;
  87. memset(b, , sizeof b);
  88. while(x) {
  89. b[ls++] = x % ;
  90. x >>= ;
  91. }
  92. }
  93. void solve() {
  94. Trie mytrie;
  95. ans = ;
  96. for(int i = ; i < n; ++i) {
  97. get(d[i]);
  98. mytrie._insert(b);
  99. ans = max(ans, mytrie._search(b));
  100. }
  101. printf("%d\n", ans);
  102. }
  103. int main() {
  104. // freopen("in.txt", "r", stdin);
  105. while(~scanf("%d", &n)) {
  106. read();
  107. bfs();
  108. solve();
  109. }
  110. }

Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值的更多相关文章

  1. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  2. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  3. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  4. 51Nod - 1295:XOR key (可持久化Trie求区间最大异或)

    给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求ALL 至 ARR 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值 ...

  5. poj 1004:Financial Management(水题,求平均数)

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: ...

  6. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  7. POJ 1149:PIGS 网络流经典题

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18345   Accepted: 8354 Description ...

  8. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

随机推荐

  1. PJAX的实现与应用

    一.前言 web发展经历了一个漫长的周期,最开始很多人认为Javascript这们语言是前端开发的累赘,是个鸡肋,那个时候人们还享受着从一个a链接蹦 到另一个页面的web神奇魔术.后来随着JavaSc ...

  2. 如何安装 第三方 Go 离线包? (GOPATH、 go install)

    有时候 go get 比较慢,可以考虑用迅雷等下载工具下载下来,然后再本地安装, 如:code.google.com/p/go.net/websocket,如何安装这些离线包? 先在你的 GOPATH ...

  3. jQuery stop()用法

    jQuery stop()的用法: stop(true)等价于stop(true,false): 停止被选元素的所有加入队列的动画. stop(true,true):停止被选元素的所有加入队列的动画, ...

  4. Json.net日期格式化

    1. 全局设置,可以在App_Global中配置 JsonSerializerSettings setting = new JsonSerializerSettings(); JsonConvert. ...

  5. ajax简单应用

    var xmlhttp;if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码 xmlhttp=new ...

  6. Android selecter背景选择器使用

    android:drawable这个属性是必须的,默认时的背景图片. android:state_pressed布尔值.true指当用户点击或者触摸该控件的状态.默认为false android:st ...

  7. HttpUtil

    1.发送doPost请求,在web那边使用request.setCharacterEncoding("UTF-8");保证中文不乱码,不需要第三方jar包 public stati ...

  8. js学习进阶中-bind()方法

    有次面试遇到的,也是没说清楚具体的作用,感觉自己现在还是没有深刻的理解! bind():绑定事件类型和处理函数到DOM element(父元素上) live():绑定事件到根节点上,(document ...

  9. Mac Pro 8G 安装MyEclipse提示虚拟内存为0 安装失败

    看的一个大神的博客,一句话解决了.哈哈   百度一下很多人都说开多一点程序,让程序占满内存,使其虚拟内存使用就能通过这一步骤,但这里有个更好一点的方案 通过执行: memory_pressure -l ...

  10. 3ds max旋转简化后模型

    简化后的模型无法与原场景直接匹配,因此需要以下步骤: 简化后的模型导入,原点在右上角 旋转后,方法是选中所有模型,右键->旋转 在偏移的x和y各输入180 还有做一些平移,微调 在导入整个she ...