点分治练习: boatherds
【题面】
求一颗树上距离为K的点对是否存在
输入数据
n,m
接下来n-1条边a,b,c描述a到b有一条长度为c的路径
接下来m行每行询问一个K
输出数据
对于每个K每行输出一个答案,存在输出“AYE”,否则输出”NAY”(不包含引号)
数据范围
对于30%的数据n<=100
对于60%的数据n<=1000,m<=50
对于100%的数据n<=10000,m<=100,c<=1000,K<=10000000
【思路】
树分治。
离线存储m个询问。分治判断该m个询问是否能够出现。
具体操作:假设当前处理第S个子树,exist[]中存储着前S-1棵子树中出现过的dis,一遍dfs得到S子树的所有dis,并判断m个询问。
时间复杂度为O(nmlogn)
别放了exist[0] <- 0和清空exist就好了QAQ
【代码】
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = 1e4+;
const int M = 1e7+; struct Edge {
int v,w;
Edge(int v=,int w=) :v(v),w(w){}
};
vector<Edge> g[N];
int n,m,k,q[N],ans[N];
int root,size,siz[N],f[N],dis[N],vis[N],tmp[N],l1,l2,exist[M]; void getroot(int u,int fa) {
siz[u]=; f[u]=; exist[]=;
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
getroot(v,u);
siz[u]+=siz[v];
if(siz[v]>f[u]) f[u]=siz[v];
}
}
f[u]=max(f[u],size-siz[u]);
if(f[u]<f[root]) root=u;
}
void dfs(int u,int fa) {
tmp[++l1]=dis[u];
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
dis[v]=dis[u]+g[u][i].w;
dfs(v,u);
}
}
}
void solve(int u) {
vis[u]=;
l1=l2=;
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
l2=l1+;
if(!vis[v]) {
dis[v]=g[u][i].w;
dfs(v,u);
FOR(i,l2,l1) FOR(j,,m)
if(q[j]>=tmp[i] && exist[q[j]-tmp[i]]) ans[j]=;
FOR(i,l2,l1) exist[tmp[i]]=;
}
}
FOR(i,,l1) exist[tmp[i]]=;
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(!vis[v]) {
root=; getroot(u,-);
size=siz[v]; solve(root);
}
}
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)){if(c=='-') c=-; c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
x*=f;
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
read(n),read(m);
int u,v,w;
FOR(i,,n-) {
read(u),read(v),read(w);
g[u].push_back(Edge(v,w));
g[v].push_back(Edge(u,w));
}
FOR(i,,m) read(q[i]);
size=n; f[]=1e9; root=;
getroot(,-); solve(root);
FOR(i,,m) if(ans[i]) puts("AYE"); else puts("NAY");
return ;
}
点分治练习: boatherds的更多相关文章
- Poj 2114 Boatherds(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- poj 2114 Boatherds 树的分治
还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- 【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics
三道题都很类似.给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using nam ...
- poj2114 Boatherds
Description Boatherds Inc. is a sailing company operating in the country of Trabantustan and offerin ...
- 树链剖分-点的分治(dis[i]+dis[j]==k的点对数量)
poj2114 Boatherds Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1195 Accepted: 387 ...
- 【poj2114】点分治(离线)
boatherds 2s 64M by czy 求一颗树上距离为K的点对是否存在 输入数据 n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行每行询问一个K 输出数据 对于每 ...
随机推荐
- Asp.net Mvc HTTP 404。
asp.net mvc 设置完起始页的时候会出现以下Error 此错误的原因是在MVC中设置完起始页 会改变 的 值 从而使服务器找不到 相对应的路径 解决方案: 通过 路由设置,解决web ...
- 关于Navicat导入MySQL数据库遇到的一些问题
今天本想将之前的一个数据库easy.sql用图形化工具Navicat导入的,开始是用“运行SQL文件”导入,结果是“queries successfully”啥的,去查是否导表成功,一看并没有. 结果 ...
- 不同版本PHP之间cURL的区别(-经验之谈)
之前在做一个采集的工具,实现采集回来的文章,图片保存起来.文章内容是保存在数据库,图片是先需要上传到图片服务器,再返回图片地址,替换掉文章的图片地址. 问题来了:都能成功采集都东西,但是,本地测试是正 ...
- yii 缓存探究
1.在配置文件中 //在权威指南上是'cache' 其实可以根据不同的缓存组件起不同的名称 //memcache缓存 'memcache' => array( 'class' => 'sy ...
- c#反射机制判断同一个类的两个实例的值是否完全一样
; i < properties1.Length; i++) { string s = properties1[i].DeclaringTyp ...
- 如何得到django中form表单里的复选框(多选框)的值( MultipleChoiceField )
直接写代码吧 CHECKBOX_CHOICES = ( ('Value1','Value1'), ('Value2','Value2'), ) class EditProfileForm(ModelF ...
- 转载:Source Insight查看ARM汇编源程序 && 高亮显示程序 && Source Insight打开project窗口出错
(1)Source Insight查看ARM汇编源程序.做ARM嵌入式开发时,有时得整汇编代码,但在SIS里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都 ...
- Rewrite Path in Asp.Net MVC Project
// Change the current path so that the Routing handler can correctly interpret // the request, then ...
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...
- uva 1400 - "Ray, Pass me the dishes!"
又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...