思路:

点分治

//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. ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked

    ES出现异常: failed to notify ClusterStateListenerjava.lang.IllegalStateException: environment is not loc ...

  2. 似然函数(likelihood function)

    1. 似然函数基本定义 令 X1,X2,-,Xn 为联合密度函数 f(X1,X2,-,Xn|θ),给定观测值 X1=x1,X2=x2,-,Xn=xn,关于 θ 的似然函数(likelihood fun ...

  3. USACO 2.1 Sorting a Three-Valued Sequence

    Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...

  4. ThinkPHP5+Redis单例型购物车

    <?php /** * Redis + 单例型购物车 * param $basket 存储商品信息 * param $ins 存储实例化对象 */ namespace lib; use redi ...

  5. 完整注册+JQuery验证+selert后台校验

    Java代码 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  6. ROS-单目摄像头标定

    前言:由于摄像图内部与外部的原因,生成的图像往往会发生畸变,为了避免数据源造成的误差,需要针对摄像头的参数进行标定. ros官方提供了camera_calibration软件包进行摄像头标定. 一.安 ...

  7. 压力测试工具 Tinyget

    Tinyget 压力测试工具使用方法为:命令行切换到工具所在路径下,然后输入压力命令.如:tinyget -srv:localhost -uri:/FeaturedProdu1cts.aspx -th ...

  8. python链接mysql数据库

    1.安装pycharm python3.6    pip 在windows+R  cmd where pip pip install mysql-client 如何看自已mysql-client有没有 ...

  9. Postgresql数据库的一些字符串操作函数

    今天做项目遇到客户反映了一个麻烦的事情,有一些数据存在,但就是在程序中搜索不出来,后来分析,发现问题为数据前面有几个空白字符,后来用SQL查询了一下,发现八九个数据表中,数千万条数据中有将近三百万条数 ...

  10. jvm 垃圾回收概念和算法

    1.概念 GC 中的垃圾,特指存在于内存中.不会再被使用的对象.垃圾回收有很多种算法,如引用计数法.复制算法.分代.分区的思想. 2.算法 1.引用计数法:对象被其他所引用时计数器加 1,而当引用失效 ...