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


记 \(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. 【转】ArcGIS Server 站点架构-Web Adaptor

    GIS 服务器内置了Web服务器,如果我想用我自己企业内部的服务器,该怎么做? 多个GIS服务器集群又如何做? …… 有问题,说明我们在思考,这也是我们希望看到的,因为只有不断的思考,不断的问自己为什 ...

  2. css3让元素自适应高度

    知识点: viewport:可视窗口,也就是浏览器.vw Viewport宽度, 1vw 等于viewport宽度的1%vh Viewport高度, 1vh 等于viewport高的的1% calc( ...

  3. 源码分析Kafka 消息拉取流程

    目录 1.KafkaConsumer poll 详解 2.Fetcher 类详解 本节重点讨论 Kafka 的消息拉起流程. @(本节目录) 1.KafkaConsumer poll 详解 消息拉起主 ...

  4. 关于django中的get_or_create方法的坑

    最近在项目中发现了这样的一个坑,那就是我们的需求是不能添加一个相同的对象到数据库中,就通过某些字段的值组合成唯一值到数据库中去查找数据,如果没有找到对象,那就创建一条新的数据库记录,而刚好django ...

  5. 第二篇:python中的字符串资源详述

    字符串资源使用方法详解 工具:Pycharm python环境:anaconda 接下来开始逐一解释: 如图: test后敲个点,就可以调用框框内的所有函数(功能),典型的面向对象思想. 上面只是简单 ...

  6. PBFT && RBFT算法流程

    PBFT && RBFT算法流程以及其实现(上) 这篇文章主要是讲一下RBFT中共识算法流程以及节点的加入的流程.在下一篇博客中,将使用Java实现该算法. 传统的PBFT算法无法动态 ...

  7. HTTP图解笔记(六)—— 第6章 HTTP首部

    前言 为啥第一章直接跳到第六章呢,因为...博主当初看书的时候挑着看..只看了第一章和第六章┗( ▔, ▔ )┛ HTTP图解对于不熟悉HTTP的小伙伴来说是很好的书籍,建议入手! 一. HTTP报文 ...

  8. 让你的 Linux 命令骚起来

    目录 管道符号 " | " grep sed awk sort comm uniq tr cat head tail wc find tsort tee 「>」重定向符号 「 ...

  9. 【Flink】Flink作业调度流程分析

    1. 概述 当向Flink集群提交用户作业时,从用户角度看,只需要作业处理逻辑正确,输出正确的结果即可:而不用关心作业何时被调度的,作业申请的资源又是如何被分配的以及作业何时会结束:但是了解作业在运行 ...

  10. [bzoj5507] [洛谷P5305] [gzoi2019]旧词

    Descriptioin 浮生有梦三千场 穷尽千里诗酒荒 徒把理想倾倒 不如早还乡 温一壶风尘的酒 独饮往事迢迢 举杯轻思量 泪如潮青丝留他方 --乌糟兽/愚青<旧词> 你已经解决了五个问 ...