题目链接: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. 12C -- ORA-28040

    新安装的12.2数据库,尝试连接数据库的时候,报ora-28040错误: 这是由于12C数据库默认参数(默认支持的客户端版本)设置的原因. 在12C中,SQLNET.ALLOWED_LOGON_VER ...

  2. GuavaCache学习笔记一:自定义LRU算法的缓存实现

    前言 今天在看GuavaCache缓存相关的源码,这里想到先自己手动实现一个LRU算法.于是乎便想到LinkedHashMap和LinkedList+HashMap, 这里仅仅是作为简单的复习一下. ...

  3. [转]设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识

    原文地址:http://www.vonwei.com/post/UniqueDeviceIDforWindows.html 唯一的标识一个设备是一个基本功能,可以拥有很多应用场景,比如软件授权(如何保 ...

  4. Atitit 常用sdk 模块 组织架构切分 规范与范例attilax总结

    Atitit 常用sdk 模块 组织架构切分 规范与范例attilax总结 常用200个模块 2017/04/12  22:01    <DIR>          acc 2017/04 ...

  5. docker的swarm介绍

    转载自:https://blog.csdn.net/karamos/article/details/80132082 另外一篇:https://www.jianshu.com/p/9eb9995884 ...

  6. 【Big Data - Hadoop - MapReduce】通过腾讯shuffle部署对shuffle过程进行详解

    摘要: 通过腾讯shuffle部署对shuffle过程进行详解 摘要:腾讯分布式数据仓库基于开源软件Hadoop和Hive进行构建,TDW计算引擎包括两部分:MapReduce和Spark,两者内部都 ...

  7. Oracle分割字符串 REGEXP_SUBSTR用法

    分割字符串中所有指定字符,然后成多行参数说明,参数1: 待分割字符串参数2:正则表达式参数3:起始位置,从第几个字符开始正则表达式匹配(默认为1)参数4:标识第几个匹配组,默认为1参数5:模式('i' ...

  8. 命名实体识别,使用pyltp提取文本中的地址

    首先安装pyltp pytlp项目首页 单例类(第一次调用时加载模型) class Singleton(object): def __new__(cls, *args, **kwargs): if n ...

  9. Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法 关于dubbo服务的 ...

  10. 【01月22日】A股滚动市盈率PE最低排名

    深康佳A(SZ000016) - 滚动市盈率PE:1.55 - 滚动市净率PB:1.03 - 滚动年化股息收益率:4.71% - - - 深康佳A(SZ000016)的历史市盈率走势图 华菱钢铁(SZ ...