首先考虑暴力,可以枚举每两个点求lca进行计算,复杂度O(n^3logn),再考虑如果枚举每个点作为lca去枚举这个点的子树中的点复杂度会大幅下降,如果我们将每个点递归考虑,每次计算过这个点就把这个点删掉,那么如果每次删掉的点是当前子树的重心,枚举点对的复杂度就只有O(logn),对于每次查询答案在trie树中找到时间复杂度基本可视为O(1),最后在分治同时考虑“关键点”即可。总复杂度O(nlogn)。

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime> using namespace std; struct Edge
{
int to,next;
}e[]; int n,k,Ans=-;
int Size[],cnt,p[],f[],a[];
bool visited[]; void Add_edge(const int x,const int y)
{
e[++cnt].to=y;
e[cnt].next=p[x];
p[x]=cnt;
return ;
} struct Trie
{
private:
int c[][];
int d[];
int cnt,root; public:
void insert(const int val,const int flag)
{
int pos=root,temp;
for(int i=;i>=;--i)
{
temp=!!(val&(<<i));
if(!c[pos][temp])
{
c[pos][temp]=++cnt;
c[cnt][]=c[cnt][]=;
d[cnt]=;
}
d[pos]=max(d[pos],flag);
pos=c[pos][temp];
}
d[pos]=max(d[pos],flag);
return ;
} void query(const int val,const int flag)
{
int temp,num=,pos=root;
for(int i=;i>=;--i)
{
temp=!(val&(<<i));
if(c[pos][temp] && d[c[pos][temp]]+flag>=k)
{
pos=c[pos][temp];
num|=(<<i);
}
else
{
if(!c[pos][temp^] || d[c[pos][temp^]]+flag<k)
{
num=-;
break;
}
else pos=c[pos][temp^];
}
}
if(k<=flag)Ans=max(Ans,val);
Ans=max(Ans,num);
} void clear()
{
root=;
cnt=;
c[root][]=c[root][]=;
d[root]=,d[]=;
return ;
}
}T; int Dfs(const int S,const int fa)
{
Size[S]=;
for(int i=p[S];i;i=e[i].next)
{
if(e[i].to!=fa && !visited[e[i].to])
Size[S]+=Dfs(e[i].to,S);
}
return Size[S];
} void Get(const int S,const int fa,int & Centre,int & tot,const int nn)
{
int Max=,i; for(i=p[S];i;i=e[i].next)
{
if(e[i].to!=fa && !visited[e[i].to])
{
Get(e[i].to,S,Centre,tot,nn);
Max=max(Max,Size[e[i].to]);
}
}
Max=max(Max,nn-Size[S]);
if(tot>Max)tot=Max,Centre=S;
return ;
} void Query(const int S,const int fa,int A,int F)
{
A^=a[S];F+=f[S];
T.query(A,F);
for(int i=p[S];i;i=e[i].next)
{
if(e[i].to!=fa && !visited[e[i].to])
Query(e[i].to,S,A,F);
}
return ;
} void Insert(const int S,const int fa,int A,int F)
{
A^=a[S];F+=f[S];
T.insert(A,F);
for(int i=p[S];i;i=e[i].next)
{
if(e[i].to!=fa && !visited[e[i].to])
Insert(e[i].to,S,A,F);
}
return ;
} void TDC(const int S)
{
int i,Centre; visited[S]=true;
for(i=p[S];i;i=e[i].next)
{
if(!visited[e[i].to])
{
int tot=n+;
Dfs(e[i].to,);
Get(e[i].to,,Centre,tot,Size[e[i].to]);
TDC(Centre);
}
}
T.clear();
for(i=p[S];i;i=e[i].next)
{
if(!visited[e[i].to])
{
Query(e[i].to,,a[S],f[S]);
Insert(e[i].to,,,);
}
}
T.query(a[S],f[S]);
visited[S]=false;
return ;
} int main()
{
int i,x,y; scanf("%d%d",&n,&k);
for(i=;i<=n;++i)scanf("%d",&f[i]);
for(i=;i<=n;++i)scanf("%d",&a[i]); for(i=;i<n;++i)
{
scanf("%d%d",&x,&y);
Add_edge(x,y);
Add_edge(y,x);
} TDC(); printf("%d\n",Ans); return ;
}

