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 ...
随机推荐
- Android Studio 中 Gradle 依赖的统一管理(rootProjectt)
最近遇到Android Studio 中 Gradle 依赖的统一管理的不懂得地方,看大神的也没看懂,百度了一下,使用起来还挺方便 下面是链接,在这里我就不详细说明了, http://www.jian ...
- 九度OJ 1199:找位置 (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2083 解决:1010 题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1:a,4 ...
- 开发者眼中的Spring与JavaEE
JavaEE与Spring 在Java社区中,Spring与Java EE之争是个永恒的话题.在这场争论中,来自两个阵营的布道师.架构师 与铁杆粉丝都在不遗余力地捍卫着本方的尊严,并试图说服对方加入到 ...
- 洛谷 1641 [SCOI2010]生成字符串
题目戳这里 一句话题意 求\(C_{m+n}^{m}\)-\(C_{m+n}^{m-1}\) Solution 巨说这个题目很水 标签居然还有字符串? 但是我还不很会用逆元真的太菜了,还好此题模数P为 ...
- Linux备份和回复mysql数据库
备份:mysqldump -u root -p密码 数据库名>/home/data.bak mysqldump -u root -p密码 数据库名.表名>/home/data.bak ...
- go语言之并发编程 channel
前面介绍了goroutine的用法,如果有多个goroutine的话相互之间是如何传递数据和通信的呢.在C语言或者JAVA中,传输的方法包括共享内存,管道,信号.而在Go语言中,有了更方便的方法,就是 ...
- Linux c编程:I/O多路复用之select
一般我们在写socet程序的时候调用的accept,recv等操作都是阻塞型的.意思就是如果我们一直收不到数据那么则会被阻塞.所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待 ...
- 搜索ABAP程序代码中的字符串
标准程序名:RPR_ABAP_SOURCE_SCAN /BEV1/NERM07DOCS
- ARDUINO使用GPRS发送GPS数据到OneNet测试
功能: 测试把固定的GPS数据发送到OneNet平台 调试途中碰到的问题 ARDUINO不支持sprintf的double打印,只能转换为char字符串然后再%s打印 #include <Tim ...
- 帝国cms数据表中文说明
本文介绍下,帝国cms中各数据表的用途,有需要的朋友,参考下吧. 帝国cms各数据表及用途说明. phome_ecms_infoclass_news 新闻采集规则记录表 phome_ecms_info ...