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] ...
随机推荐
- Android 你应该注意的开发规范
本文由Blankj投稿. Blankjd的博客地址: http://www.jianshu.com/u/46702d5c6978 为了利于项目维护以及规范开发,促进成员之间Code Review的效率 ...
- 【转载】Java NIO学习
这篇文章介绍了NIO的基本概念: http://www.iteye.com/magazines/132-Java-NIO Java NIO提供了与标准IO不同的IO工作方式: Channels and ...
- ajax加载时的进度条
运行效果如下图,pc和移动都可以展示,调用方法很简单,开始调用:loading.baosight.showPageLoadingMsg(false),false代表不现实加载说明,true展示加载说明 ...
- STM32串行通信USART解说笔记
STM32串行通信USART程序例举链接:http://blog.csdn.net/dragon12345666/article/details/24883111 1.STM32串行通信USART的相 ...
- Windows下编程2----- C语言常用函数举例
几个小函数 1. //MessageBoxA(0,"网络故障,重新登录","qq error",3); //弹出对话框 2. //ShellExec ...
- 【Access2007】Access2007的打开方式
Access2007提供了多种打开方式 仅仅读与非仅仅读就不用说了,就是能编辑与不可以编辑的差别 是否以独占的方式打开是Access2007的打开方式的核心 这里什么都没有写的打开是指以"共 ...
- Regex 手机号 座机 正則表達式
近期在工作中须要推断一个号码是否是手机号,是否是座机号. 在网上也搜到了大家总结的方法,没有直接使用这些方法是由于:手机号码在不断開始新的号码段(比方17x).座机号中个别区号由于行政区域的变化而废除 ...
- fuser - identify processes using files or sockets
FUSER(1) User Commands FUSER(1) NAME fuser - identify processes using files or sockets SYNOPSIS fuse ...
- Java泛型擦除
Java泛型擦除: 什么是泛型擦除? 首先了解一下什么是泛型?我个人的理解:因为集合中能够存储随意类型的对象.可是集合中最先存储的对象类型一旦确定后,就不能在存储其它类型的对象了,否则,编译时不会报错 ...
- 03 svn 权限与用户管理
一:权限管理 (1)svn仓库各个作用 svnserve.conf [svn仓库的配置文件] password [svn仓库账号和密码配置文件] authz [svn仓库的访问权限] (2)访问权限 ...