早就听过用字典树求异或最大值,然而没做过。发现一碰到异或的题就GG,而且因为以前做过的一道类似的题(事实上并不类似)限制了思路,蠢啊= =。

题意:一棵带权的树,求任意两点间路径异或的最大值。

题解:设xor(a,b)是求a,b间路径的异或值,那么xor(a,b)=xor(root,a)^xor(root,b)。因为如果LCA(a,b)==root时结论显然成立,不然的话就会有重复走过的部分,但是异或有性质x^x=0,所以LCA(a,b)!=root结论依然成立。

这样题目就很简单了。对每一个xor(root,i)(0<i<n)建立trie,因为每个数转成二进制都是一个01组成的字符串,用来建立trie。然后对每一个xor(root,i)在trie查询最大值就好了。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define pk printf("lalala");
#define ppp(x) printf("%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x)) const int KIND = ;
const int MAXN = ;
const int N = ;
int cnt_node; struct node{
node* nt[KIND];
void init(){
memset(nt, , sizeof(nt));
}
} Heap[MAXN];
node *root;
int Xor[N]; inline node* new_node()
{
Heap[cnt_node].init();
return &Heap[cnt_node++];
} void insert(node* root, int *str)
{
for(int i = ; i <= ; ++i){
int ch = str[i];
if(root->nt[ch] == NULL)
root->nt[ch] = new_node();
root = root->nt[ch];
}
} int count(node* root, int *str)
{
int ans = ;
for(int i = ; i <= ; ++i){
int ch = str[i];
int need = (ch ^ );
if(root->nt[need] == NULL) {
root = root->nt[ch];
} else {
root = root->nt[need];
ans += ( << ( - i));
}
}
return ans;
} struct Edge {
int to;
int w;
int next;
} edge[N * ]; int cnt_edge;
int head[N]; void add_edge(int u, int v, int w)
{
edge[cnt_edge].to = v;
edge[cnt_edge].w = w;
edge[cnt_edge].next = head[u];
head[u] = cnt_edge++;
} void dfs(int u, int fa, int val)
{
Xor[u] = val;
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
int w = edge[i].w;
if (v == fa) continue;
dfs(v, u, val^w);
}
} int str[N][]; int main()
{
int n;
while (~scanf("%d",&n)) {
clr(head, -);
cnt_edge = ;
cnt_node = ;
root = new_node();
int u, v, w;
for (int i = ; i < n; ++i) {
scanf("%d%d%d",&u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
dfs(, -, );
//for (int i = 0; i < n; ++i) printf("%d ", Xor[i]); printf("\n");
int ans = ;
for (int i = ; i < n; ++i) {
int idx = ;
for (int b = ; b >= ; --b) {
str[i][idx++] = Xor[i] & ( << b) ? : ;
}
//for (int j = 0; j < idx; ++j) printf("%d ", str[i][j]); printf("\n");
insert(root, str[i]);
}
for (int i = ; i < n; ++i) {
ans = max(ans, count(root, str[i]));
}
printf("%d\n", ans);
}
return ;
}

POJ 3764 (异或+字典树)的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  3. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  4. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

  5. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  6. nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  7. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  8. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  9. POJ 1002 487-3279(字典树/map映射)

    487-3279 Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 309257        Accepted: 5 ...

随机推荐

  1. 李洪强iOS开发之使用 Reachability 检测网络

    1.iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用. 大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作. 2.在你的应用尝试通 ...

  2. 编程之美 3.1 字符串移位包含问 复杂度(O(N*K)

    分享关于编程之美3.1自己编写的代码,很简单. s2.沿着s1匹配(循环匹配,利用%Length技巧),匹配上,返回true. //BOP3.1 char src[] = "AABBCD&q ...

  3. jqueryrotate 使用 帮助 笔记 学习

      1.想变角度 $("imgID").rotate(45);   2.想变角度时,有运动过程 $("imgID").rotate({animateTo:45} ...

  4. MAX-HEAPIFY(2/3n的疑惑)

    Q: In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, The children’s subtrees eac ...

  5. Android 内存管理分析(四)

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...

  6. Java中的protected访问修饰符

    在某个类中定义的protected 方法和属性和默认权限方法和属性是一样的.比如,某类的protected 方法和属性在包外是不能通过该类实例进行访问的(你能在包外访问一个类的默认权限的方法和属性吗? ...

  7. 大四实习准备5_android广播机制

    2015-5-1 android 广播机制 5.1简介 分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先 ...

  8. poj2月题解

    竟然生日前一天poj破百,不错不错,加速前进! poj2437 由于泥泞不重叠,所以按其实左边排个序再统计一遍即可(如果不是刚好盖满就尽量往后盖) poj2435 细节bfs poj2230 求欧拉回 ...

  9. UVa 658 (Dijkstra) It's not a Bug, it's a Feature!

    题意: 有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间. 求修复所有BUG的最短时间. 分析: 可以用n个二进制位表示这 ...

  10. 三个流行MySQL分支的对比

    MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这么重要 ...