POJ 3764[The xor-longest Path]题解
来源:互联网 发布:linux如何安装xampp 编辑:程序博客网 时间:2024/05/16 19:34
(传送门)
题目大意
给出一棵树,求出权值最大的的简单路径,定义一条简单路径的权值为这条简单路径上的边权的xor和。
解题分析
正在学习Trie树的神奇用处,Trie树又称字母树,用于存储字符串,代码简单,用起来也方便,Trie树也是学习AC自动机等其他字符串算法的基础,但是这里讲的,是Trie树与二进制xor运算的关系,多用于处理xor数最大最小问题。
首先根据xor原理,相同为0,不同为1。那么对于两个二进制数的同一位,如果要大一点,那么就需要让两个二进制数的这一位不相同,反正让他们这一位相同就行,同时很明显,高位对xor的影响比低位大。那么套用Trie树,首先先把数字看做二进制数对待,然后二进制数当字符串对待(注意,因为高位的影响比低位大,所以要对待的二进制数要补足至31位,高位用0来填),然后字符串就插入Trie树。就好了。
当查询的时候(这里以最大值为例),从根节点开始,贪心的思路。如果查询的数这一位取反后有路可走,那么就走这条路(因为这是当前的最高位,影响很大,即使后面全部111111,也抵不过最开始一个1然后一堆0),如果不能,就只能走另外一条路了(不过没有路是不可能的,原因自己想,很简单的)。
然后转向这道题,首先求xor和最大路径,那么根据xor的运算规律,可以先求出每个点到根节点的距离a[i],然后两个点i,j的xor和就是a[i] xor a[j]。从他们公共祖先的路径已经算了两边又成了0,问题就成了a[]数组中找两个数求最大xor值,然后就是上面模型。解决
复杂度:
时间:O(n*30);
空间:O(n*60);
代码
#include<cstdio>#include<cstring>#define maxn 200005#define maxe 400005using namespace std;int n,len,tot,ans,son[maxe],nxt[maxe],w[maxe],lnk[maxn],a[maxn],que[maxn],trie[6000005][2];bool vs[maxn];void trie_init(int x){trie[x][0]=trie[x][1]=0;}void trie_Insert(int x){ int now=0; for (int i=30;i>=0;i--){ int t=(x&(1<<i))>>i; if (!trie[now][t]) {trie[now][t]=++len; trie_init(len);} now=trie[now][t]; }}int trie_find(int x){ int now=0,tem=0; for (int i=30;i>=0;i--){ int t=(x&(1<<i))>>i; if (trie[now][t^1]){ tem+=(1<<i); now=trie[now][t^1]; }else now=trie[now][t]; } return tem;}inline void readi(int &x){ x=0; char ch=getchar(); while ('0'>ch||ch>'9') ch=getchar(); while ('0'<=ch&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}}void _add(int x,int y,int z){ tot++; son[tot]=y; w[tot]=z; nxt[tot]=lnk[x]; lnk[x]=tot;}void _bfs(){ int hed=0,til=1; memset(vs,0,sizeof(vs)); memset(que,0,sizeof(que)); que[1]=1; vs[1]=true; a[1]=0; while (hed!=til){ hed++; for (int j=lnk[que[hed]];j;j=nxt[j]) if (!vs[son[j]]){ que[++til]=son[j]; vs[son[j]]=true; a[son[j]]=a[que[hed]]^w[j]; } }}void _work(){ len=tot=0; memset(lnk,0,sizeof(lnk)); memset(nxt,0,sizeof(nxt)); for (int i=1;i<n;i++){ int x,y,z; readi(x); readi(y); readi(z); x++; y++; _add(x,y,z); _add(y,x,z); } _bfs(); trie_init(0); int ans=0,tem; for (int i=1;i<=n;i++){ tem=trie_find(a[i]); if (ans<tem) ans=tem; trie_Insert(a[i]); } printf("%d\n",ans);}int main(){ freopen("xorp.in","r",stdin); freopen("xorp.out","w",stdout); while (scanf("%d",&n)==1) _work(); return 0;}
PS:BZOJ上也有这道题,编号1954,不过有权限,这里就没有传送门了。
PPS:Orz Matchperson
- POJ 3764[The xor-longest Path]题解
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- poj 3764 The xor-longest Path
- poj-3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- POJ 3764 The xor-longest Path
- The xor-longest Path ( POJ - 3764)
- POJ 3764 The xor-longest Path Trie
- The xor-longest Path POJ
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
- 【POJ 3764】The xor-longest Path 中文题意&题解&代码(C++)
- poj 3764 The xor-longest Path(字典树)
- poj - 3764 - The xor-longest Path(Trie)
- POJ 3764 The xor-longest Path【字典树】
- 关于前面二维码生成类字数限制的修改
- Java 关于继承中的static代码块,普通代码块及构造方法的执行顺序
- JStorm与Storm源码分析(三)--Scheduler,调度器
- python字符串截取操作
- 公司清算案件的诉讼费收取问题
- POJ 3764[The xor-longest Path]题解
- raid 不同级别详解
- Makefile详解(三)
- Hibernate中的代理对象和延迟加载的问题
- QTP11.5破解延长试用
- 随笔
- android Activity 四种启动模式剖析
- 正则表达式
- AngularJS 前端分页