博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4757
阅读量:4350 次
发布时间:2019-06-07

本文共 1922 字,大约阅读时间需要 6 分钟。

可持久化trie树。不会可持久化数据结构的话推荐先看陈立杰的论文。先掌握可持久化线段树和可持久化trie树。

 

//可持久化trie树,题目已知一棵树,每个点有点权,询问一对点路径上点权与给定值异或的最大值#pragma comment(linker, "/STACK:1024000000,1024000000")#include 
#include
#include
#define N 100100using namespace std;struct Edge{ int v,next;}edge[N*2];int head[N],val[N],cnt,n,m;void addedge(int u,int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].next=head[v]; head[v]=cnt++;}struct Trie{ int go[2],cnt;}trie[N*17];int root[N],tot;int insert(int id,int num){ int p=++tot,tem=p; trie[p]=trie[id]; for(int i=15;i>=0;i--){ int tmp=(num>>i)&1; trie[++tot]=trie[trie[p].go[tmp]]; trie[tot].cnt++; trie[p].go[tmp]=tot; p=tot; } return tem;}void dfs(int u,int father){ root[u]=insert(root[father],val[u]); for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==father)continue; dfs(v,u); }}struct Query{ int v,id,w,next;}query[N*2];int head2[N],cnt2,f[N],lca[N],ans[N];bool flag[N];void addedge2(int u,int v,int w,int id){ query[cnt2].v=v; query[cnt2].w=w; query[cnt2].id=id; query[cnt2].next=head2[u]; head2[u]=cnt2++; query[cnt2].v=u; query[cnt2].w=w; query[cnt2].id=id; query[cnt2].next=head2[v]; head2[v]=cnt2++;}int find(int u){ if(u==f[u]) return u; return f[u]=find(f[u]);}void tarjan(int u,int father){ for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==father) continue; tarjan(v,u); f[v]=u; } flag[u]=1; for(int i=head2[u];i!=-1;i=query[i].next){ int v=query[i].v; if(flag[v]){ lca[query[i].id]=find(v); } }}int findans(int u,int v,int LCA,int num){ int p1=root[u],p2=root[v],p3=root[LCA],ans_tmp=0; for(int i=15;i>=0;i--){ int tmp=(num>>i)&1; int sum=trie[trie[p1].go[!tmp]].cnt+trie[trie[p2].go[!tmp]].cnt-2*trie[trie[p3].go[!tmp]].cnt; if(sum>0){ p1=trie[p1].go[!tmp]; p2=trie[p2].go[!tmp]; p3=trie[p3].go[!tmp]; ans_tmp+=1<

 

 

转载于:https://www.cnblogs.com/james1207/p/3367935.html

你可能感兴趣的文章
前端javascript 错误 Uncaught SyntaxError: Unexpected token ILLEGAL
查看>>
2017.4.18 Java的Integer与int互转
查看>>
小程序接受返回数组的坑
查看>>
echart.js的使用
查看>>
自己动手写一个单链表
查看>>
生产者与消费者(综合案例)
查看>>
常用正则表达式
查看>>
6.2.7 Math对象的使用
查看>>
PHP 重置数组为连续数字索引的几种方式
查看>>
南阳理工acm 88-汉诺塔(一)
查看>>
160809308周子济第六次作业
查看>>
大型Web应用运行时 PHP负载均衡指南
查看>>
为phpStorm 配置PHP_CodeSniffer自动检查代码
查看>>
测试工具网址大全(转)
查看>>
ServiceStack DotNet Core前期准备
查看>>
webpack中‘vant’全局引入和按需引入【vue-cli】
查看>>
Date、String和Timestamp类型转换
查看>>
计算机的组成
查看>>
CSS命名规范
查看>>
初始化构造函数中定义的实体集合,方便嵌套类型的遍历
查看>>