题目大意

给定一棵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. LVS + KEEPALIVED + WINDOWS SERVER 2008 R2 ------高可用负载均衡(转)

    工作原理此处不作讲解,自己去官方网站学习(http://www.linuxvirtualserver.org),这里重点讲如何配置!注:最好从官方网站对其进行了解,不至于会对某些问题产生误解,尽管是英 ...

  2. cmake学习之-project

    一.系统版本 cmake version: 3.5.2 系统版本: Ubuntun 16.04 cmake docment: 3.14.4 最后更新: 2019-05-31 二.指令说明 projec ...

  3. spark之pycharm开发设置

    方法一: __author__ = 'similarface' import os import sys os.environ['SPARK_HOME']="/Users/similarfa ...

  4. jdbcTemplaate queryForObject的两个易混淆的方法

    JdbcTemplate中有两个可能会混淆的queryForObject方法: 1.    Object queryForObject(String sql, Object[] args, Class ...

  5. golang手动管理内存

    作者:John Graham-Cumming.   原文点击此处.翻译:Lubia Yang(已失效) 前些天我介绍了我们对Lua的使用,implement our new Web Applicati ...

  6. PowerBuilder -- 数据窗口

    获取数据窗口列数 ls_colnum= integer(this.Describe("DataWindow.Column.Count")) 获取数据窗口列名 ls_colName ...

  7. java 发展简史

    [0]README 0.1) 本文转自 core java volume 1,仅供了解Java 的发展历史,它的前世今生,所谓知己知彼,百战不殆(just a joke) : [1]java 发展简史 ...

  8. ASP.NET动态网站制作(5)-- 标签语义化及知识补充

    前言:这节课主要是讲标签语义化及一些知识点的补充 内容:参考老师的博文:http://www.cnblogs.com/ruanmou/p/4821894.html

  9. AppICon设置

  10. HTML5,不仅仅是看上去非常美(第二弹:打造最美3D机房)

    前言 近期项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,曾经一直搞java,对js的一些语言特性和概念一时还转换只是来. 上一篇第一 ...