HDU 4757
可持久化trie树。不会可持久化数据结构的话推荐先看陈立杰的论文。先掌握可持久化线段树和可持久化trie树。
//可持久化trie树,题目已知一棵树,每个点有点权,询问一对点路径上点权与给定值异或的最大值
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100100
using 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<<i;
}
else{
p1=trie[p1].go[tmp];
p2=trie[p2].go[tmp];
p3=trie[p3].go[tmp];
}
}
return max(ans_tmp,num^val[LCA]);
} void solve(){
for(int i=1;i<=n;i++)
for(int j=head2[i];j!=-1;j=query[j].next){
int id=query[j].id;
if(ans[id]!=-1)continue;
ans[id]=findans(i,query[j].v,lca[id],query[j].w);
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
} void init(){
memset(head,-1,sizeof(head));
memset(head2,-1,sizeof(head2));
memset(ans,-1,sizeof(ans));
cnt=cnt2=0;
} int main(){
int u,v,w;
while(scanf("%d %d",&n,&m)!=EOF){
init();
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++){
scanf("%d %d",&u,&v);
addedge(u,v);
}
root[0]=0;
trie[0].go[0]=trie[0].go[1]=0;
trie[0].cnt=0;
tot=0;
dfs(1,0);
for(int i=1;i<=m;i++){
scanf("%d %d %d",&u,&v,&w);
addedge2(u,v,w,i);
}
for(int i=1;i<=n;i++) f[i]=i;
memset(flag,0,sizeof(flag));
tarjan(1,0);
solve();
}
return 0;
}
HDU 4757的更多相关文章
- HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Su ...
- HDU 4757 Tree(可持久化trie)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4757 题意:给出一棵树,节点有权值.每次询问x到y的路径上与z抑或的最大值. 思路:可持久化trie. ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
- HDU 4757 Tree 可持久化字典树 trie
http://acm.hdu.edu.cn/showproblem.php?pid=4757 给出一棵树,每个节点有权值,每次查询节点 (u,v) 以及 val,问 u 到 v 路径上的某个节点与 v ...
- HDU 4757 Tree
传送门 Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Prob ...
- HDU.4757.Tree(可持久化Trie)
题目链接 \(Description\) 给定一棵树,点有点权.\(Q\)次询问\(x,y,z\),求\(x\)到\(y\)的简单路径中,与\(z\)异或能得到的最大的数是多少. \(Solution ...
- HDU 4757 可持久化trie树
首先如果给定一些数,询问这些数中哪个数^给定的数的值最大的话,我们可以建立一颗trie树,根连接的两条边分别为0,1,表示二进制下第15位,那么我们可以建立一颗trie树,每一条从根到叶子节点的链表示 ...
- HDU 4757 Tree(可持续化字典树,lca)
题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...
- HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)
Problem Description Zero and One are good friends who always have fun with each other. This time, ...
随机推荐
- python魔法方法-自定义序列详解
自定义序列的相关魔法方法允许我们自己创建的类拥有序列的特性,让其使用起来就像 python 的内置序列(dict,tuple,list,string等). 如果要实现这个功能,就要遵循 python ...
- React Native 系列(四)
前言 本系列是基于React Native版本号0.44.3写的.RN支持CSS中的布局属性,因此可以使用CSS布局属性,这里就不详细地讲解了,这篇文章的重点主要是讲述一下RN中的Flex布局. CS ...
- 让新版Chrome支持本地跨域请求调试
1.创建一个Chrome的启动快捷方式: 2.右键点击快捷方式属性,然后在目标路径后面,添加以下参数: --disable-web-security --user-data-dir="e:\ ...
- 看雪论坛 破解exe 看雪CTF2017第一题分析-『CrackMe』-看雪安全论坛
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 逆向 黑客 破解 学习 论坛 『CrackMe』 http://bbs.pediy.co ...
- python中后端数据序列化不显示中文的解决方法
我们在前后端交互的时候,让序列化的数据更友好的显示,我们会用到 import json js = json.loads('{"name": "多多"}') pr ...
- JSONObject 和 JSONArray 的区别和用法
JSONObject 和 JSONArray 的数据表现形式不同: JSONObject的数据是用 { } 来表示的,例如: { "id" : "1", &q ...
- python学习一月总结_汇总大牛们的思想_值得收藏
''' 下面是我汇总的我学习一个月python(version:3.3.2)的所有笔记 你可以访问:http://www.python.org获取更多信息 你也可以访问:http://www.cnbl ...
- ExtJS 表单 submit时错误处理
这里不提success,提提Extjs 表单提交的failure方法. 在表单的提交中,当发生异常行为时通常分为三种情况 1. 无法连接到服务器 2. 表单验证错误 3. 业务逻辑错误 对应下面的代码 ...
- How to make a combo box with fulltext search autocomplete support?
I would like a user to be able to type in the second or third word from a TComboBoxitem and for that ...
- eclipse的项目和配置文件 .project .cproject .classpath .metadata
eclipse CDT建立project后在project name对应的目录下面会生成.project和.cproject两个隐藏文件. eclipse java建立project后在projec ...