ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接: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 && 字典树 && 贪心)的更多相关文章
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- 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 ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
- ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)
http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...
- 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 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
随机推荐
- XML使用总结(一)
XML使用总结(一): XML是一种可拓展的标记语言,被设计用来描写叙述.存储及传递数据的语言体,而它的标签没有被提前定义,须要用户自行定义,是W3C推荐的数据存储和传递的标准标记语言. · ...
- task19-21
[说明]理想是丰满的,现实很骨感,昨天还说今天有望干掉5个小任务,看来是没可能了,兜兜转转地做了一天也才完成下面的这些 一:今日完成 19.学习Spring,配置Spring和Junit 1)先安装一 ...
- 【BZOJ4128】Matrix BSGS+hash
[BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...
- 九度OJ 1349:数字在排序数组中出现的次数 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2489 解决:742 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数组的大小. ...
- Python菜鸟之路:Python基础
一.Python版本升级至3.0的必然性 In November 2014, it was announced that Python 2.7 would be supported until 202 ...
- centos7 使用postgres
1: http://www.cnblogs.com/think8848/p/5877076.html 2:http://blog.csdn.net/xuaa/article/details/52262 ...
- 使用基本 SQL 命令
概述 在本教程中,将学习结构化查询语言 (SQL),包括: 使用基本 SQL 命令 执行基本数据操做 数据库和 SQL 在本系列教程中,目前我们使用平面文本文件来存储数据.平面文本文件可能适合相对较少 ...
- ip地址设备信息
其实是个小问题,在项目中遇到要获取手机ip地址和mac地址的问题,mac地址获取用appcan的uexDevice插件可以实现. 但是ip地址,获取的方式很多,具体有搜狐/腾讯/新浪等提供的接口.但是 ...
- C#使用SQL语句时候的万用密码问题
实际上万用密码就是因为SQL里面的语句--是注释,利用bug添加用户名和密码. 例如,用户名为 adada’ or 1=1-- 第一个种写法登录成功了 第二种写法登录失败(正确) 第三种写法登录失败( ...
- elk示例-精简版2
作者:Danbo 时间:2016-03-13 1.保存进Elasticsearch Logstash可以试用不同的协议实现完成将数据写入Elasticsearch的工作,本节中介绍HTTP方式. 配置 ...