题目大意

给定一棵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的更多相关文章

  1. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

  2. 【POJ 3764】 The xor-longest path

    [题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...

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

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

  4. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  5. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

  6. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  7. 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 ...

  8. 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 ...

  9. POJ 3764 DFS+trie树

    题意: 给你一棵树,求树中最长的xor路径.(n<=100000) 思路: 首先我们知道 A xor B =(A xor C) xor (B xor C) 我们可以随便选一个点DFS 顺便做出与 ...

随机推荐

  1. cmake学习之- set

    最后更新: 2019-06-06 一.简单介绍 set 和 unset 为一对相反的指令,分别为设置变量以及取消设置变量. 什么是变量? 变量就是用于保存值的存储单元. set 有哪几种? set 有 ...

  2. AndroidX86模拟器Genymotion的一些使用和另一款Andy模拟器

    命令行启动虚拟机 当我们下载安装好,可以通过命令行运行指定名字模拟器 D:\ProgramFiles\Genymobile\Genymotion\player  --vm-name "Sam ...

  3. org hibernate querytimeoutexception

    起因 在做Hibernate批量插入时,出现这个错误org.hibernate.QueryTimeoutException: 错误原因是表空间的容量不足,需要加大空间容量:那首先想到的是应该查询其容量 ...

  4. 17 nginx连接memcached

    一:配置php扩展memcached wget http://memcached.googlecode.com/files/memcached-1.4.9.tar.gz # tar zvxf memc ...

  5. 转载 -- iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...

  6. python cookbook第三版学习笔记十一:类和对象(二)调用父类的方法

    在子类中调用父类的方法,可以下面的A.spam(self)的方法. class A(object):     def spam(self):         print 'A.spam' class ...

  7. STO存在哪些潜在隐患?

    STO(Security Token Offering),即证券型通证发行,无疑是现目前区块链圈子讨论最热门的话题之一,纵使STO有很好的前景,但是其潜在隐患也不得不引起重视. 第一,STO与分布式网 ...

  8. PHP接口中的静态变量、常量与类中静态变量、常量的区别

    接口: 1 不能够定义静态变量(常量除外) 2 定义的常量 const YOUCONST = VALUE,不能在子类中覆盖,在子类中以 interfaceName::YOUCONST的方式调用 3 不 ...

  9. PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP

    $URL['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME ...

  10. Scipy.sparse矩阵的存储,读取和转化为稠密矩阵

    import numpy as np import scipy.sparse as sp m = sp.lil_matrix((7329,7329)) np.save(path,m) #用numpy的 ...