[TS-A1486][2013中国国家集训队第二次作业]树[树的重心,点分治]
首先考虑暴力,可以枚举每两个点求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中国国家集训队第二次作业]树[树的重心,点分治]的更多相关文章
- [tsA1491][2013中国国家集训队第二次作业]家族[并查集]
m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...
- [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]
这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...
- [TS-A1489][2013中国国家集训队第二次作业]抽奖[概率dp]
概率dp第一题,开始根本没搞懂,后来看了09年汤可因论文才基本搞懂,关键就是递推的时候做差比较一下,考虑新加入的情况对期望值的贡献,然后推推公式(好像还是不太会推qaq...) #include &l ...
- [TS-A1488][2013中国国家集训队第二次作业]魔法波[高斯消元]
暴力直接解异或方程组,O(n^6)无法接受,那么我们考虑把格子分块,横着和竖着分别分为互不影响的块,这样因为障碍物最多不超过200个,那么块的个数最多为2*(800+200)=2000个,最后用bit ...
- [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]
根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...
- [TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]
按Dfs序逐个插入点,建立可持久化线段树,每次查询即可,具体详见代码. 不知道为什么,代码慢的要死,, #include <iostream> #include <algorithm ...
- < < < 2013年国家集训队作业 > > >
完成题数/总题数: 道/37道 1. A1504. Book(王迪): 数论+贪心 ★★☆ 2013中国国家集训队第二次作业 2. A1505. 树(张闻涛): 倍增LCA+可 ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
- BZOJ4317Atm的树&BZOJ2051A Problem For Fun&BZOJ2117[2010国家集训队]Crash的旅游计划——二分答案+动态点分治(点分树套线段树/点分树+vector)
题目描述 Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径都有边权,一个神秘的声音告诉他,每个点到其他的 ...
随机推荐
- 使用display:flex;实现垂直水平居中
body,div{margin:0px;padding:0px;} .flex-container{display:flex;height:300px;background-color:#ddd;ju ...
- 基于行为树的AI 与 Behavior Designer插件
优点: 0.行为逻辑和状态数据分离,任何节点都可以反复利用. 1.高度模块化状态,去掉状态中的跳转逻辑,使得状态变成一个"行为". 2."行为" ...
- 清北刷题班day3 morning
P99zhx: 竞赛时间:???? 年?? 月?? 日??:??-??:??题目名称 a b c名称 a b c输入 a.in b.in c.in输出 a.out b.out c.out每个测试点时限 ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(4)iOS 版 App Store Connect
通过 iOS 版 App Store Connect,您可以在移动设备上查看销售数据.App 元数据和顾客评论.您还可以检查 App 状态.发布您 App 的新版本并回应“Resolution Cen ...
- Java 删除List元素的正确方式
方式一:使用Iterator的remove()方法 public class Test { public static void main(String[] args) { List<Strin ...
- DB2锁表或超时解决方案
DB2锁表或超时 一.场景 对数据表进行更新(查询没问题),错误提示如下: SQLCODE=-911, SQLSTATE=40001, DRIVER=3.63.75SQL0911N The curre ...
- python自动化测试学习笔记-2-字典、元组、字符串方法
一.字典 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割, ...
- Jsp入门小常识
因为选修了一门信息系统的课,选择了用jsp做了一个系统.在这期间自学了jsp的一点皮毛,特与大家分享: script标签:用于向jsp中嵌入java代码块,<% // embed java c ...
- 【BZOJ4241】历史研究(回滚莫队)
题目: BZOJ4241 分析: 本校某些julao乱膜的时候发明了个"回滚邹队",大概意思就是某个姓邹的太菜了进不了省队回滚去文化课 回滚莫队裸题qwq(话说这个名字是不是莫队本 ...
- vue-cli 打包优化
1. 优化打包体积 先上2个图 (上图A是优化前的各个js大小对比视图,下图B是优化后,还未完全优化完成的,不过也可以看得出来对比) 图A是3个压缩文件,包括部分图片和使用的所有js,体积都偏大 图B ...