题目传送门: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. 使用urllib和http.cookiejar获取python老男孩学员成绩

    打开http://crm.oldboyedu.com/crm/grade/single/ 鼠标右键查看源代码,可以看到我们需要post的name.如下: 这里需要在post试提交token和searc ...

  2. HDU 4460 Friend Chains

    Problem Description For a group of people, there is an idea that everyone is equals to or less than ...

  3. OpenGL Loading

    什么是 OpenGL loading? OpenGL是一份API规范,并不是一个库.记住这点非常重要!它意味着每一个API背后的具体实现都依赖于你的GPU硬件.操作系统以及显卡驱动. OpenGL规范 ...

  4. 通过TLS回调函数的反调试

    下面是TLS数据结构的定义 typedef struct _IMAGE_TLS_DIRECTORY { DWORD StartAddressOfRawData; DWORD EndAddressOfR ...

  5. one-vs-all案例

    使用one-vs-all初始手写字母识别 数据特点 每一个图片都是20 x 20的像素矩阵,但是在输入的样本中是一个1 x 400的向量,标签y在{0, 1, 2, ..., 9}之间取值 共有500 ...

  6. Windows映射网络驱动器提示错误

    问题描述:Windows映射网络驱动器的时候,提示文件和打印机共享资源处于联机状态未对连接尝试检测到做出响应 解决方法:不同情况可能不一样,我的原因是,映射的Linux,防火墙处于开启状态,关闭了就可 ...

  7. 10、选择框:ion-select

    !重点 multiple="true" 控制 选择框是 多选还是单选.true为 多选类似 checkbox. /* ---html----*/ <ion-content p ...

  8. Javascript 简单实现鼠标拖动DIV

    http://zhangbo-peipei-163-com.iteye.com/blog/1740078 比较精简的Javascript拖动效果函数代码 http://www.jb51.net/art ...

  9. wex5新增数据库

    首先是要打开Wex5   (这是废话,下面进入正题..) 1.第一步,找到界面中的 ”窗口” 点击打开,你会看到一个 “ 首选项 ”按照流程也要打开 (囧),,,,,,,,看图为重 2.当你打开了 “ ...

  10. VUE-地区选择器(V-Distpicker)

    V - Distpicker 地区选择器环境问题不多说,自己看文档,主要讲一下在实际使用过程中如何将下拉框的值赋值到对象属性上.文档: https://distpicker.pigjian.com/g ...