思路:

点分治

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 10005
int n,k,xx,yy,first[N],next[N*2],v[N*2],w[N*2],tot;
int f[N],size[N],vis[N],d[N],deep[N],root,sum,ans;
void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void getroot(int x,int fa){
size[x]=1,f[x]=0;
for(int i=first[x];~i;i=next[i])if(!vis[v[i]]&&v[i]!=fa)
getroot(v[i],x),size[x]+=size[v[i]],f[x]=max(f[x],size[v[i]]);
f[x]=max(f[x],sum-size[x]);
if(f[x]<f[root])root=x;
}
void getdeep(int x,int fa){
deep[++deep[0]]=d[x];
for(int i=first[x];~i;i=next[i])if(v[i]!=fa&&!vis[v[i]]&&d[x]+w[i]<=k)
d[v[i]]=d[x]+w[i],getdeep(v[i],x);
}
int calc(int x,int now){
d[x]=now,deep[0]=0,getdeep(x,0);
sort(deep+1,deep+deep[0]+1);
int t=0,l=1,r=deep[0];
while(l<r)
if(deep[l]+deep[r]<k)l++;
else if(deep[l]+deep[r]>k)r--;
else{
if(deep[l]==deep[r]){t+=(r-l+1)*(r-l)/2;break;}
int st=l,ed=r;
while(deep[st]==deep[l])st++;
while(deep[ed]==deep[r])ed--;
t+=(st-l)*(r-ed);
l=st,r=ed;
}
return t;
}
void work(int x){
ans+=calc(root,0),vis[x]=1;
for(int i=first[x];~i;i=next[i])if(!vis[v[i]])
ans-=calc(v[i],w[i]),
sum=size[v[i]],root=0,
getroot(v[i],x),work(root);
}
int read(){
int x=0;char p=getchar();
while(p<'0'||p>'9')p=getchar();
while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
return x;
}
int main(){
while(scanf("%d",&n)&&n){
memset(first,-1,sizeof(first));tot=0;
for(int i=1;i<=n;i++)
while(xx=read())
yy=read(),add(i,xx,yy),add(xx,i,yy);
while(k=read()){
memset(vis,0,sizeof(vis));
f[0]=10005,root=ans=0,sum=n,getroot(1,0);
work(root);
puts(ans?"AYE":"NAY");
}puts(".");
}
}

POJ 2114 点分治的更多相关文章

  1. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  2. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

  3. poj 2114 Boatherds 树的分治

    还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...

  4. Poj 2114 Boatherds(点分治)

    Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...

  5. POJ 2114 (点分治)

    题目:https://vjudge.net/contest/307753#problem/B 题意:求树中路径和=k的点对是否存在 思路:点分治,这个题其实和上一题洛谷一样,只是这个数据强,我们不能直 ...

  6. 树分治 点分治poj 2114

    存在2点间距离==k 输出AYE 否则输出NAY #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  8. poj 2114 树的分治 可作模板

    /* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...

  9. POJ 2114 - Boatherds

    原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...

随机推荐

  1. word2010无法显示endnote x7插件及破解endnote x7

    最近本人由于要写文章需要使用endnotex7,相比于mendeley和noteexpress,文献管理和引用我喜欢endnote x7,阅读喜欢mendeley.可是由于之前用的正版30天到期了,破 ...

  2. Ubuntu18.04修改Hostname

    1. 设置新的hostnamesudo hostnamectl set-hostname newNameHere 2. 修改配置文件使hostname可以保存编辑这个文件: /etc/cloud/cl ...

  3. radio判断是否为空

    isMarital = $('[name="isMarital"]:checked').val(); isMarital == null //当radio选择为空的时候 isMar ...

  4. struts2学习之基础笔记6

    第十一章 Struts 2的国际化 1 国际化简介 http协议,request_locale值 Locale类àà封装类request_locale值       ResourcesBandleàà ...

  5. input 框输入数字相关

    input框限制只能输入正整数,逻辑与和或运算 有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: &l ...

  6. LIst和map的遍历

    1. public static void main(String[] args) { // ArrayList类实现一个可增长的动态数组 List<String> list = new ...

  7. USB接口

     总结: 1.电脑的usb接口是usb母接口,u盘接口是usb公接口 2.usb otg指的是不需要电脑作为中转站接口,例如如果买一个micro 转otg接口即可将手机直接接u盘 3.方形usb口是u ...

  8. rem 自适应布局 bootstrap 移动端适配

    移动端适配用:rem 自使用布局用:bootstrap

  9. Java开发就业形势和面试技巧

    如果从软件编程的就业来讲,如果你现在不懂架构,那么找到一份好工作还是比较难的,但是这里面有两点需要注意: 传统软件公司,这类公司还会使用最为原始的开发技术(SSH),但是这样的传统软件公司的招聘量已经 ...

  10. React高级指南

    高级指南 1.深入JSX: 从本质上讲,JSX 只是为 React.createElement(component, props, ...children) 函数提供的语法糖. 因为 JSX 被编译为 ...