【POJ 3764】The Xor-longest Path
题目
给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径。
\(n\le 10^5\)
分析
一个简单的例子
相信大家都做过这题:
给定一个\(n\)个点的带权无根树,有\(m\)个询问,要求树上两点之间的权值异或和。
\(n,m\le 10^7\)
Xor运算有一些很显然的性质:
\(a \oplus a = 0\)
\(a \oplus b = b \oplus a\)
\(a\oplus b\oplus c = a\oplus(b\oplus c)\)
对于这道水题,我们只需要随便取一个点作为根节点,预处理节点\(i\)到根节点的路径异或和\(dist[i]\),然后\(a,b\)的路径的异或和即为\(dist[a]\oplus dist[b]\oplus dist[LCA(a,b)]\oplus dist[LCA(a,b)]=dist[a]\oplus dist[b]\)
回到本题
回到我们这题,我们只需要随便取一个点作为根节点,利用dfs处理出每个点到根节点的路径异或和,求这些异或和的最大异或即可,因为它们的LCA到根节点的路径被抵消了。
利用Trie实现求最大异或和。
代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct edge {
int to, nxt, val;
} edges[MAXN << 1];
int head[MAXN], path[MAXN], trie[MAXN << 5][2], num = 2, tot = 0, cnt = 0, n;
bool vis[MAXN];
void addedge(int from, int to, int val) {
edges[cnt].to = to, edges[cnt].val = val, edges[cnt].nxt = head[from];
head[from] = cnt++;
}
void dfs(int x, int w) {
vis[x] = 1;
path[tot++] = w;
for(int i = head[x]; i != -1; i = edges[i].nxt) {
if(!vis[edges[i].to]) {
dfs(edges[i].to, w ^ edges[i].val);
}
}
}
void insert(int x) {
int cur = 1;
for(int i = 30; i >= 0; i--) {
int p = (x >> i) & 1;
if(trie[cur][p]) cur = trie[cur][p];
else cur = trie[cur][p] = num++;
}
}
int getmaxxor(int x) {
int cur = 1, ans = 0;
for(int i = 30; i >= 0; i--) {
int p = ((x >> i) & 1) ^ 1;
if(trie[cur][p]) {
cur = trie[cur][p];
ans = ans << 1 | 1;
} else {
cur = trie[cur][p ^ 1];
ans <<= 1;
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
memset(head, 0xff, sizeof(head));
int u, v, w;
cin >> n;
for(int i = 0; i < n - 1; i++) {
cin >> u >> v >> w;
addedge(u, v, w);
addedge(v, u, w);
}
dfs(1, 0);
int ans = 0;
for(int i = 0; i < n; i++) insert(path[i]);
for(int i = 0; i < n; i++) ans = max(ans, getmaxxor(path[i]));
cout << ans << endl;
return 0;
}
【POJ 3764】The Xor-longest Path的更多相关文章
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- Spring Boot概要
1.Spring Boot使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置)的理念,使用户的项目实现快速运行.通过学习Spring Boot中的配置文件application. ...
- Cocos2d-x v3.1 坐标系统(五)
Cocos2d-x v3.1 坐标系统(五) 为了能够更好的布局以及了解对象所在的位置,我们必须对Cocos2d-x中的坐标有详细的了解,本篇文章主要就是了解Cocos中用到的坐标系统.学过数学的人都 ...
- cocos2d-x 学习资料汇总
cocos2d-x配置问题 - 我要飞的更高 - 博客频道 - CSDN.NET Cocos2d-x win7 + vs2010 配置图文详解(亲测) - 子龙山人 - 博客园 WINDONWS7+V ...
- GitLab-CE-8.9.4 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: gitlab-8.9.4 bug tracking collaboration commercial development devops git ...
- EOS签名R值过大导致报错"is_canonical( c ): signature is not canonical"
简要 EOS中规定签名的R和S必须同时小于N/2才是合法的签名. 详细 EOS签名交易相对BTC和ETH来说,对签名的要求更加严格了. BTC中bip62规定了((Low S values in si ...
- PHP:php遍历数组each()方法总结
each()的作用是将数组当前元素的键值对拆成一个新数组,并把下一个元素作为当前元素.比如Array(...,'Robert'=>'Bob',...)中的'Robert'=>'Bob'键值 ...
- SEO人士一定要了解的搜索引擎惩罚原则
SEO人士一定要了解的搜索引擎惩罚原则 SEO 的人一般都知道SEO分为白帽,黑帽,甚至还有灰帽.简单说,如果你熟读过谷歌网站质量指南,就可以了解,符合搜索引擎质量规范并且符合用户体验的SEO ...
- RobotFramework:钉钉扫码登录UI自动化
背景: 遇到一个项目,使用的是钉钉扫码登录,一时间不知道该怎么下手了,还是先F12抓包看下都有什么数据传输吧. 分析: 先熟悉下钉钉扫码登录的逻辑,参考官文:https://open-doc.ding ...
- Linux 的歷史
Unix 狹義作業系統提供應用程式及命令直譯器. 作業系統發展初期並不具可攜性. Bell, GE 及 MIT 合作開發的 "Multice" 系統( 相容分時系統 ). 1969 ...
- Arguments Optional-freecodecamp算法题目
Arguments Optional 1.要求 创建一个计算两个参数之和的 function.如果只有一个参数,则返回一个 function,该 function 请求一个参数然后返回求和的结果. 如 ...