传送门

解题思路

  \(trie\)的一个比较经典的应用,首先把每个点到根的异或和算出,然后建一棵\(trie\)把所有权值插入到\(Trie\)中,之后枚举所有结点,在\(Trie\)上贪心的跑统计答案,时间复杂度\(O(nlogn)\)

代码

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
const int N=100005; inline int rd(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
} int n,head[N],cnt,to[N<<1],nxt[N<<1],val[N<<1];
int tot,ans,res,w[N],rt; struct Trie{
int ch[N*40][2];
void insert(int &x,int d,int now){
if(!x) x=++tot; if(!d) return;
if((1<<(d-1))&now) insert(ch[x][1],d-1,now);
else insert(ch[x][0],d-1,now);
}
void query(int x,int d,int now){
if(!d) return;
if((1<<(d-1))&now) {
if(ch[x][0]) res|=(1<<(d-1)),query(ch[x][0],d-1,now);
else query(ch[x][1],d-1,now);
}
else {
if(ch[x][1]) res|=(1<<(d-1)),query(ch[x][1],d-1,now);
else query(ch[x][0],d-1,now);
}
}
}tree; inline void add(int bg,int ed,int w){
to[++cnt]=ed,nxt[cnt]=head[bg],val[cnt]=w,head[bg]=cnt;
} void dfs(int x,int F){
for(int i=head[x];i;i=nxt[i]){
int u=to[i]; if(u==F) continue;
w[to[i]]=(w[x]^val[i]); dfs(u,x);
}
} int main(){
n=rd(); int x,y,z;
for(int i=1;i<n;i++){
x=rd(),y=rd(),z=rd();
add(x,y,z),add(y,x,z);
}
dfs(1,0);
for(int i=1;i<=n;i++) tree.insert(rt,31,w[i]);
for(int i=1;i<=n;i++)
res=0,tree.query(rt,31,w[i]),ans=max(ans,res);
printf("%d\n",ans);
return 0;
}

BZOJ 1954: Pku3764 The xor-longest Path(贪心+trie)的更多相关文章

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

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

  2. bzoj 1954 & poj 3764 The xor-longest Path dfs+Trie

    题目大意 给定一棵n个点的带权树,求树上最长的异或和路径 题解 因为\(xor\)操作满足可结合性,所以有 \(a\text{ }xor\text{ }b\text{ }xor\text{ }b = ...

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

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

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

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

  6. 「LOJ#10056」「一本通 2.3 练习 5」The XOR-longest Path (Trie

    #10056. 「一本通 2.3 练习 5」The XOR-longest Path 题目描述 原题来自:POJ 3764 给定一棵 nnn 个点的带权树,求树上最长的异或和路径. 输入格式 第一行一 ...

  7. 51nod1295 XOR key(可持久化trie)

    1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...

  8. [多校联考2019(Round 4 T1)][51nod 1295]Xor key(可持久化trie)

    [51nod 1295]Xor key(可持久化trie) 题面 给出一个长度为n的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] 至 A[R] ...

  9. 【BZOJ】1954: Pku3764 The xor-longest Path

    [算法]trie树+xor路径 [题解] 套路1:统计从根到每个点的xor路径和,由于xor的自反性,两个点到根的xor路径和异或起来就得到两点间路径和. 然后问题就是找到n个值中异或值最大的两个值, ...

随机推荐

  1. HttpSessionBindingListener和HttpSessionAttributeListener区别

    HttpSessionBindingListener和HttpSessionAttributeListener是两个经常让初学者弄混的监听器,其实它们有很大的区别.这2个监听器在文章中简称为Bindi ...

  2. vue-wacth监听事件

    2019-08-05   0:20 Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化. 以下实例通过使用 watch 实现计数器:(此时我就想了一下,好像绑定点击事件,也 ...

  3. Bootstrap 学习笔记12 轮播插件

    轮播插件: <!-- data-ride="carousel"自动播放 --> <div id="myCarousel" class=&quo ...

  4. MySQL基础(创建库,创建表,添加数据)

    CREATE DATABASE 数据库名; CREATE TABLE student2(sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号&quo ...

  5. 洛谷 P3368 【模板】树状数组 2(区间加,单点查询)

    题目链接 https://www.luogu.org/problemnew/show/P3368 树状数组 最基础的用法:https://www.cnblogs.com/yinyuqin/p/1096 ...

  6. Python自学第二天学习之《列表》

    一.  列表:list类型,是有序的,可以被修改的. 格式 : li=["cd",1,"gfds",[1,2,3]] 1.类型转换: #字符串转换成列表 b=“ ...

  7. HDU 6386 Age of Moyu (最短路+set)

    <题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是1) ...

  8. eclipsePreferences位置

    1.Windows:菜单栏-Window-Preferences 2.Mac:应用顶部最左侧Eclipse-Preferences ---------------------------------- ...

  9. 本机ip地址怎么查

     转自:https://www.192ly.com/basic/local-ip-address-lookup-method.html 百度搜索一下[IP],你就可以轻松看到你的IP地址了,百度出来的 ...

  10. Runtime-iOS运行时应用篇

    一.动态方法交换:Method Swizzling实现动态方法交换(Method Swizzling )是Runtime中最具盛名的应用场景,其原理是:通过Runtime获取到方法实现的地址,进而动态 ...