题目链接:http://poj.org/problem?id=3764

Time Limit: 2000MS  Memory Limit: 65536K

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:

$_{xor}length(p) = \bigoplus_{e \in p}w(e)$

$\oplus$ 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 \oplus 4$

题意:

对一棵带权树,我们定义树上的某条路径 $p$ 的“异或长度”为该路径上所有边的边权的异或值。要求你找出树上“异或长度”最长的路径,并求出其“异或长度”。

题解:

显然对于树上任意节点 $x$,我们可以用DFS递推地求出 $dist[x] = dist[par[x]] \oplus w(par[x],x)$。

我们可以枚举所有路径的两个端点 $x,y$,其路径为 $x \sim LCA(x,y) \sim y$。

那么根据异或的性质,就有 $_{xor}length(x \sim y) = dist[x] \oplus dist[y]$,因为 $_{xor}length(root \sim LCA(x,y))$ 这一段同时出现在 $dist[x]$ 和 $dist[y]$ 中,异或之后即为 $0$,正好抵消掉了。

因此,问题就转化为对于长度为 $n$ 的序列 $dist[1] \sim dist[n]$,寻找某一对 $(x,y)$ 使得 $dist[x] \oplus dist[y]$ 最大,给出最大值。该问题即CH 1602 - The XOR Largest Pair - [字典树变形]

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+; int n; struct Edge{
int u,v,w;
int next;
};
Edge E[*maxn];
int head[maxn],ne;
void init()
{
ne=;
memset(head,,sizeof(head));
}
void addedge(int u,int v,int w)
{
++ne;
E[ne].u=u, E[ne].v=v, E[ne].w=w;
E[ne].next=head[u];
head[u]=ne;
} int d[maxn];
bool vis[maxn];
void dfs(int u)
{
vis[u]=;
for(int i=head[u];i;i=E[i].next)
{
if(!vis[E[i].v])
{
d[E[i].v]=d[u]^E[i].w;
dfs(E[i].v);
}
}
} namespace Trie
{
const int SIZE=maxn*;
int sz;
struct TrieNode
{
int ed;
int nxt[];
}trie[SIZE];
void init()
{
sz=;
memset(trie,,sizeof(trie));
}
void insert(int x)
{
int p=;
for(int k=;k>=;k--)
{
int ch=(x>>k)&;
if(trie[p].nxt[ch]==) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
}
int MaxXor(int x)
{
int res=;
int p=;
for(int k=;k>=;k--)
{
int ch=(x>>k)&;
if(trie[p].nxt[ch^])
{
p=trie[p].nxt[ch^];
res|=<<k;
}
else p=trie[p].nxt[ch];
}
return res;
}
}; int main()
{
while(cin>>n)
{
init();
for(int i=,u,v,w;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u+,v+,w);
addedge(v+,u+,w);
} memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
dfs(); Trie::init();
int ans=;
for(int i=;i<=n;i++)
{
Trie::insert(d[i]);
ans=max(ans,Trie::MaxXor(d[i]));
}
cout<<ans<<endl;
}
}

POJ 3764 - The xor-longest Path - [DFS+字典树变形]的更多相关文章

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

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

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

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

  3. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  4. CH 1602 - The XOR Largest Pair - [字典树变形]

    题目链接:传送门 描述在给定的 $N$ 个整数 $A_1, A_2,\cdots,A_N$ 中选出两个进行xor运算,得到的结果最大是多少? 输入格式第一行一个整数 $N$,第二行 $N$ 个整数 $ ...

  5. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  6. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

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

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

  8. GCPC 2013_A Boggle DFS+字典树 CSU 1457

    上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛.当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好 ...

  9. nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...

随机推荐

  1. [转]抛弃jQuery,使用原生JavaScript

    原文链接 Document Ready 事件 在jQuery中,document.ready可以让代码在整个文档加载完毕之后执行: $(document).ready(function() { // ...

  2. Sort_Buffer_Size 设置对服务器性能的影响

    基础知识: 1. Sort_Buffer_Size 是一个connection级参数,在每个connection第一次需要使用这个buffer的时候,一次性分配设置的内存.2. Sort_Buffer ...

  3. synchronized和lock比较

    一.synchronized的实现方案 1.synchronized能够把任何一个非null对象当成锁,实现由两种方式: a.当synchronized作用于非静态方法时,锁住的是当前对象的事例,当s ...

  4. Android开发(二十三)——Application

    参考: [1] Android中Application类用法.http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.htm ...

  5. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-6底层驱动之多路开关选择器

    视频简介:该视频介绍iCore3应用开发平台中多路开关选择器的应用,多路开关选择器的硬件电路连接以及软件实现的方法. 源视频包下载地址:链接:http://pan.baidu.com/s/1geQ4W ...

  6. 【webssh】网页上的SSH终端

    [webssh] ——记两天来比较痛苦的历程 广义上来说,webssh泛指一种技术可以在网页上实现一个SSH终端.从而无需Xshell之类的模拟终端工具进行SSH连接,将SSH这一比较低层的操作也从C ...

  7. ubuntu将GNU nano换成vim

    ubuntu默认编辑器为gnu nano,使用方法是ctrl+字母键 但是gnu nano没有vim好用,我们把它切换到vim 在终端下输入以下命令 sudo update-alternatives ...

  8. Java知多少(77)日期和时间类

    Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...

  9. linux下用/proc/stat文件来计算cpu的利用率-c语言实现

    proc文件系统介绍 /proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为内核与进程提供通信的接口.用户和应用程序可以通过/proc得到系统的信息,并可以改变内 ...

  10. (笔记)Linux内核学习(二)之进程

    一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程而不是进程.对 ...