[bzoj1316] 树上的询问
裸的点分治。。
及时把已经确定的询问清掉就能快不少。时间复杂度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] 树上的询问的更多相关文章
- [bzoj1316]树上的询问_点分治
树上的询问 bzoj-1316 题目大意:一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 注释:$1\le n\le 10^4$,$1\ ...
- [BZOJ1316]树上的询问 点分治
1316: 树上的询问 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1017 Solved: 287[Submit][Status][Discus ...
- 【点分治】bzoj1316 树上的询问
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #defin ...
- 【BZOJ1316】树上的询问 点分治+set
[BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...
- BZOJ 1316: 树上的询问( 点分治 + 平衡树 )
直接点分治, 用平衡树(set就行了...)维护. -------------------------------------------------------------------------- ...
- BZOJ_1316_树上的询问_点分治
BZOJ_1316_树上的询问_点分治 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整 ...
- 【bzoj1316】树上的询问 树的点分治+STL-set
题目描述 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 输入 第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行 ...
- ac自动机fail树上按询问建立上跳指针——cf963D
解法看着吓人,其实就是为了优化ac自动机上暴力跳fail指针.. 另外这题对于复杂度的分析很有学习价值 /* 给定一个母串s,再给定n个询问(k,m) 对于每个询问,求出长度最小的t,使t是s的子串, ...
- [POJ 1316] 树上的询问
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...
随机推荐
- ES6之遍历器(Iterator)
什么是Iterator?他是一种接口,为各种不同的数据结构提供统一的访问机制,任何数据结构只要部署上Iterator接口就可以完成遍历操作(PS:个人认为他的这个遍历就是c语言里面的指针),他的作用有 ...
- 小白的Python之路 day4 迭代器
迭代器 学习前,我们回想一下可以直接作用于for循环的数据类型有以下几种: 1.集合数据类型,如list.tuple.dict.set.str等: 2.是generator,包括生成器和带yield的 ...
- jstree 学习
最近的项目用到了jstree,因为对官方文档理解不充分,所以很多功能都是在网站上搜索再进行使用的.(我只是大自然的搬运工) 对每一级的节点,右键后出现不同的结果. 在jstree中右键是由 conte ...
- Xamarin Android自定义文本框
xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(m ...
- 工厂方法模式(Method Factory),理解多态应用的好例子.
工厂方法模式又称为多态性工厂,个人认为多态性工厂更能准确的表达这个模式的用处.与简单工厂(静态工厂)相比较,这里的多态性是指抽象出一个工厂基类,将因为一个产品有N种不同的是现这种变化封装起来,将具体的 ...
- Java解析word,获取文档中图片位置
前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...
- Zabbix自动发现监控Tomcat进程
1.编辑自动发现脚本 自动发现脚本只支持JSON格式 #!/usr/bin/env python # -*- coding:utf-8 -*- import commands import psuti ...
- C# 命名空间和程序集
一.命名空间 1.通过使用using关键字引入命名空间,减少代码量 命名空间对相关的类型进行逻辑分组,通过命名空间能快速的定位到相关的类型,例如:在System.IO命名空间下,定义了所有I/O操作的 ...
- python3 python2 import 的区别
https://stackoverflow.com/questions/12172791/changes-in-import-statement-python3
- Linux 下Beanstalk安装
1.安装 # wget https://github.com/kr/beanstalkd/archive/v1.10.tar.gz # tar xzvf v1.10 # cd beanstalkd-1 ...