由于权值是在边上,所以很容易发现一个性质:d(x,y)=d(x,root) xor d(y,root)。

  因为有了这个性质,那么就很好做了。对于每一个点统计到root的距离,记为f 数组。

  将f数组里的每个值插进按照二进制位插进字典树里面。

  枚举每一个点,然后在字典树中搜索最大的xor值就可以了。

Program CODEVS1187;
const maxn=;
type arr=record
u,v,w,next:int64;
end;
type arr1=record
next:array[..] of longint;
end;
var eg:array[..maxn*] of arr;
last:array[..maxn] of longint;
fa:array[ ..maxn] of longint;
f:array[..maxn] of int64;
T:array[..maxn*] of arr1;
a:array[..] of longint;
n,u,v,w,root,mx,num,m,k,sum,ans,now:int64;
i,j:longint;
procedure add(u,v,w:longint);
begin
inc(j);
eg[j].u:=u;
eg[j].v:=v;
eg[j].w:=w;
eg[j].next:=last[u];
last[u]:=j;
end;
procedure dfs(u:longint;sum:int64;fa:longint);
var i:longint;
begin
f[u]:=sum;
i:=last[u];
while i<> do
begin
if eg[i].v<>fa then
dfs(eg[i].v,sum xor eg[i].w,u);
i:=eg[i].next;
end;
end;
begin
readln(n);
for i:= to n- do
begin
readln(u,v,w);
add(u,v,w);
add(v,u,w);
end;
root:=;
dfs(root,,);
{---------------------------------------------------}
mx:=;
for i:= to n do if f[i]>mx then mx:=f[i];
j:=mx; num:=;
while j> do
begin
inc(num);
j:=j div ;
end;
m:=num; k:=;
for i:= to n do
begin
fillchar(a,sizeof(a),);
j:=f[i]; num:=;
while j> do
begin
inc(num);
a[num]:=j mod ;
j:=j div ;
end;
now:=;
for j:=m downto do
if T[now].next[a[j]]<> then now:=T[now].next[a[j]] else
begin
inc(k);
T[now].next[a[j]]:=k;
now:=k;
end;
end;
ans:=;
for i:= to n do
begin
fillchar(a,sizeof(a),);
j:=f[i]; num:=;
while j> do
begin
inc(num);
a[num]:=j mod ;
j:=j div ;
end;
now:=; sum:=;
for j:=m downto do
if T[now].next[-a[j]]<> then
begin
sum:=sum*+-a[j];
now:=T[now].next[-a[j]];
end
else
begin
sum:=sum*+a[j];
now:=T[now].next[a[j]];
end;
if sum xor f[i]>ans then ans:=sum xor f[i];
end;
writeln(ans);
end.

最新文章

  1. python leetcode 1
  2. 浅析word-break work-wrap区别
  3. js跳转到新页面传参以及接收参数的方法
  4. sqlzoo.net刷题2
  5. [asp.net] 通过JS实现对treeview控件的复选框单选控制。
  6. systemd详解
  7. MySQL 5.7.9多源复制报错修复
  8. QQ空间自动发广告解决方法
  9. android下tcpdump抓包
  10. STL之stack(栈)
  11. linux: 鸟哥的私房菜
  12. DOM中document对象的常用属性方法
  13. MVC(一)-MVC的基础认知
  14. Caused by: java.net.SocketException: Broken pipe
  15. python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
  16. 专题:CF图论杂题
  17. CentOS下使用VirtualBox 安装 Windows虚拟机的简单方法
  18. git 查看远程分支最后一次提交时间
  19. mssql拿webshell的方法
  20. go语言之进阶篇指针类型和普通类型的方法集

热门文章

  1. A Few Words on Callbacks and Asynchronous Mechanism In Javascript
  2. iis 服务器而配置php运行环境
  3. centos源更新
  4. 修路方案 Kruskal 之 次小生成树
  5. 学习c语言的感想
  6. V-SQL的简单使用
  7. JQuery中常用的$.get(),$.post(),$.ajax(),$.getJSON(),load()的详解与区别
  8. [转]【C/C++】Linux下使用system()函数一定要谨慎
  9. CUDA-GPU编程
  10. JS——高级各行换色