做该题之前,至少要先会做这道题


记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得。

\(~\)

考虑 \(u\) 到 \(v\) 简单路径的异或和该怎么求?

令 \(z=\operatorname{lca}(u,v)\) ,则 \(u\) 到 \(v\) 简单路径的异或和可以分成两段求解:一段是 \(z\) 到 \(u\) 简单路径的异或和,一段是 \(z\) 到 \(v\) 简单路径的异或和,二者异或一下即为 \(u\) 到 \(v\) 简单路径的异或和。

由于异或 "\(a \operatorname{xor} a=0\)" 的性质,两条路径重叠的部分异或起来即为 \(0\),可得

​ \(z\) 到 \(v\) 简单路径的异或和为

\[d[u] \operatorname{xor} d[z]
\]

​ \(z\) 到 \(v\) 简单路径的异或和为

\[d[v] \operatorname{xor} d[z]
\]

进一步,可得

​ \(u\) 到 \(v\) 简单路径的异或和为

\[(d[u]\operatorname{xor}d[z])\operatorname{xor}(d[v]\operatorname{xor}d[z])
\]

​ 由于异或满足交换律,可化简为

\[d[u]\operatorname{xor}d[v]
\]

由上述性质,答案即为 \(\max\limits_{1\leq i<j\leq n}\)\(\{d[i] \operatorname{xor} d[j]\}\),又回到了这道题,字典树直接解决即可。

时间复杂度 \(\theta(32n)\) 。


CODE

#include<cstdio>
#include<algorithm>
#include<queue> #define RI register int using namespace std; inline int read()
{
int x=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-f;s=getchar();}
while(s>='0'&&s<='9'){x=x*10-'0'+s;s=getchar();}
return x*f;
} const int N=100100,M=200100; int n;
int tot_E,head[N],ver[M],edge[M],Next[M]; void add(int u,int v,int w)
{
ver[++tot_E]=v; edge[tot_E]=w; Next[tot_E]=head[u]; head[u]=tot_E;
} int d[N];
int vis[N]; void bfs()
{
queue<int>q;
q.push(1);vis[1]=1;
while(q.size())
{
int u=q.front();q.pop();
for(RI i=head[u];i;i=Next[i])
{
int v=ver[i],w=edge[i];
if(vis[v])continue;
vis[v]=1;
d[v]=d[u]^w;
q.push(v);
}
}
} int trie[N*32+10][2],tot=1;
int ans; void insert(int num)
{
int p=1;
for(RI k=31;k>=0;k--)
{
int ch=num>>k&1;
if(trie[p][ch]==0)trie[p][ch]=++tot;
p=trie[p][ch];
}
} int search(int num)
{
int p=1,sum=0;
for(RI k=31;k>=0;k--)
{
int ch=num>>k&1;
if(trie[p][ch^1])p=trie[p][ch^1],sum+=1<<k;
else p=trie[p][ch];
if(p==0)return sum;
}
return sum;
} int main()
{
scanf("%d",&n);
for(RI i=1;i<n;i++)
{
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
} bfs(); for(RI i=1;i<=n;i++)
{
ans=max(ans,search(d[i]));
insert(d[i]);
} printf("%d\n",ans); return 0;
}

thanks for watching

题解 bzoj1954【Pku3764 The xor – longest Path】的更多相关文章

  1. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  2. Solve Longest Path Problem in linear time

    We know that the longest path problem for general case belongs to the NP-hard category, so there is ...

  3. Why longest path problem doesn't have optimal substructure?

    We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...

  4. BZOJ1954: Pku3764 The xor-longest Path

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

  5. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

  6. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

  7. [LeetCode]题解(python):064-Minimum Path Sum

    题目来源 https://leetcode.com/problems/minimum-path-sum/ Given a m x n grid filled with non-negative num ...

  8. [LeetCode]题解(python):063-Unique path II

    题目来源 https://leetcode.com/problems/unique-paths-ii/ Follow up for "Unique Paths": Now cons ...

  9. [LeetCode]题解(python):071-Simplify Path

    题目来源: https://leetcode.com/problems/simplify-path/ 题意分析: 简化Unix上的绝对路径,也就是多个'/'代表一个,'..'表示返回上一级目录,‘.' ...

随机推荐

  1. 用ES5实现ES6的数组方法map

    先举个常见的栗子: var arr = [1,2,3,4,6,7,8,9,12,3,25,63,100] var arr2 = arr.map(item => item += 1) consol ...

  2. java socket通讯

    本来是打算验证java socket是不是单线程操作,也就是一次只能处理一个请求,处理完之后才能继续处理下一个请求.但是在其中又发现了许多问题,在编程的时候需要十分注意,今天就拿出来跟大家分享一下. ...

  3. FTP服务器虚拟用户配置

    FTP服务配置问题及解决方案 使用被动模式,设置云主机IP为被动模式数据传输地址:在配置文件内添加 pasv_enable=YES pasv_promiscuous=YES pasv_address= ...

  4. 【转】早该知道的7个JavaScript技巧

    我写JAVAScript代码已经很久了,都记不起是什么年代开始的了.对于JavaScript这种语言近几年所取得的成就,我感到非常的兴奋:我很幸运也是这些成就的获益者.我写了不少的文章,章节,还有一本 ...

  5. Linux删除文件 清除缓存

    相信很多测试 经常会经历开发叫你清除缓存这种事. 那我们要怎么清呢? 一.首先,确认你要清除的缓存在哪个目录下,然后切换到该目录下,比如 我现在知道我的的缓存目录是在newerp这个目录下,则如图 二 ...

  6. 三分钟带你入门GitHub

    一,首先,我们来说一下什么是GitHub GitHub是一个基于git打造的开源社区 ,同时也是一个大型同性交友平台 ,作为一个专业的程序员,你非常有必要知道并使用GitHub:作为一个国际化社区,所 ...

  7. 如何用好Go的测试黑科技

    测试是每一个开发人员都需要掌握的技能,尽管你不需要像测试人员那么专业,但你也应该尽可能的做到那么专业,据我了解到我身边的一些Go开发人员,他们对Go的测试仅仅局限于写一个_test.go 测试文件,对 ...

  8. Nginx在Centos 7中配置开机启动

    1.创建脚本 # vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v ...

  9. Java.数据结构.集合体系详解

    I. 第一部分:常见数据结构 首先简单说下数据结构. 什么是数据结构?数据结构就是组织数据的方式. 常见的数据结构:栈,堆,树,图,数组,队列,链表. 这里主要介绍与java集合体系相关的栈.数组和链 ...

  10. MySql 常用时间函数

    1.date() 提取日期或日期时间表达式的日期部分 select date(create_time) from blog_article; 2.date_format() select date_f ...