对于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. css制作导航栏的上下三角

    1)先完成一个导航条 <style type="text/css"> .nav-ul{ list-style: none; } .nav-ul li{ width: 1 ...

  2. php字符串函数(1)

    下面去学习一下php的字符串函数,那么怎么去看手册呢,举个例子 int strcasecmp ( string $str1 , string $str2 ) 第一个int,表示此函数返回的类型是int ...

  3. Windows I/O模型、同步/异步、阻塞/非阻塞

    转载自:http://www.cppblog.com/tx7do/articles/5954.html 同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回.按照这个定义,其实 ...

  4. Java 非对称加密

    package test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.Object ...

  5. JAVA 函数式接口与c#委托对应关系(一)

    C# Action委托 VS JAVA Action 接口函数 1.c#:Action 封装一个方法,该方法不具有参数并且不返回值. 构造实体类类 using System; namespace Ac ...

  6. 如何获得Windows 8中已记住的WIFI的明文密码

    网上很流行的一种查看WIFI密码明文的方法,如下: 今天遇到了一种状况,就是如果不连WIFI的情况我能抓到这个密码吗?(实在不想开口问同事密码多少,只能苦逼的自己想办法了o(︶︿︶)o ) 答案当然是 ...

  7. ios专题 - 多线程非GCD(1)

    iOS多线程初体验是本文要介绍的内容,iPhone中的线程应用并不是无节制的,官方给出的资料显示iPhone OS下的主线程的堆栈大小是1M,第二个线程开始都是512KB.并且该值不能通过编译器开关或 ...

  8. ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)

    题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...

  9. Java实现单向链表

    /* 先定义一个Node类用来存储节点的值域和指针域 * 即当前节点中的值和后面节点的方法 * 在C中就是相当与定义一个结构体类型一个数据域和指针域的方法 */class LNode{//这个写法已经 ...

  10. execute、executeUpdate、executeQuery三者的区别及返回值

    一.boolean execute(String sql)允许执行查询语句.更新语句.DDL语句.返回值为true时,表示执行的是查询语句,可以通过getResultSet方法获取结果:返回值为fal ...