[TS-A1486][2013中国国家集训队第二次作业]树[树的重心,点分治]的更多相关文章

  1. [tsA1491][2013中国国家集训队第二次作业]家族[并查集]

    m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...

  2. [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]

    这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...

  3. [TS-A1489][2013中国国家集训队第二次作业]抽奖[概率dp]

    概率dp第一题,开始根本没搞懂,后来看了09年汤可因论文才基本搞懂,关键就是递推的时候做差比较一下,考虑新加入的情况对期望值的贡献,然后推推公式(好像还是不太会推qaq...) #include &l ...

  4. [TS-A1488][2013中国国家集训队第二次作业]魔法波[高斯消元]

    暴力直接解异或方程组,O(n^6)无法接受,那么我们考虑把格子分块,横着和竖着分别分为互不影响的块,这样因为障碍物最多不超过200个,那么块的个数最多为2*(800+200)=2000个,最后用bit ...

  5. [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]

    根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...

  6. [TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]

    按Dfs序逐个插入点,建立可持久化线段树,每次查询即可,具体详见代码. 不知道为什么,代码慢的要死,, #include <iostream> #include <algorithm ...

  7. < < < 2013年国家集训队作业 > > >

    完成题数/总题数:  道/37道 1.  A1504. Book(王迪): 数论+贪心   ★★☆        2013中国国家集训队第二次作业 2.  A1505. 树(张闻涛): 倍增LCA+可 ...

  8. [转] ACM中国国家集训队论文集目录(1999-2009)

    国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...

  9. BZOJ4317Atm的树&BZOJ2051A Problem For Fun&BZOJ2117[2010国家集训队]Crash的旅游计划——二分答案+动态点分治(点分树套线段树/点分树+vector)

    题目描述 Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径都有边权,一个神秘的声音告诉他,每个点到其他的 ...

随机推荐

  1. 异常值检测(Detecting Outliers)

    Most statistical approaches to outlier detection are based on building a probability distribution mo ...

  2. ubuntu系统快捷键设置

    1.打开'系统设置' 2.点击键盘 3.选择快捷键,查看和修改对应的快捷键.

  3. 湖南集训day8

    难度:☆☆☆☆☆☆☆ /* 可以先考虑一维,可知 模k意义下相同的前缀和任意两个相减都是k的倍数 问题等价于统计前缀何种模k相同的数的对数. 多维的时候二维前缀和,压行或者压列,n^3可以解决. */ ...

  4. codevs1993 草地排水(最大流)

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在农夫约翰的农场上,每逢下雨,Bes ...

  5. 【转】深入理解Java多态原理

    之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...

  6. 免费开源ERP成功案例分享:化学之家通过Odoo实现工业互联网转型

    本文来自<开源智造Odoo客户成功案例采访实录>的精选内容章节.请勿转载.欢迎您反馈阅读意见. 客户地区:江苏常州 客户名称:化学之家(中外合资) 所属行业:化工制造(工业) 实施模块:销 ...

  7. win快速搜索软件

    Everything 与其他搜索工具的简单比较: Everything 是至今为止 X-Force 所使用过速度最快的文件搜索工具.与它相似的有异次元曾经介绍过一款很老的软件AVAFind,也非常的优 ...

  8. [转]android 获取 imei号码

    核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)) .getDeviceId(); 1.加入权限 在manife ...

  9. 2018.10.9 logstash启动慢的问题解决

    问题描述: 线上部署logstash 启动非常缓慢,达到了10分钟 问题解决 忍无可忍 网上资料基本都是熵过低导致jruby启动缓慢的问题,需要安装haveged 参考https://www.jian ...

  10. WinForm 之 使用ListView控件展示数据

    在学习了这么多的WinForm基本控件后,今天在来学习一个比较有意思的包含图片的控件! >>>图像列表控件 ImageList是含有图像对象的集合,可以通过索引或关键字引用该集合中的 ...