对于xor有一个非常重要的性质
A xor B xor B=A 并且满足交换律和结合律
这道题是求无根树上最长的xor路径
我们知道,无根树的题目我们都是要想办法转化为有根树来处理
当我们确定了一个根,根到每个节点i的xor路径f[i]可知
则在树中,任意两个节点ij间的xor路径长度即为f[i] xor f[j]
为什么,利用之前的性质我们可以知道
路径长度=d[i,LCA] xor d[LCA,j]=d[i,LCA] xor d[LCA,root] xor d[root,LCA] xor d[LCA,j]=f[i] xor f[j]
这样就转化为一个经典的问题,在一堆数中找两个数是xor值最大
这个我们可以将所有值得二进制建成一棵trie,
然后穷举每个数,遍历trie树找到和这个数xor值最大的数(贪心)
PS:这道题和poj3764一样,只不过bzoj1954标号是1~n,poj是0~n-1
但我改了标号始终在poj上WA……求指教

code(按bzoj1954)

 type node=record
point,next,cost:longint;
end; var son:array[..,..] of longint;
p,f:array[..] of longint;
v:array[..] of boolean;
edge:array[..] of node;
ans,m,n,len,t,r,i,x,y,z:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y,z:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].cost:=z;
edge[len].next:=p[x];
p[x]:=len;
end; procedure build(x:longint);
var p,i,y:longint;
begin
p:=;
for i:= downto do
begin
y:=( shl i) and x;
if y<> then y:=;
if son[p,y]=- then
begin
inc(t);
son[p,y]:=t;
end;
p:=son[p,y];
end;
end; procedure dfs(x:longint);
var i,y:longint;
begin
i:=p[x];
v[x]:=true;
while i<>- do
begin
y:=edge[i].point;
if not v[y] then
begin
f[y]:=f[x] xor edge[i].cost;
dfs(y);
end;
i:=edge[i].next;
end;
end; function getans(x:longint):longint;
var y,p,s,i:longint;
begin
p:=;
s:=;
for i:= downto do
begin
y:=( shl i) and x;
if y<> then y:=;
if son[p,-y]<>- then
begin
s:=s+ shl i;
p:=son[p,-y];
end
else p:=son[p,y];
end;
exit(s);
end; begin
while not eof do
begin
readln(n);
fillchar(p,sizeof(p),);
len:=;
for i:= to n- do
begin
readln(x,y,z);
// inc(x);
// inc(y);
add(x,y,z);
add(y,x,z);
end;
fillchar(v,sizeof(v),false);
fillchar(son,sizeof(son),);
f[]:=;
t:=;
dfs();
for i:= to n do
build(f[i]); ans:=;
for i:= to n do
ans:=max(ans,getans(f[i]));
writeln(ans);
end;
end.

bzoj1954 poj3764的更多相关文章

  1. POJ3764,BZOJ1954 The xor-longest Path

    题意 In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of ...

  2. 【poj3764】 The xor-longest Path

    http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...

  3. POJ3764

    题目 POJ3764 The xor-longest Path 原题传送门 主要思路: 1.求出每个点到根节点(这里是树,所以直接取0)路径上所有权值xor和为d[i],则任意两点间路径xor和则为 ...

  4. POJ3764 The xor-longest Path

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6361   Accepted: 1378 Description In ...

  5. POJ3764 The xor-longest Path(Trie树)

    题目给一棵有边权的树,问树上任意两点路径上的边异或值最多是多少. 记录每个点u到根路径的异或值xor[u],那么任意两点u.v路径的异或值就是xor[u]^xor[v]. 于是这个问题就变成了从n个数 ...

  6. POJ3764 The xor-longest path Trie树

    代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...

  7. BZOJ1954: Pku3764 The xor-longest Path

    题解: 在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和. 所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可. 代码: #include<cstd ...

  8. poj3764(dfs+Trie树+贪心)

    题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...

  9. [POJ3764]最长异或路径

    Description: 给定一棵n个点的带权树,结点下标从1开始到N.寻找树中找两个结点,求最长的异或路径. Hint: \(n<=10^5\) Solution: 真是01Trie傻逼题,居 ...

随机推荐

  1. Spring Mvc session拦截器实现

    Spring Mvc拦截器实现session过期跳转到登录页面 配置拦截器 <mvc:interceptors> <mvc:interceptor> <mvc:mappi ...

  2. Ajax无刷新提交表单和显示

    ajax无刷新表单提交:   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  3. [XML] C#XMLProcess操作Xml文档的帮助类 (转载)

    点击下载 XMLProcess.rar 主要功能如下所示 看下面代码吧 /// <summary> /// 类说明:XMLProcess /// 编 码 人:苏飞 /// 联系方式:361 ...

  4. Http协议、线程、线程池

    Socket模拟服务端运行代码: 1:启动服务端监听的服务,并接受客户端的连接 1.1 创建Socket Socket listenSocket=new Socket(AddressFamily.In ...

  5. .NET设计模式(9):桥接模式(Bridge Pattern)

      .NET设计模式(9):桥接模式(Bridge Pattern)   桥接模式(Bridge Pattern) --.NET设计模式系列之九 年月 实现代码如下:..所谓抽象和实现沿着各自维度的变 ...

  6. linux ssh rsa免输入密码

    A为本地主机(即用于控制其他主机的机器) ; B为远程主机(即被控制的机器Server), 假如ip为172.24.253.2 ;     在A上的命令: ssh-keygen -t rsa (连续三 ...

  7. iOS截屏代码

    转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...

  8. a+b(高精度)[陈涛]——c语言

    #include<stdio.h>#include<string.h>char s[1000];---------------------------------------- ...

  9. boost::xml————又一次失败的尝试

    尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码) 请看源码: DealXml.h #pragma once #include &l ...

  10. thinkphp框架使用心得

    接触的第一个PHP框架就是TP,在使用的了一段时间后就放弃了,说实话TP的弊端挺多,之后又接触laravel框架,慢慢的就爱上laravel这个框架了.这段时间由于公司的原因,又不得不使用thinkp ...