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字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
随机推荐
- Spring5参考指南:IOC容器
文章目录 为什么使用Spring5 什么是IOC容器 配置元数据 实例化容器 XML嵌套 groovy bean定义DSL 使用容器 最近在翻译Spring Framework Documentati ...
- jax-rs下载文件
@Path("/file") public class FileService { private static final String FILE_PATH = "c: ...
- c++ 如何开N次方?速解
c++ 如何开N次方?速解 直接上代码 #include <iostream> #include <cmath> using namespace std; typedef ...
- c语言-----劫持系统03
1. 回顾 在前2节我们已经实现了劫持原理.函数指针等一些概念,下面进行系统劫持 2. 工具 vs2017 Detours 3. windows如何创建一个进程? (1)创建进程函数 CreatePr ...
- CloudCC CRM探讨:精细流程管理与员工悟性培养
很多企业主招聘时更喜欢专业的销售,来给他们创造价值.老板不愿意花时间在"磨刀上",而喜欢员工一来就"砍柴".即使是建立培训机制,仍然很大程度依赖于员工自己的悟性 ...
- (PSO-BP)结合粒子群的神经网络算法以及matlab实现
原理: PSO(粒子群群算法):可以在全局范围内进行大致搜索,得到一个初始解,以便BP接力 BP(神经网络):梯度搜素,细化能力强,可以进行更仔细的搜索.数据: ...
- Radix_Sort
public class Radix_sort { public static void sort(int[] arrays,int radix){ int n = 1; int length = a ...
- 图论--网络流--最大流 HDU 3572 Task Schedule(限流建图,超级源汇)
Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...
- 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)
//非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...
- js世家委托详解
事件原理 通过div0.addElementListener来调用:用法:div0.addElementListener(事件类型,事件回调函数,是否捕获时执行){}.1.事件类型(type):必须是 ...