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 顺便做出与 ...
随机推荐
- Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的. 所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可以完 ...
- uboot生成随机的MAC地址
转载:http://blog.chinaunix.net/uid-25885064-id-3303132.html 在使用U-boot时,有个问题就是MAC地址的设置,如果MAC地址相同的两块开发板在 ...
- ASP.NET动态网站制作(13)-- JQ(5)
前言:jq的最后一节课,主要讲解应用, 内容: 1.会飞的li: HTML代码: <!DOCTYPE html> <html xmlns="http://www.w3.or ...
- 《Java设计模式》之构建者模式
概述: 构造者模式(Builder Pattern):构造者模式将一个复杂对象的构造过程和它的表现层分离开来.使得相同的构建过程能够创建不同的表示,又称为生成器模式. Bu ...
- java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object;
spring3_hibernate 集成报错信息 java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljav ...
- 五个知识体系之-SQL语句大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- HUD3689 Infinite monkey theorem
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 九度OJ 1153:括号匹配问题 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5193 解决:2248 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括 ...
- 【题解】P3162CQOI2012组装
[题解][CQOI2012]组装 考虑化为代数的形式,序列\(\left[a_i \right]\)表示选取的\(i\)种类仓库的坐标. \(ans=\Sigma(a_i-x)^2,(*)\),展开: ...
- 解决MAC Appium设备连不上IOS的的问题'idevice_id' program is not installed
解决MAC Appium设备连不上IOS的的问题 错误的: [XCUITest] The 'idevice_id' program is not installed. If you are runni ...