题目链接: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. 11G新特性 -- archival(long-term)backups

    在oracle 10g中,提供了backup ... keep功能来重载配置好的retention策略. 在oracle 11g中,可以重定义backup ... keep命令来创建长期保留的备份,称 ...

  2. 借着Python-3来聊聊utf-8字符集

    [关于文本文件] 文本文件也是以二进制序列的方式保存在磁盘中的,磁盘并不能保存文本:我们打开文本文件的时候之所以能看到文字,是因为 软件根据文件所用编码的字符集对文件进行解码的原因. [以utf-8字 ...

  3. CentOS 7 安装配置zabbix 3.2.8

    运行环境:CentOS 7.2 LNMP(已提前安装好此环境) 1.首先导入zabbix安装源# rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x ...

  4. 开始逐步补充下相关Web知识,很多年没搞了....

    <script type="text/javascript"> $(function(){ ShowProduct(); $("#ShowUserInfo&q ...

  5. C#获取起始位置以及添加全局资源字典

    获取起始位置 Path.Combine(AppDomain.CurrentDomain.BaseDirectory); 添加全局资源 string temp = "this is a str ...

  6. 执行start-dfs.sh后,datenode没有启动的解决办法

    执行start-dfs.sh后,datenode没有启动,很大一部分原因是因为在第一次格式化dfs后,启动并使用了hadoop,后来又重新执行了格式化命令(hdfs namenode -format) ...

  7. Mac/Linux如何查找应用所安装路径

    Linux.Mac中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. ...

  8. 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...

  9. IDEA的这八条配置你一定要改!

    一定要改的八条军规.快捷键使用idea默认的,与其他同事保存一致 自动编译开关 忽略大小写开关 智能导包开关 悬浮提示开关 取消单行显示tabs的操作 项目文件编码 滚轴修改字体大小 设置行号显示 在 ...

  10. WPF之依赖属性和附加属性

     参考资料: 一站式WPF--依赖属性(DependencyProperty)一 一站式WPF--依赖属性(DependencyProperty)二         依赖属性之我见: 这两篇文章介绍的 ...