题目传送门: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. Python Fileinput 模块

    作者博文地址:http://www.cnblogs.com/liu-shuai/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...

  2. opensuse 安装oracle 界面乱码

    执行.runInstall时,出现界面乱码 export LANG=en_US export LC_ALL=en_US 终端里 执行这两句.用英文界面安装 再执行 .runInstall

  3. Whu 1604——Play Apple——————【博弈】

    Problem 1604 - Play Apple Time Limit: 1000MS   Memory Limit: 65536KB   Total Submit: 442  Accepted: ...

  4. Aaja.pro 未定义

    问题描述:安装新系统后,将代码迁至新系统,所有用到ajaxpro框架调用ajax方法时均报“xx未定义”的错: 解决问题的过程 : 1.看看你在前台调用的方法的命名空间,方法名和后台的是否对应.在后台 ...

  5. JavaScript的作用域(Scope)和上下文(Context)

    JavaScript对于作用域(Scope)和上下文(Context)的实现是这门语言的一个非常独到的地方,部分归功于其独特的灵活性. 函数可以接收不同的的上下文和作用域.这些概念为JavaScrip ...

  6. MySQL8.0加载文件内容报错: ERROR 1148: The used command is not allowed with this MySQL version

    mysql数据库将文件内容加载到表中报错: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINAT ...

  7. Csharp:user WebControl Read Adobe PDF Files In Your Web Browser

    namespace GeovinDu.PdfViewer { [DefaultProperty("FilePath")] [ToolboxData("<{0}:Sh ...

  8. SharePoint - Templates & Definitions

    1. <ListTemplate>元素的SecurityBits属性 Optional Text. Defines the item-level permissions in the li ...

  9. Div+Css布局教程(-)CSS必备知识

    目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...

  10. wxPython控件学习之wx.grid.Grid 表格控件

    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...