CODEVS1187 Xor最大路径 (Trie树)
2024-09-30 05:22:23
由于权值是在边上,所以很容易发现一个性质: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.
最新文章
- python leetcode 1
- 浅析word-break work-wrap区别
- js跳转到新页面传参以及接收参数的方法
- sqlzoo.net刷题2
- [asp.net] 通过JS实现对treeview控件的复选框单选控制。
- systemd详解
- MySQL 5.7.9多源复制报错修复
- QQ空间自动发广告解决方法
- android下tcpdump抓包
- STL之stack(栈)
- linux: 鸟哥的私房菜
- DOM中document对象的常用属性方法
- MVC(一)-MVC的基础认知
- Caused by: java.net.SocketException: Broken pipe
- python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
- 专题:CF图论杂题
- CentOS下使用VirtualBox 安装 Windows虚拟机的简单方法
- git 查看远程分支最后一次提交时间
- mssql拿webshell的方法
- go语言之进阶篇指针类型和普通类型的方法集
热门文章
- A Few Words on Callbacks and Asynchronous Mechanism In Javascript
- iis 服务器而配置php运行环境
- centos源更新
- 修路方案 Kruskal 之 次小生成树
- 学习c语言的感想
- V-SQL的简单使用
- JQuery中常用的$.get(),$.post(),$.ajax(),$.getJSON(),load()的详解与区别
- [转]【C/C++】Linux下使用system()函数一定要谨慎
- CUDA-GPU编程
- JS——高级各行换色