传送门

解题思路

  \(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. Jquery.extend()和jQuery.fn.extend(object);

    摘自: jquery $.fn $.fx是什么意思有什么用_jquery_脚本之家 jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法. jQuery.fn.ex ...

  2. 2019/12/22 TZOJ

    4986 Team Formation http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=498 ...

  3. 5G网络的深度强化学习:联合波束成形,功率控制和干扰协调

    摘要:第五代无线通信(5G)支持大幅增加流量和数据速率,并提高语音呼叫的可靠性.在5G无线网络中共同优化波束成形,功率控制和干扰协调以增强最终用户的通信性能是一项重大挑战.在本文中,我们制定波束形成, ...

  4. shell 删除项目日志

    删除半年之前的日志 find 后面紧跟目录 .为当前目录 -type f 指定查找的是文件 -mtime +180  查找180天之前的 -name  文件名筛选 -exec -rm -rf  执行的 ...

  5. ArrayBlockingQueue使用

    阻塞与非阻塞: 阻塞: 阻塞调用是没有获得资源则挂起进程,被挂起的进程进入休眠状态,调用的函数只有在得到结果之后才返回,进程继续. 对象是否处于阻塞模式和函数是不是阻塞调用有很强的相关性,但并不是一一 ...

  6. Codeforces - 1088B - Ehab and subtraction - 堆

    https://codeforc.es/contest/1088/problem/B 模拟即可. #include<bits/stdc++.h> using namespace std; ...

  7. php cookie session 深究一下

    当一个用户用浏览器访问web(www.96net.com.cn)时候,若服务器开启session_start() 服务器tmp临时目录 自动生成session_id 并放回给创建一个cookie 保存 ...

  8. 在vue中设计一个客户签名的功能

    直接贴代码: <template> <div class="hello"> <p>签字:</p> <canvas id=&qu ...

  9. C#设计模式:原型模式(Prototype Pattern)

    一,原型模式:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建.(包含深度克隆和浅克隆) 主要面对的问题是:“某些结构复杂的对象”的创建工作:由于 ...

  10. MVC路由学习:自定义路由参数(用户看不到参数名),重新定义路由规则

    MVC路由:由于路由global中注册了,在程序第一次运行时,在MVC会自动生成路由,类似于字典的格式缓存下来,但路由生成的规则又是怎样的呢? 路由生成规则是: 1>更具你定义的的顺序查找路由规 ...