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 ...
随机推荐
- NYOJ 552 小数阶乘
小数阶乘 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 编写一个程序,求一个数m的阶乘. 输入 有多组測试数据,以EOF结束. 每组測试数据有1个整数m. 输出 每 ...
- 中英文对照 —— 标点符号(punctuation)
有限的几个: What Are the Fourteen Punctuation Marks in English Grammar? period:句号:comma:逗号:冒号:colon:分号:se ...
- Hypervisor scheduler
Techniques for configuring a hypervisor scheduler to make use of cache topology of processors and ph ...
- 【例题 6-8 UVA - 548】Tree
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 后序遍历的最后一个是根节点. ->然后在中序遍历中找到这个节点. 分为左右两段. 然后递归上述操作就好. 题目描述好坑啊. 原 ...
- GDB如何调试没有符号表(未加-g选项的编译)的程序
/********************************************************************* * Author : Samson * Date ...
- 【3005】拦截导弹问题(noip1999)
Time Limit: 3 second Memory Limit: 2 MB 某国为了防御帝国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然他的第一发炮弹能达到任意的高度,但是 ...
- Your algorithm's runtime complexity must be in the order of O(log n).
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- hadoop容灾能力测试 分类: A1_HADOOP 2015-03-02 09:38 291人阅读 评论(0) 收藏
实验简单来讲就是 1. put 一个600M文件,分散3个replica x 9个block 共18个blocks到4个datanode 2. 我关掉了两个datanode,使得大部分的block只在 ...
- 用strace排查故障的5种简单方法(每日一译)
原文链接:5 simple ways to troubleshoot using Strace 我很意外大部分人都不知道如何使用strace.strace一直是我的首选debug工具,因为它非常的有效 ...
- blob-照片转换与展示
File转java.sql.Blob(照片)Struts2 public Blob photos(File zp) { Blob photo=null; try { FileInputStream f ...