首先考虑暴力,可以枚举每两个点求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. bzoj3662

    数学 其实我们发现不用每个数都去试一下,只要确定每个数字有几个就可以确定这个数.所以我们先搜索一下,然后检验. 但是这样太慢了,所以我们打表. 打出1-30的结果,然后取模. 打表的程序好像弄丢了.. ...

  2. phpexecl 的基本操作

    基本使用方法分三部分:一.引入接口 // PHPExcel_IOFactory require_once dirname(__FILE__).'/Classes/PHPExcel/IOFactory. ...

  3. P3291 [SCOI2016]妖怪

    传送门 我数学的确白学了--这种题目竟然一点思路都没有-- 首先可以把每个妖怪看成二维平面上的一个点,那么每一个环境\((a,b)\)就可以看成一条斜率\(k=-\frac{b}{a}\)的过该点的直 ...

  4. [Swift通天遁地]四、网络和线程-(9)上传图片并实时显示上传进度

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. mac 安装 swoole 可能会出现的错误

    请先看完之后再操作 一.用pecl安装swoole(没有安装起来) 2018年4月,由于homebrew的变动,导致无法使用brew install的方式安装php的扩展,现在改为用pecl安装,pe ...

  6. dubbo+zookeeper下生产者和消费者配置(基于springboot开发)

    一.总共分为三个目录: dubbo-api      服务的接口用于对接客户端和服务端 dubbo-client     客户端配置文件为:consumer.xml dubbo-service  服务 ...

  7. 题解报告:hihoCoder #1175:拓扑排序·二

    题目链接:https://hihocoder.com/problemset/problem/1175 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学 ...

  8. 4CSS颜色和背景

    ---------------------------------------------------------------------------------------------------- ...

  9. spring boot打包文件后,报错\No such file or directory

    现象: 一段代码: ClassLoader loader = XXXUtil.class.getClassLoader(); String jsFileName = loader.getResourc ...

  10. c# winform控件dock属性停造位置、摆放顺序详解

    dock : [英文释义- 码头.依靠][winform释义- 获取或设置当前控件依靠到父容器的哪一个边缘.] 用途:多数控件都有这个属性,主要用来设置控件的布局. 但对于不太了解这个属性的朋友来说有 ...