poj3764字典树路径最大异或和
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6853 | Accepted: 1464 |
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)
先求出来到节点1的,然后字典树从高位到低位。异或和的性质以及贪心?。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+;
int head[N],tot,sz,val[N];
struct node
{
int v,next,w;
} e[N<<];
struct Trie
{
int next[];
void init()
{
memset(next,,sizeof(next));
}
} Ti[N*];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].next=head[u];
e[tot].w=w;
head[u]=tot++;
e[tot] .v=u;
e[tot].next=head[v];
e[tot].w=w;
head[v]=tot++;
}
void Add(int x)
{
int u=,ind;
for(int i=; i>=; --i)
{
if((<<i)&x) ind=;
else ind=;
if(!Ti[u].next[ind])
{
Ti[u].next[ind]=++sz;
Ti[sz].init();
}
u=Ti[u].next[ind];
}
}
int get(int x)
{
int u=,num=,ind;
for(int i=; i>=; --i)
{
if((<<i)&x) ind=;
else ind=;
if(Ti[u].next[ind])
{
u=Ti[u].next[ind];
num|=(<<i);
}
else u=Ti[u].next[!ind];
}
return num;
}
void dfs(int x,int v,int fa)
{
val[x]=v;
for(int i=head[x]; i+; i=e[i].next)
{
int to=e[i].v;
if(to==fa) continue;
dfs(to,e[i].w^v,x);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
tot=sz=;
int x,y,z;
memset(head,-,sizeof(head));
for(int i=; i<n; ++i)
{
scanf("%d%d%d",&x,&y,&z);
add(x+,y+,z);
}
Ti[].init(); //必须初始化
dfs(,,);
int maxx=;
for(int i=; i<=n; ++i)
{
maxx=max(maxx,get(val[i]));
Add(val[i]);
}
printf("%d\n",maxx);
}
}
poj3764字典树路径最大异或和的更多相关文章
- AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)
在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...
- HDU4825 Xor Sum(字典树解决最大异或问题)
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...
- 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] ...
- 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 edg ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...
- 算法笔记--字典树(trie 树)&& ac自动机 && 可持久化trie
字典树 简介:字典树,又称单词查找树,Trie树,是一种树形结构,是哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较. 性质:根节点不包含字符,除根节点外每一个 ...
- cf842D 01字典树|线段树 模板见hdu4825
一般异或问题都可以转换成字典树的问题,,我一开始的想法有点小问题,改一下就好了 下面的代码是逆向建树的,数据量大就不行 /*3 01字典树 根据异或性质,a1!=a2 ==> a1^x1^..^ ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
随机推荐
- java并发中ExecutorService的使用
文章目录 创建ExecutorService 为ExecutorService分配Tasks 关闭ExecutorService Future ScheduledExecutorService Exe ...
- cobbler的网页操作
需求:安装一台服务器 1.指定两块网卡一块外网一块内网2.内网ip10.0.0.62外网为172.16.1.623.主机名为m02 开始吧! 1.添加镜像文件 2.创建ks文件 编写ks文件 附:ks ...
- 当setWidth()和setHeight()方法不起作用时
当在Android开发中用方法setWidth()和setHeight()动态设置控件的宽高时,当被改后的宽高小雨原来的宽高时,这两个方法将不会生效. 解决办法: 1 2 3 4 LayoutPara ...
- Notations
下面四种记号是为了建立函数间的相对级别. CLRS上的一张图很直观: 大O记号 定义:如果存在正常数\(c\)和\(n_0\),使得当\(N\ge n_o\)时\(T(N)\le cf(N)\),记\ ...
- Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)
Hanh lives in a shared apartment. There are nn people (including Hanh) living there, each has a priv ...
- ACM学习总结 6月11日
经过这几天没有队友的协助,又是算法题比较多,有点碰触到自己的短板,因为搜索的题目就做了1个,一遇到搜索就跳过,DP也有点忘得差不多了,四边形优化,斜率优化还不会,这是下一阶段努力方向,把之前做过的题, ...
- CodeForces - 224C. Bracket Sequence (栈模拟)简单做法
A bracket sequence is a string, containing only characters "(", ")", "[&quo ...
- jQuery简单竖排手风琴折叠菜单代码
项目需求1.刚开始只显示,每个标题, 2.让每个 li列表隔行换色 3.当我点击某个标题时,下面的列表会缓慢的展开,其他列表展开的内容会收起 <!DOCTYPE html> <htm ...
- 【T-SQL】基础 —— 语法(1)
USE master--检查是否已经存在一个表,如果有就删除IF(EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ' ...
- 【Scala】代码实现Scala的各种模式匹配操作
文章目录 内容匹配 类型匹配 s表达式 case class 样例类 偏函数 内容匹配 import scala.util.Random object TestMatch { def main(arg ...