【BZOJ1316】树上的询问 点分治+set
【BZOJ1316】树上的询问
Description
Input
Output
Sample Input
1 2 5
1 3 7
1 4 1
3 5 2
3 6 3
1
8
13
14
Sample Output
Yes
No
Yes
HINT
30%的数据,n≤100.
100%的数据,n≤10000,p≤100,长度≤1000000.
题解:直接点分治。本题可以采用树形DP形态的点分治,用set维护在x的第1..i-1号儿子的所有子树中,所有出现过的深度,然后在扫第i个儿子时顺便用(Len-当前dep)在set里找一下更新答案。复杂度O(nlog2n*100)。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=10010;
int n,m,cnt,tot,rt,mn;
int to[maxn<<1],next[maxn<<1],val[maxn<<1],siz[maxn],head[maxn],q[110],ans[110],vis[maxn],p[maxn];
set<int> s;
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void add(int a,int b,int c)
{
to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
}
void getrt(int x,int fa)
{
siz[x]=1;
int i,tmp=0;
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa&&!vis[to[i]])
getrt(to[i],x),siz[x]+=siz[to[i]],tmp=max(tmp,siz[to[i]]);
tmp=max(tmp,tot-siz[x]);
if(tmp<mn) mn=tmp,rt=x;
}
void getdep(int x,int fa,int dep)
{
int i;
p[++p[0]]=dep,siz[x]=1;
for(i=1;i<=m;i++) ans[i]|=(s.find(q[i]-dep)!=s.end());
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa&&!vis[to[i]]) getdep(to[i],x,dep+val[i]),siz[x]+=siz[to[i]];
}
void dfs(int x)
{
vis[x]=1;
int i,j;
s.clear(),s.insert(0);
for(i=head[x];i!=-1;i=next[i]) if(!vis[to[i]])
{
p[0]=0,getdep(to[i],x,val[i]);
for(j=1;j<=p[0];j++) s.insert(p[j]);
}
for(i=head[x];i!=-1;i=next[i]) if(!vis[to[i]]) tot=siz[to[i]],mn=1<<30,getrt(to[i],x),dfs(rt);
}
int main()
{
n=rd(),m=rd();
int i,a,b,c;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),c=rd(),add(a,b,c),add(b,a,c);
for(i=1;i<=m;i++) q[i]=rd();
tot=n,mn=1<<30,getrt(1,0),dfs(rt);
for(i=1;i<=m;i++)
{
if(!q[i]||ans[i]) printf("Yes\n");
else printf("No\n");
}
return 0;
}
【BZOJ1316】树上的询问 点分治+set的更多相关文章
- [BZOJ1316]树上的询问 点分治
1316: 树上的询问 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1017 Solved: 287[Submit][Status][Discus ...
- [bzoj1316]树上的询问_点分治
树上的询问 bzoj-1316 题目大意:一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 注释:$1\le n\le 10^4$,$1\ ...
- BZOJ 1316: 树上的询问( 点分治 + 平衡树 )
直接点分治, 用平衡树(set就行了...)维护. -------------------------------------------------------------------------- ...
- 【点分治】bzoj1316 树上的询问
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #defin ...
- [bzoj1316] 树上的询问
裸的点分治.. 及时把已经确定的询问清掉就能快不少.时间复杂度O(nlogn*p) #include<cstdio> #include<iostream> #include&l ...
- BZOJ_1316_树上的询问_点分治
BZOJ_1316_树上的询问_点分治 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整 ...
- 【BZOJ-3784】树上的路径 点分治 + ST + 堆
3784: 树上的路径 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 462 Solved: 153[Submit][Status][Discuss ...
- 【Luogu2664】树上游戏(点分治)
[Luogu2664]树上游戏(点分治) 题面 洛谷 题解 很好的一道点分治题. 首先直接点分治,考虑过每个分治重心的链的贡献. 我们从分治重心开始找每种颜色,强制令一种颜色只在其到分治重心的链上第一 ...
- 【bzoj1316】树上的询问 树的点分治+STL-set
题目描述 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 输入 第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行 ...
随机推荐
- 判断scrollView的滑动方向
第一种方式: float lastContentOffset; - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { las ...
- bitmap自己项目中处理遇到的问题
String path = "图片路径";Bitmap bitmap = BitmapFactory.decodeFile(path);安卓处理图片都是Bitmap,然后取到图片的 ...
- Hdoj 2509 Be the Winner
Diciption Let's consider m apples divided into n groups. Each group contains no more than 100 apples ...
- 从 Git Gui 管理的Repository(库) 提交更改到 Bonobo服务器管理的Repository(库)
要提交更改到Bonobo服务器管理的某个Repository(库),必须先得在Bonobo服务器上有此Repository(库)——简直就是废话.那么怎么才能这个Repository(库)变出来呢?其 ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- 获取安装后Apache、MySQL、Nginx、PHP编译时参数
# cat /usr/local/apache2/build/config.nice //获取Apache编译时的参数 #!/bin/sh # #Created by configure & ...
- 【温故知新】——HTML基础重要知识点复习
前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 一.HTML快速入门(重点) 1.HTML概述 1.什么是HTML HTML : Hyper Text Markup Lan ...
- mongodb文档的CRUD
本章会介绍对数据库移入或者移出数据的基本操作 向集合添加文档 从集合删除文档 更新现有的文档 为这些操作选择合适的安全级别 添加删除数据库 添加数据库 :use foo 如果存在foo 就use ...
- android图片素材參考
hpi:通常是大图像素是:480x800 (640*960)宽比长大致为0.6左右 一般240dpi. 小图的像素依据实际来. xhdi:一般大图像素是: 640x1136 (72 ...
- 1、C++的的升级
1.C语言的缺点 (1)重用性差 (2)维护性差 2.C++ 从面向世界的需求出发来设计我们的程序, 3. 使用增强 A. ; i < ; i++) { } C语言编译器的话,会报错,因为C语言 ...