裸的点分治。。

  及时把已经确定的询问清掉就能快不少。时间复杂度O(nlogn*p)

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
struct zs{int len,id;bool done;}q[];
struct zs1{int too,pre,dis;}e[maxn<<];int tot,last[maxn];
int st[maxn],top,pre,sz[maxn],mx[maxn],POI,dis[maxn];
bool gg[],can[],del[maxn];
int i,j,k,n,m,rt,mm; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
}
inline int max(int a,int b){return a>b?a:b;}
void getrt(int x,int fa){
sz[x]=,mx[x]=;
for(int i=last[x];i;i=e[i].pre)if(!del[e[i].too]&&e[i].too!=fa)
getrt(e[i].too,x),sz[x]+=sz[e[i].too],mx[x]=max(mx[x],sz[e[i].too]);
mx[x]=max(mx[x],POI-sz[x]);
if(mx[x]<mx[rt])rt=x;
}
void getpoi(int x,int fa){
st[++top]=dis[x];
for(int i=last[x];i;i=e[i].pre)if(!del[e[i].too]&&e[i].too!=fa)
dis[e[i].too]=dis[x]+e[i].dis,getpoi(e[i].too,x);
}
inline void updQ(){
mm=m;m=;
for(int i=;i<=mm;i++)if(!q[i].done)q[++m]=q[i];
}
void work(int x){
int i,j,k;bool flag=;
rt=,getrt(x,);
can[]=;pre=;
for(i=last[rt];i;i=e[i].pre)if(!del[e[i].too]){
dis[e[i].too]=e[i].dis,getpoi(e[i].too,rt);
for(j=pre;j<=top;j++)
for(k=;k<=m&&q[k].len>=st[j];k++)if(can[q[k].len-st[j]])q[k].done=,flag=;
while(pre<=top)
if(st[pre]<=q[].len)
can[st[pre++]]=;
else pre++;
}
if(flag)updQ();
if(!m)return;
while(top)
if(st[top]<=)can[st[top--]]=;else top--;
del[rt]=;
for(i=last[rt];i;i=e[i].pre)if(!del[e[i].too])POI=sz[e[i].too],work(e[i].too);
} inline void insert(int a,int b,int c){
e[++tot].too=b,e[tot].dis=c,e[tot].pre=last[a],last[a]=tot;
e[++tot].too=a,e[tot].dis=c,e[tot].pre=last[b],last[b]=tot;
}
bool cmp(zs a,zs b){return a.len>b.len;} int main(){mx[]=1e9;
n=read(),m=read();int m1=m;
for(i=;i<n;i++)j=read(),k=read(),insert(j,k,read());
for(i=;i<=m;i++)q[i].len=read(),q[i].id=i;
sort(q+,q++m,cmp);
while(m&&q[m].len==)q[m].done=,m--;
POI=n,work();
for(i=;i<=m;i++)gg[q[i].id]=;
for(i=;i<=m1;i++)puts(gg[i]?"No":"Yes");
return ;
}

[bzoj1316] 树上的询问的更多相关文章

  1. [bzoj1316]树上的询问_点分治

    树上的询问 bzoj-1316 题目大意:一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 注释:$1\le n\le 10^4$,$1\ ...

  2. [BZOJ1316]树上的询问 点分治

    1316: 树上的询问 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1017  Solved: 287[Submit][Status][Discus ...

  3. 【点分治】bzoj1316 树上的询问

    #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #defin ...

  4. 【BZOJ1316】树上的询问 点分治+set

    [BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...

  5. BZOJ 1316: 树上的询问( 点分治 + 平衡树 )

    直接点分治, 用平衡树(set就行了...)维护. -------------------------------------------------------------------------- ...

  6. BZOJ_1316_树上的询问_点分治

    BZOJ_1316_树上的询问_点分治 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整 ...

  7. 【bzoj1316】树上的询问 树的点分治+STL-set

    题目描述 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 输入 第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行 ...

  8. ac自动机fail树上按询问建立上跳指针——cf963D

    解法看着吓人,其实就是为了优化ac自动机上暴力跳fail指针.. 另外这题对于复杂度的分析很有学习价值 /* 给定一个母串s,再给定n个询问(k,m) 对于每个询问,求出长度最小的t,使t是s的子串, ...

  9. [POJ 1316] 树上的询问

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...

随机推荐

  1. Udacity并行计算课程笔记-The GPU Hardware and Parallel Communication Patterns

    本小节笔记大纲: 1.Communication patterns gather,scatter,stencil,transpose 2.GPU hardware & Programming ...

  2. 关于PLC

    学电气的一方面是单片机,一方面是PLC,,,,常常看到说选择比努力更重要,,单片机都很熟悉了,我就来介绍一下PLC..... 然后呢我先吹吹牛,,,目的是让大家相信我介绍的PLC绝对是亲身体验.... ...

  3. CentOS7.2 使用Shell安装Oracle12c

    一.操作系统说明 1.操作系统 版本 2.磁盘分区用量 二.安装必要的软件包 for pkg in 'binutils' 'compat-libcap1' 'compat-libstdc++-33' ...

  4. Spring Dynamic DataSource Routing

    Use AbstractRoutingDataSource to dynamicly switch datasources, see http://spring.io/blog/2007/01/23/ ...

  5. Python 接口测试(十)

    这里对接口测试9 进行优化升级,前端进行重构后的代码,源码已经开源 经过将近一个月的编写 , TIAPTest 接口测试平台 , 已经部署到服务器,开始运行了. http://60.205.187.1 ...

  6. fastq,sam文件一些小结(持续补充。。。)

    ST-E00211::H5L3NCCXY:::: chr14 141M = - ACTTCACCTCCTGGAGTCCTGGACTTCCCCACATCTCCCCTGCCCCTCCCACGTTTCCAT ...

  7. VMware_ubuntu设置共享文件夹

    1. 点击安装VMware tools 2.将/media/vmtool的压缩包复制到/home/pc/vm_tool下,应为原路径在root权限下竟然也是只读的,并且无法更改. 3.进入/home/ ...

  8. 基于阿里云的JavaEE系统框架介绍

    基于阿里云的系统框架展望 1) CDN 用于缓存静态文件等等.七牛和阿里的都还可以. 七牛要做的久一点,各种图片处理的接口要完善一些 阿里的CDN要稍微好一点点,但是没有不安全的访问方式,访问稍微没有 ...

  9. 找回Git中丢失的Commit

    在使用Git的过程中,有时候会因为一些误操作,比如reset.rebase.merge等.特别是在Commit之后又执行了git reset --hard HEAD强制回滚本地记录以及文件到服务器版本 ...

  10. copy&deepcopy

    import copy 字典参照列表结论,看是否有深层嵌套. a = {'name':1,'age':2} b = a a['name'] = 'ff' print(a) print(b) print ...