题目传送门:http://poj.org/problem?id=3764

The xor-longest Path

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9482   Accepted: 1932

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

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)

题目大意:

有一棵N个节点的树, 求树上的最大路径异或和。

解题思路:

静态链接表存树。

我们要求一条最大异或和的路径,暴力太恐怖了。

这里巧妙地运用了公式 a ⊕ b ⊕  a = b,也就是说相同的路径被异或两次相当于没有被异或,那么我们从根节点开始 dfs 并且每到一个点就把该点到根节点的路径异或和丢进 01字典树里,然后每次都把当前边和 01字典树里匹配得到最大的异或值(因为在同一棵树,两节点间必定有一个公共祖先,所以起点终点必定是相连的),最后遍历完一棵树得到的最大异或值就是结果。

AC code:

 ///数组实现
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
#define LL long long int
#define Bit 30
using namespace std;
const int MAXN = 1e5+; struct node{
int to, va, next;
}t[MAXN<<]; int head[MAXN];
int ch[MAXN*Bit][];
int value[Bit*MAXN];
//bool vis[MAXN];
int node_cnt, edge_cnt;
int N, ans; inline void init()
{
ans = ;
node_cnt = ;
edge_cnt = ;
memset(head, -, sizeof(head));
memset(ch[], , sizeof(ch[]));
// memset(vis, false, sizeof(vis));
}
void add_edge(int u, int v, int w)
{
t[edge_cnt].next = head[u];
t[edge_cnt].to = v;
t[edge_cnt].va = w;
head[u] = edge_cnt++;
}
void Insert(int x)
{
int cur = ;
for(int i = Bit; i >= ; i--){
int index = (x>>i)&;
if(!ch[cur][index]){
memset(ch[node_cnt], , sizeof(ch[node_cnt]));
ch[cur][index] = node_cnt;
value[node_cnt++] = ;
}
cur = ch[cur][index];
}
value[cur] = x;
}
int query(int x)
{
int cur = ;
for(int i = Bit; i >= ; i--)
{
int index = (x>>i)&;
if(ch[cur][index^]) cur = ch[cur][index^];
else cur = ch[cur][index];
}
return value[cur]^x;
}
void solve(int x,int fa, int res)
{
Insert(res);
//vis[x] = true;
for(int i = head[x]; i != -; i = t[i].next){
int v = t[i].to;
if(v == fa) continue;
//if(!vis[v]){
ans = max(ans, query(res^t[i].va));
solve(v, x, res^t[i].va);
//}
}
}
int main()
{
int a, b, c;
while(~scanf("%d", &N)){
init();
for(int i = ; i < N; i++){
scanf("%d%d%d", &a, &b, &c);
a++, b++;
add_edge(a, b, c);
add_edge(b ,a, c);
}
solve(, -, );
printf("%d\n", ans);
}
return ;
}

POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】的更多相关文章

  1. POJ 3764 The xor-longest Path (01字典树)

    <题目链接> 题目大意: 给定一颗$n$个节点$(n\leq10^5)$,有边权的树,其边权$(0\leq w < 2^{31})$.让你求出这棵树上任意两个节点之间的异或最大值. ...

  2. 2014百度之星资格赛—— Xor Sum(01字典树)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. Xor Sum---hdu4825(01字典树模板)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...

  4. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  5. [Hdu4825]Xor Sum(01字典树)

    Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问 ...

  6. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  7. HDU 4825 Xor Sum(01字典树)题解

    思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...

  8. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  9. 牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)

    牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并) 题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和 题解: 根据异或的性质我们可以得到 \ ...

随机推荐

  1. 【Tensorflow】 Object_detection之模型训练日志结果解析

    日志展示 指标说明: AP值表示正确识别物体的个数占总识别出的物体个数的百分数 AR值表示正确识别物体的个数占测试集中物体的总个数的百分数 IoU值即生成的框/掩膜与数据集中的标准的面积之交处于面积之 ...

  2. HDFS基本shell操作

    在客户端输入Hadoop fs,可以查看所有的,hadoop shell # -help [cmd] //显示命令的帮助信息,如: hadoop fs -help ls # -ls(r) <pa ...

  3. zookeeper在windows下的伪集群模式

    参考:zookeeper在windows下的伪集群模式 踩到的坑: 注意windows下路径需要使用\ dataDir=D:\Program Files\Java\zookeeper-3.4.10-c ...

  4. postgres formencode.api.Invalid

    错误提示: Invalid: expected an int in the IntCol 'geom', got <type 'str'> '010100000007EBFFFC3A611 ...

  5. mysql 存储过程(proceduce)查询一个表的结果插入另外一个表

    公司的时间戳存证业务,对发版过程中间数据处理需要用到存储过程.对此做一个简短记录,以免遗忘. DROP procedure record_timestamp_deal ; ##创建存储过程 creat ...

  6. CSS+DIV进度条

    <style type="text/css"> .Bar { position: relative; width: 200px; /* 宽度 */ border: 1p ...

  7. jsp的动作标签

    常用的标签: 1. forward   请求转发 [基本不使用] <==> request.getRequestDispatcher(url).forward(request,respon ...

  8. java项目升级spring4.3.x 、jdk1.8 、tomcat8.5遇到的坑及解决方案

    在将spring3.x 升级为4.3.x,jdk1.7 tomcat7升级到jdk1.8.tomcat8.5过程中,碰到了很多问题,也学习到了很多东西,现将这些问题分享出来,方便大家后续遇到同样问题时 ...

  9. 自己动手实现STL 01:内存配置器的实现(stl_alloc.h)

    一.前言 在STL中,容器是其中的重中之重,基本的STL中的算法,仿函数等都是围绕着容器实现的功能.而,内存配置器,是容器的实现的基础.所以,我第一次要去编写便是内存配置器的实现.在STL中,内存配置 ...

  10. bind 事件名称 命名空间

    1.通过在事件名称后面添加以点号分隔的后缀来为事件名称指派命名空间 $("#button").bind("click.editMode",function(){ ...