题目链接:http://poj.org/problem?id=3764

题目大意是在树上求一条路径,使得xor和最大。

由于是在树上,所以两个结点之间应有唯一路径。

而xor(u, v) = xor(0, u)^xor(0, v)。

所以如果预处理出0结点到所有结点的xor路径和,问题就转换成了求n个数中取出两个数,使得xor最大。

这个之前用字典树处理过类似问题。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ; //求n个数中取出两个数能xor的最大值
//Trie Tree字典树
//len*n, len为数的二进制最大长度
const int len = ;//len表示数的二进制最大长度
struct Trie
{
int next[];
}tree[maxN*len];
int cntTree; void initTree()
{
cntTree = ;
memset(tree, -, sizeof(tree));
} void add(int x)
{
int now = ;
bool k;
for (int i = len; i >= ; i--)
{
k = x&(<<i);
if (tree[now].next[k] == -)
tree[now].next[k] = ++cntTree;
now = tree[now].next[k];
}
} //返回当前数中能和x合成最大数的数
int query(int x)
{
int v = , now = ;
bool k;
for (int i = len; i >= ; i--)
{
k = x&(<<i);
if (tree[now].next[!k] != -)
k = !k;
v = v|(k<<i);
now = tree[now].next[k];
}
return v;
} //链式前向星
struct Edge
{
int to, next;
int val;
}edge[maxN*]; int head[maxN], cnt; void addEdge(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].val = w;
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} int n, p[maxN]; void dfs(int now)
{
int to;
for (int i = head[now]; i != -; i = edge[i].next)
{
to = edge[i].to;
if (p[to] == -)
{
p[to] = p[now]^edge[i].val;
dfs(to);
}
}
} void input()
{
initEdge();
memset(p, -, sizeof(p));
int u, v, w;
for (int i = ; i < n; ++i)
{
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
p[] = ;
dfs();
} void work()
{
int ans = ;
initTree();
for (int i = ; i < n; ++i)
{
add(p[i]);
ans = max(ans, p[i]^query(p[i]));
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
input();
work();
}
return ;
}

ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)的更多相关文章

  1. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  2. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  3. ACM学习历程——POJ 1700 Crossing River(贪心)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  4. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

    Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...

  5. ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)

    http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...

  6. ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  8. ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...

  9. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

随机推荐

  1. STM32F103RCT6移植到STM32F103C8T6注意事项

    1,修改IC为STC32F103C8 2,修改晶振为8.0M 3,修改C/C++宏定义,由STM32F10X_HD,USE_STDPERIPH_DRIVER 改为 STM32F10X_MD,USE_S ...

  2. EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明

    1.相关资料入口 腾讯云LVB EasyNVR.com 2.加速说明 2.1. 腾讯LVB加速 2.1.1. 开通服务 腾讯云视频LVB开通入口 2.1.2. 登录进入控制台 腾讯云直播控制台 2.1 ...

  3. 三款查看class文件结构的工具

    classpy JavaClassViewer jclasslib

  4. Bootstrap 轮播图(Carousel)插件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 关于js中undefined的判断

    在开发中遇到一个情景,当添加用户的时候不需要传入用户id,如果是修改那么需要传入id,因为用的是angular框架,参数是早就定义好了的,那么在新增用户的时候就会出现undefined的情况,之前我一 ...

  6. python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数

    import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...

  7. php匹配字符串中大写字母的位置

    变量名用的是驼峰,数据库中字段中的是下划线,现在想把userId等变量批量转换成user_id,怎么样获取大写字母在字符串中的位置?echo strtolower(preg_replace('/((? ...

  8. Docker DNS

    从 Docker 1.10 版本开始,docker daemon 实现了一个内嵌的 DNS server,使容器可以直接通过“容器名”通信. 方法很简单,只要在启动时用 --name 为容器命名并且连 ...

  9. C、C++、Java、JavaScript、PHP、Python、Ruby 这些语言分别主要用来开发什么?

    C.C++.Java.JavaScript.PHP.Python.Ruby 这些语言分别主要用来开发什么? pansz,欢迎评论 此贴纯科普用,以下仅仅说主要用途,其他用途限于篇幅关系省略之,不要钻牛 ...

  10. stdcall、cdecl详解(以及WINAPI和CALLBACK之类的宏对应什么)

    转自:http://blog.csdn.net/huanjieshuijing/article/details/5822942 对_stdcall 的理解在C语言中,假设我们有这样的一个函数:int ...