题目描述

求一棵带边权的树的一条最大 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树的更多相关文章

  1. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  2. 51nod 1295 XOR key (可持久化Trie树)

    1295 XOR key  题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个长度为N的正整数数组A,再给出Q个查 ...

  3. 51nod 1295 XOR key | 可持久化Trie树

    51nod 1295 XOR key 这也是很久以前就想做的一道板子题了--学了一点可持久化之后我终于会做这道题了! 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X ...

  4. 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 ...

  5. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  6. 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 ...

  7. Poj 3764 The xor-longest Path(Trie树+xor+贪心)

    The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...

  8. The XOR Largest Pair (trie树)

    题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1​,A2​,--,AN​ 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...

  9. BZOJ5338 [TJOI2018] Xor 【可持久化Trie树】【dfs序】

    题目分析: 很无聊的一道题目.首先区间内单点对应异或值的询问容易想到trie树.由于题目在树上进行,case1将路径分成两段,然后dfs的时候顺便可持久化trie树做询问.case2维护dfs序,对d ...

随机推荐

  1. 有关Canvas的一点小事—canvas和resize

     之前就说了canvas设置大小的时候用的就是设置实打实的像素值,像图像一样设置百分比然后根据浏览器大小自己适应大小是不可能的——当然一般也不会想要cavans改变大小.不过项目之前有用到过,既然去了 ...

  2. 一个Java8模型的batch队列

    有点小问题,cpu过高,但是思路不错: http://www.tuicool.com/articles/URz2i2q

  3. Windows下合并tar分卷

    如有例如以下几个tar分卷:logs.tar.gza1.logs.tar.gza2.logs.tar.gza3.在Windows下怎样进行合并呢? 按"win+r"键在弹出的输入框 ...

  4. Android中CursorLoader的使用、原理及注意事项

    前言 最近在项目中涉及到了即时聊天,因此不可避免地要用到实时刷新的功能,因为在以前的项目中见到别人使用CursorLoader+CursorAdapter+ContentProvider的机制来实现实 ...

  5. 支付宝支付返回通知时 notify_url和return_url的选择

    页面跳转同步通知页面特性(return_url特性) 买家在支付成功后会看到一个支付宝交易提示成功的页面,该页面会停留几秒,然后会自动跳转回商户指定的同步通知页面(参数return_url) 该页面中 ...

  6. 2、在uboot上实现电源管理

    tar xjf u-boot-1.1.6.tar.bz2 cd u-boot-1.1.6 patch -p1 < ../u-boot-1.1.6_jz2440.patch make 100ask ...

  7. CSS垂直居中的实现

    这个问题可以说是老生常谈了,面试时经常问道,一直没整理过,这次做个系统梳理 1.利用display:table实现 从caniuse.com上查到,display:table可以兼容到IE8,以目前环 ...

  8. HDU 1496 Equations hash HDU上排名第一!

    看题传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1496 题目大意: 给定a,b,c,d.a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 ...

  9. hdu 4811 数学 不难

    http://acm.hdu.edu.cn/showproblem.php? pid=4811 由于看到ball[0]>=2 && ball[1]>=2 && ...

  10. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...