POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)
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)
题意:
给出一棵树,求树上的最大路径异或和。
思路:
dfs得到根到节点的异或前缀和,然后把每个点的异或前缀和插入字典树中,就可以按套路,在字典树里找对应的最大异或了。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxm=;
const int maxn=;
int Laxt[maxm],Next[maxm],To[maxm],val[maxm],cnt,Xor[maxm];//dfs
int ch[maxn][],tot,ans,b[],n;//trie
int q_pow(int a,int x){ int res=;while(x){if(x&) res*=a;x>>=;a*=a;} return res;}
int read()
{
int res=; char c=getchar();
for(;c>''||c<'';c=getchar());
for(;c<=''&&c>='';res=res*+c-'',c=getchar()) ;
return res;
}
void init()
{
memset(Laxt,,sizeof(Laxt));
memset(Xor,,sizeof(Xor));
memset(ch,,sizeof(ch));
cnt=tot=ans=;
}
void add(int u,int v,int x)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
val[cnt]=x;
}
void dfs(int u,int pre,int x)
{
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=pre){
Xor[To[i]]=x^val[i];
dfs(To[i],u,Xor[To[i]]);
}
}
}
void insert(int x)
{
int Now=;
for(int i=;i<=;i++) { b[i]=x&;x>>=;}
for(int i=;i>=;i--){
if(!ch[Now][b[i]]) ch[Now][b[i]]=++tot;
Now=ch[Now][b[i]];
}
}
void find(int x)
{
int Now=,tmp=;
for(int i=;i<=;i++){ b[i]=x&; x>>=; }
for(int i=;i>=;i--){
if(ch[Now][b[i]^]) Now=ch[Now][b[i]^],tmp+=q_pow(,i);
else Now=ch[Now][b[i]];
} ans=max(ans,tmp);
}
void build()
{
for(int i=;i<=n;i++) insert(Xor[i]);
for(int i=;i<=n;i++) find(Xor[i]);
}
int main()
{
while(~scanf("%d",&n)){
init(); int u,v,x;
for(int i=;i<n;i++){
u=read();v=read();x=read();
u++;v++;
add(u,v,x); add(v,u,x);
}
dfs(,,); build();
printf("%d\n",ans);
} return ;
}
POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)的更多相关文章
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)
在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...
- POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】
题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- P4551 最长异或路径 (01字典树,异或前缀和)
题目描述 给定一棵 n 个点的带权树,结点下标从 1 开始到 N .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 输入输出格式 输入格式: 第一行一 ...
- AcWing 143. 最大异或对 01字典树打卡
在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...
- poj3764字典树路径最大异或和
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6853 Accepted: 1 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。
/** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...
随机推荐
- Canvas的效果操作及save()和restore()方法应用
平移.缩放.旋转等操作等于是,我在一个正的画布绘制好图,然后再把画布做旋转.平移.缩放等等的效果. 也就是说,我使用的X.Y坐标还是正常的坐标(没旋转.平移.缩放等之前的坐标). save()和res ...
- DICOM医学图像处理:Deconstructed PACS之Orthanc
背景: 此篇博文介绍一个开源的.基于WEB的DICOM Server软件.该开源软件完全使用C++编写,不依赖于第三方数据库(内置了SQLite数据库)或其他框架,支持RESTful API设计模式. ...
- c++中.dll与.lib文件的生成与使用的详解
两种库: • 包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.• 包含函数代码本身,在编译时 ...
- Redis安装过程
Redis在windows下安装过程 学习了:https://www.cnblogs.com/M-LittleBird/p/5902850.html 学习了:http://www.runoob.com ...
- C#如何生成release版本的程序,生成debug版本的程序
除了右击项目在生成中配置改成Release还要在顶部切换成Release
- hibernate session缓存
Session 概述 Session 接口是 Hibernate 向应用程序提供的操纵数据库的最基本的接口, 它提供了基本的保存, 更新, 删除和载入 Java 对象的方法. Session 具有一个 ...
- Android加壳native实现
本例仅在Android2.3模拟器跑通过,假设要适配其它机型.请自行研究,这里不过抛砖引玉. 0x00 在Android中的Apk的加固(加壳)原理解析和实现,一文中脱壳代码都写在了java层非常ea ...
- 如何去掉Google搜索的跳转 让你的Google搜索不被reset掉
http://www.nowamagic.net/librarys/veda/detail/389 在点击google搜索结果时,google会在结果的URL前做个跳转,且有时这个跳转地址会被墙,这样 ...
- PHP读取远程文件的4种方法
1. fopen, fread1 if($file = fopen("http://www.example.com/", "r")) {2 while(!feo ...
- java 是 传值还是传址 Pass-by-value or Pass-by-reference
原文在此,写的非常好,解答了我的疑问 http://www.javadude.com/articles/passbyvalue.htm 首先放上一段代码,我是在找寻这段代码的内部原理的时候,在stac ...