bzoj 1954 & poj 3764 The xor-longest Path dfs+Trie
题目大意
给定一棵n个点的带权树,求树上最长的异或和路径
题解
因为\(xor\)操作满足可结合性,所以有
\(a\text{ }xor\text{ }b\text{ }xor\text{ }b = a\)
那么我们可以计算出每个点到根的xor距离,设为\(dis\)
那么我们知道\(dis_u\text{ }xor\text{ }dis_v\)即\(u,v\)之间的距离的xor值
所以我们把所有的\(dis\)插到01Trie里,再对每个\(dis\)值求最大即可
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 100010;
struct Edge{
int to,next,dis;
}G[maxn<<1];
int head[maxn],cnt;
void add(int u,int v,int d){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].dis = d;
}
inline void insert(int u,int v,int d){
add(u,v,d);add(v,u,d);
}
int dis[maxn];
#define v G[i].to
void dfs(int u,int fa){
for(int i = head[u];i;i=G[i].next){
if(v == fa) continue;
dis[v] = dis[u]^G[i].dis;
dfs(v,u);
}
}
#undef v
int ch[maxn*32][2],nodecnt;
bool ed[maxn*32];
inline void insert(int x){
int nw = 0;
for(int i = 31;i;--i){
int id = (bool)(x & (1 << (i-1)));
if(ch[nw][id] == 0) ch[nw][id] = ++nodecnt;
nw = ch[nw][id];
}ed[nw] = true;
}
inline int query(int x){
int ret = 0,nw = 0;
for(int i=31;i;--i){
int id = (bool)(x & (1 << (i-1)));
if(ch[nw][id^1] != 0){
ret |= (1<<(i-1));
nw = ch[nw][id^1];
}else nw = ch[nw][id];
}return ret;
}
inline void init(){
memset(head,0,sizeof head);
memset(ch,0,sizeof ch);
memset(ed,0,sizeof ed);
memset(dis,0,sizeof dis);
cnt = nodecnt = 0;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
init();
int u,v,d;
for(int i=1;i<n;++i){
read(u);read(v);read(d);
insert(u,v,d);
}dfs(1,0);
for(int i=1;i<=n;++i) insert(dis[i]);
int ans = 0;
for(int i=1;i<=n;++i){
ans = max(ans,query(dis[i]));
}printf("%d\n",ans);
}
getchar();getchar();
return 0;
}
bzoj 1954 & poj 3764 The xor-longest Path dfs+Trie的更多相关文章
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 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 ...
- 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 ...
- POJ 3764 DFS+trie树
题意: 给你一棵树,求树中最长的xor路径.(n<=100000) 思路: 首先我们知道 A xor B =(A xor C) xor (B xor C) 我们可以随便选一个点DFS 顺便做出与 ...
随机推荐
- NoSQL数据库的分类
- 软件工程第3次作业——Visual Studio 2017下针对代码覆盖率的C/C++单元测试
本项目Github地址(同时包括两个作业项目): Assignment03 -- https://github.com/Oberon-Zheng/SoftwareEngineeringAssignme ...
- 跟我一起写 Makefile(一)[转]
原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) 陈皓 概述—— 什么是makef ...
- linux下网卡绑定
网卡绑定的作用:1.冗余,防止单点故障 2.防止传输瓶颈 1.交换机端口绑定: system-view link-aggregation group 1 mode manual 比如把端口1和2进行绑 ...
- mac上利用minikube搭建kubernetes(k8s)环境
友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...
- Hadoop常见异常及其解决方式
1.Shell$ExitCodeException 现象:执行hadoop job时出现例如以下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : ...
- 帝国CMS万能标签ecmsinfo介绍
带模板的信息调用标签:[万能标签](ecmsinfo) 标签名称: 带模板的信息调用标签 (sys_GetEcmsInfo) 格式:[ecmsinfo]栏目ID/专题ID, 显示条数, 标题截取数, ...
- go test 上篇
前言 Go语言本身集成了轻量级的测试框架,由go test命令和testing包组成.包含单元测试和压力测试,是保证我们编写健壮Golang程序的有效工具. 演示环境 $ uname -a Darwi ...
- ArcGIS api for javascript 离线部署
一.说明: 1.本篇文章以及之后的相关的文章所使用的ArcGIS API for javascript 的版本是3.3.初学者为了学习简单,可以引用在线的 Javascript.但是如果只能连接内网, ...
- 创建laravel项目
下载项目到本地 git clone https://github.com/251068550/LaraBlog.git compoer安装 cd LaraBlog composer install 如 ...