POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】
题目传送门: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
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】的更多相关文章
- POJ 3764 The xor-longest Path (01字典树)
<题目链接> 题目大意: 给定一颗$n$个节点$(n\leq10^5)$,有边权的树,其边权$(0\leq w < 2^{31})$.让你求出这棵树上任意两个节点之间的异或最大值. ...
- 2014百度之星资格赛—— Xor Sum(01字典树)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- [Hdu4825]Xor Sum(01字典树)
Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum(01字典树)题解
思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- 牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并)
牛客2018国庆集训 DAY1 D Love Live!(01字典树+启发式合并) 题意:给你一颗树,要求找出简单路径上最大权值为1~n每个边权对应的最大异或和 题解: 根据异或的性质我们可以得到 \ ...
随机推荐
- 在NuoDB上运行Asterisk
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自云+社区翻译社,作者Hans362 您可能已经熟悉Asterisk,一个广泛部署的开源Telephony框架.如果你不太熟悉,你应该 ...
- 【SoapUI】http接口测试
一.接口介绍 API(Application Programming Interface,应用程序编程接口) 1.硬件接口 USB接口 硬盘接口 SD卡接口 LAN口和WAN口 CONSOLE口 .. ...
- RegExp正则表达式内容
在线测试工具:http://leaverou.github.io/regexplained/ RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法:/pattern/at ...
- Visual Studio中C++项目编译常见问题总结
1. 工程引用外部头文件 工程->属性->配置属性->C/C++ ->常规->附加包含目录:输入头文件存放目录 2. 添加lib库引用 添加lib库的路径:工程-> ...
- 初学C#——选号器
private void Form1_Load(object sender, EventArgs e) { Random x = new Random(); ); //生成一个大于等于0,小于100之 ...
- 如何用 windows+github搭建一个优美的hexo博客
1.Hexo简单介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 风一般的速度Hexo基于Nod ...
- C#简单实现读取txt文本文件并分页存储到数组
最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...
- PAT 1021 Deepest Root
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...
- java常用API之System类
System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象.System类中的都是static方法,类 ...
- 正则表达式验证问题(用户名、密码、email、身份证
实现的代码如下: <html> <head> <meta charset="UTF-8"> </head> <body> ...