题目链接: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. stm32DMA通道 ADC通道

    DMA: 1.使用DAC的时候.将转化后得到的模拟信号通过IO口输出的时候.为什么还将IO口配置能输入模式 PS:stm32手冊上定义PA4和PA5分别和DAC1通道和DAC2通道相连  : DMA1 ...

  2. 线性同余方程模板( A+C*x=B(mod D) )

    void extendgcd(long long a,long long b,long long &d,long long &x,long long &y) { ){d=a;x ...

  3. vue-cli (vue脚手架)

    vue-cli(脚手架):它可以自动生成目录 1.在网速不佳的情况下可以安装cnpm(淘宝镜像)如果网速快可以不用安装cnpm直接进行下一步操作 第一步:在命令行执行(全局安装cnpm) npm in ...

  4. iOS 添加震动效果

    开发过程中,有时候会碰到点击按钮或者某个动画会配合震动效果:下面介绍iOS开发过程中的震动添加: 导入:#import <AudioToolbox/AudioToolbox.h> 在需要出 ...

  5. SpringMVC请求流程

    Spring结构图 SpringMVC请求流程图 SpringMVC请求流程图语述: request--->DispatcherServler(中央调度器/前端控制器)---> Handl ...

  6. VLFeat图像库在VS2012下的配置

         近期做课题所需,開始使用VLFeat图像库.      库下载链接:      http://download.csdn.net/detail/sunboyiris/7500097     ...

  7. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

  8. ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据到 OneNet 服务器

    简述 原来写了一个C++的wifi库但是发现用c++ arduino这小身板有点扛不住,代码比较大,使用String类型数据处理速度慢,而且很容易无缘无故跑飞.而且封装成库后使用还需要修改arduin ...

  9. 【HTTP】初识代理

    Web代理(proxy)位于客户端和服务器端之间.HTTP的代理服务器既是Web服务器端又是Web客户端. 1. 代理和网关的对比 代理连接的是两个或者多个使用相同协议的应用程序. 网关连接的是两个或 ...

  10. Linux:进程管理

    Linux:进程管理 进程间通信 文件和记录锁定. 为避免两个进程间同时要求访问同一共享资源而引起访问和操作的混乱,在进程对共享资源进行访问前必须对其进行锁定,该进程访问完后再释放.这是UNIX为共享 ...