Xor - Trie树
题目描述
求一棵带边权的树的一条最大 Xor 路径的值。这里的“路径”不一定从根到叶子结点,中间一段路径只要满足条件也可以。
输入格式
第一行,一个整数 N ,表示一颗树有 N 个节点,接下来 N-1 行,每行三个整数 a,b,c 表示节点 a 和节点 b 之间有条权值为 c 的边。
输出格式
输出仅一行,即所求的最大值。
样例数据 1
输入 [复制]
4
1 2 3
1 3 4
1 4 7
输出
7
备注
【数据范围】
对 40% 的输入数据 :数据退化为一条链;
另对 10% 的输入数据 :N≤1000;
对 100% 的输入数据 :1≤N≤100000, c≤231-1。
题目分析
套路Xor多半是trie树(普通/可持久化)。
由于Xor的性质之 a ^ b ^ b = a, u与v之间路径的xor和就为sum[u] ^ sum[v](不必求lca)。
于是可以将每个点到根节点的路径xor和计算出来,再将每个点的sum由高位到低位插入到trie树中。
接下来扫描每个节点,对于当前节点的sum,从trie树根节点开始匹配,尽量向异或后为1的转移,取最大值即可。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 100005, L = 35;
int n, ecnt, adj[N], go[N << 1], nxt[N << 1], len[N << 1], sum[N], ans = 0;
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar())
i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
struct node{
node *ch[2];
node(){}
inline void clean(){
ch[0] = ch[1] = NULL;
}
}pool[N * L], *root = NULL, *tail = pool;
inline void dfs(int u, int f){
for(int e = adj[u], v; e; e = nxt[e]){
if((v = go[e]) == f) continue;
sum[v] = sum[u] ^ len[e];
dfs(v, u);
}
}
inline int get(int s, int k){
return (s >>(k - 1)) & 1;
}
inline void insert(int s){
node *pos = root;
for(int i = 31; i >= 1; i--){
if(!pos->ch[get(s, i)])
(pos->ch[get(s, i)] = tail++)->clean();
pos = pos->ch[get(s, i)];
}
}
inline int getAns(int k){
int ret = sum[k];
node *pos = root;
for(int i = 31; i >= 1; i--){
if(pos->ch[get(sum[k], i) ^ 1]){
pos = pos->ch[get(sum[k], i) ^ 1];
ret |= (1 << (i - 1));
}
else{
pos = pos->ch[get(sum[k], i)];
ret ^= ((get(sum[k], i)) << (i - 1));
}
}
return ret;
}
inline void addEdge(int u, int v, int w){
nxt[++ecnt] = adj[u], adj[u] = ecnt, go[ecnt] = v, len[ecnt] = w;
}
int main(){
//freopen("xor.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
n = read();
for(int i = 1; i < n; i++){
int x = read(), y = read(), z = read();
addEdge(x, y, z);
addEdge(y, x, z);
}
dfs(1, 0);
(root = tail++)->clean();
for(int i = 1; i <= n; i++) insert(sum[i]);
for(int i = 1; i <= n; i++)
sum[i]=getAns(i),ans=max(ans,sum[i]);
wr(ans);
return 0;
}
Xor - Trie树的更多相关文章
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- 51nod 1295 XOR key (可持久化Trie树)
1295 XOR key 题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查 ...
- 51nod 1295 XOR key | 可持久化Trie树
51nod 1295 XOR key 这也是很久以前就想做的一道板子题了--学了一点可持久化之后我终于会做这道题了! 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X ...
- HDU 5269 ZYB loves Xor I Trie树
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- The XOR Largest Pair (trie树)
题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1,A2,--,AN 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...
- BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】
题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...
随机推荐
- Python 极简教程(十三)while 循环
循环简单来说就是让一段代码按你想要的方式多次运行.软件拥有强大的运算能力,就是由循环提供的. 在 Python 中支持的循环由两种:while 循环 和for 循环. 现在我们先来讲while循环. ...
- 【Codeforces Round #431 (Div. 2) A】Odds and Ends
[链接]点击打开链接 [题意] 让你把一个数组分成奇数个部分. 且每个部分的长度都是奇数. [题解] 很简单的脑洞题. 开头和结尾一定要为奇数,然后 n为奇数的话,就选整个数组咯. n为偶数的话,不能 ...
- Valgrind的用法
Valgrind是执行在Linux上一套基于仿真技术的程序调试和分析工具,它包括一个内核──一个软件合成的CPU,和一系列的小工具,每一个工具都能够完毕一项任务──调试.分析,或測试等. Valgri ...
- [AngularJS] Interpolation fail in IE 11
When you occured this problem, check few things: For the input field, use // Use ng-attr-placeholder ...
- 【z02】选择客栈
(hotel.cpp/c/pas) [问题描述] 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照 某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示) ...
- 推荐一下《聊聊JVM》的专栏
依照惯例新开了一个专栏后要单推一下.推荐一下<聊聊JVM的专栏>,网上关于JVM的文章太多,这个专栏希望能在已有的资料的基础上写出点新意,对一些重要的概念归纳总结,说说自己的观点.理解和实 ...
- [Docker] Create Docker Volumes for Persistent Storage
Docker containers are stateless by default. In order to persist filesystem changes, you must use doc ...
- testng并发测试与测试并发
一.testng并发测试 通过xml文件中suit结点的parallel属性指定,如 <suite name="bundle-module-testabc" parallel ...
- keil出现蓝色小箭头
- 对象模型图(OMD)阅读指南
樱木 原文 对象模型图(OMD)阅读指南(转载) 补充几个名词概念: UML:Unified Modeling Language 统一建模语言,是用来对软件密集系统进行可视化建模的一种语言.UML为面 ...