询问树上是否存在距离为k[i]的点对

直接点分治把所有距离预处理出来,然后O(1)回答即可

Code

#include <cstdio>
#include <algorithm>
#define N 10010
using namespace std; const int mx=N*1000;
struct info{int to,nex,w;}e[N<<1];
int n,m,tot,head[N],Ans[mx],sz[N],rt,d[N],sum,f[N];
bool vis[N]; void Link(int u,int v,int w){
e[++tot].to=v,e[tot].w=w,e[tot].nex=head[u];head[u]=tot;
} inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void getrt(int u,int fa){
sz[u]=1,f[u]=0;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==fa||vis[v]) continue;
getrt(v,u);
sz[u]+=sz[v];
f[u]=max(f[u],sz[v]);
}
f[u]=max(f[u],sum-sz[u]);
if(f[rt]>f[u]) rt=u;
} void getdep(int u,int fa,int dep){
d[++d[0]]=dep;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==fa||vis[v]) continue;
getdep(v,u,dep+e[i].w);
}
} void calc(int u,int f,int pre){
d[0]=0;
getdep(u,0,0);
for(int i=1;i<=d[0];++i)
for(int j=i+1;j<=d[0];++j)
if(f&&d[i]+d[j]<=mx) ++Ans[d[i]+d[j]];
else if(d[i]+d[j]+pre<=mx) --Ans[d[i]+d[j]+pre];
} void solve(int u){
calc(u,1,0);
vis[u]=1;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(vis[v]) continue;
calc(v,0,e[i].w*2);
sum=sz[v];
getrt(v,rt=0);
solve(rt);
}
} int main(){
n=read(),m=read();
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
Link(u,v,w),Link(v,u,w);
}
sum=n,f[0]=1e9;
getrt(1,0);
solve(rt);
while(m--){
int x=read();
puts(Ans[x]?"AYE":"NAY");
}
return 0;
}

[Luogu3806]点分治的更多相关文章

  1. 【Luogu3806】点分治(点分治)

    [Luogu3806]点分治(点分治) 题面 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行 ...

  2. [luogu3806]【模板】点分治1

    description 求树上长度为\(k\)的路径是否存在. data range \[n\le 10000,k\le 10000000\] solution 点分治复习... 使用普通的点分治枚举 ...

  3. [bzoj2152][聪聪和可可] (点分治+概率)

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  6. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  7. BZOJ 3262 陌上花开 ——CDQ分治

    [题目分析] 多维问题,我们可以按照其中一维排序,然后把这一维抽象的改为时间. 然后剩下两维,就像简单题那样,排序一维,树状数组一维,按照时间分治即可. 挺有套路的一种算法. 时间的抽象很巧妙. 同种 ...

  8. BZOJ 1176 [Balkan2007]Mokia ——CDQ分治

    [题目分析] 同BZOJ2683,只需要提前处理s对结果的影响即可. CDQ的思路还是很清晰的. 排序解决一维, 分治时间, 树状数组解决一维. 复杂度是两个log [代码] #include < ...

  9. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

随机推荐

  1. Java执行Shell脚本

    Linux 系统下采用 Java 执行 Shell 脚本,直接上代码: package com.smbea.demo; import java.io.BufferedReader; import ja ...

  2. Windows和Linux执行Java代码的不同方式

    一.Windows 下编译并执行 Java 字节码文件(类文件) 1.编译 Hello.java 源码文件: java -d . Hello.java 2.执行 Hello.class 字节码文件: ...

  3. Design Pattern ->Composite

    Layering & Contract Philosophy With additional indirection class CComponent { ; ; ; public: virt ...

  4. Design Pattern ->Abstract Factory

    Layering & Contract Philosophy With additional indirection Abstract Factory //The example code i ...

  5. libmysqlclient.so.16未找到方法

    用mysql命令登录的时候报错: [root@iZ www]# mysql -uroot -p mysql: error while loading shared libraries: libmysq ...

  6. 如何让MVC和多层架构和谐并存(二)

    上一节说了一些笼统的东西,这节说一些实际的操作. 1.取列表.这是一个新闻列表:         对应MVC的model是: public class NewsListModel { /// < ...

  7. 数组模拟栈(C语言)

    用数组模拟栈的实现: #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef struct ...

  8. 源码安装zabbix3.2.7时PHP ldap Warning

    问题如下: 解决方法: 1.首先查看源码安装的php模块中是否有ldap.so [root@nms ldap]# ll  /usr/local/php/lib/php/extensions/no-de ...

  9. TP5.1:模板赋值与变量输出

    模板赋值:assign() 模板渲染:fetch() 前提准备: 1.在app/index/controller下建立一个控制器,名为Templates.php,里面有test1和test2方法,并且 ...

  10. 如何在markdown中实现缩进,空格,制表符

    参考 https://stackoverflow.com/questions/6046263/how-to-indent-a-few-lines-in-markdown-markup Markdown ...