BZOJ 1954 (POJ 3764) Trie的经典应用 求树上最大异或值
来源:互联网 发布:怎么开企业淘宝店铺 编辑:程序博客网 时间:2024/06/05 20:34
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1954 (然而这是权限题…… 所以还是看POJ 3764吧)
感谢黄学长。。涨姿势了。。
树上路径问题,xor运算满足分配率交换律,先求出每个节点到根节点的路径权值,这样就转化为从一堆值中选出两个xor起来最大的。
于是把这一堆值(不足30位的补到30位)一一插入一颗Trie中,然后一一查询每个值。每次查询,尽量走与当前查询值在该位不同的边,以获得xor值的最大。
最终的答案即为所有查询结果的最大值。
// BZOJ 1954 ( POJ 3764 )#include <cstdio>#include <algorithm>#include <cstring>using namespace std; const int N=100000+5, M=N; struct Edge { int from, to, w, pre; }e[M*2]; int k, last[N], ch[N*30][2], sz, n, u, v, w, ans, d[N]; void ine(int x, int y, int w) { k++; e[k].from=x, e[k].to=y, e[k].pre=last[x], e[k].w=w; last[x]=k; } #define reg(i,x) for (int i=last[x]; i; i=e[i].pre) #define rep(i,a,b) for (int i=a; i<=b; i++) #define dep(i,a,b) for (int i=a; i>=b; i--) #define read(x) scanf("%d", &x) #define fill(a,x) memset(a, x, sizeof(a)) void change(int x, int fa, int dis) { d[x]=dis; reg(i,x) { int y=e[i].to; if (y==fa) continue; change(y, x, dis^e[i].w); } } void init() { k=0; fill(last, 0); fill(d, 0); sz=1; fill(ch, 0); } void charen(int x, int *s) { rep(i,0,30) s[i]=0; int i=0; while (x>0) s[i++]=(x%2), x/=2; } int s[35]; void insert(int x) { int u=1; charen(x, s); dep(i,30,0) { if (!ch[u][s[i]]) { sz++; ch[sz][0]=ch[sz][1]=0; ch[u][s[i]]=sz; } u=ch[u][s[i]]; } } void search(int x) { charen(x, s); int u=1, t=0; dep(i,30,0) if (ch[u][s[i]^1]) { t+=(1<<i); u=ch[u][s[i]^1]; } else u=ch[u][s[i]]; ans=max(ans, t); }int main(){ while (scanf("%d", &n)==1 && n) {init();rep(i,1,n-1) read(u), read(v), read(w), u++, v++, ine(u, v, w), ine(v, u, w);change(1, 0, 0); ans=0; rep(i,1,n) insert(d[i]); rep(i,1,n) search(d[i]); printf("%d\n", ans); } return 0;}
0 0
- BZOJ 1954 (POJ 3764) Trie的经典应用 求树上最大异或值
- 字典树(Trie、prefix tree)及其应用(求一个数组中的最大异或值)
- 01Trie求最大异或
- 【Trie树】POJ 3764 最大连续异或和
- poj 2631 (求树上的最大距离 基础题)
- BZOJ 3261 最大异或和 可持久化Trie
- BZOJ 3261 最大异或和 可持久化Trie
- [BZOJ]3261: 最大异或和 可持久化Trie
- Trie树上dfs跑 与 或 异或
- bzoj 3261 最大异或和 可持久化trie树
- BZOJ 3261 最大异或和 可持久化Trie树
- 【BZOJ】【P3261】【最大异或和】【题解】【可持久化Trie】
- BZOJ 3261 浅谈可持久化TRIE树最大连续异或和
- BZOJ 3261 最大异或和 && qwb VS 去污棒(可持久化01Trie)
- POJ 1816 Wild Words(trie 树上的DFS)
- 【BZOJ 3689】异或之 trie+堆
- BZOJ 4260 REBXOR 异或Trie+dp
- codeforces367(字符串hash求lcp比较大小,Trie解最大异或问题)
- 数组和字符串
- Implement Trie Tree by hashmap
- 黑马程序员——OC之NSArray、NSDictionary
- Matlab 2012b--64bit安装教程
- java解惑--谜题5:十六进制的趣事
- BZOJ 1954 (POJ 3764) Trie的经典应用 求树上最大异或值
- 使用 DownloadManager 实现完全在后台下载
- CSU 1550-Simple String(字符串)
- A+B=X
- ThinkPHP中initialize和construct的不同
- style/Theme.NoTitleBar崩溃
- sql语句
- Codeforces Round #335 (Div. 2) A. Magic Spheres 水题
- Codeforces Round #335 (Div. 2) 605A Sorting Railway Cars