题意:给一颗n个节点的树,每条边上有一个距离v(v<=1000)。定义d(u,v)为u到v的最小距离。给定k值,求有多少点对(u,v)使u到v的距离小于等于k。

/*
照着点分治模板敲了敲,有很多细节地方应该注意,不然很容易出错。
点分治的大概就是解决树上路径中统计某些东西的一个算法,顾名思义,肯定与递归有些关系。
简单步骤就是:①求重心 ②统计这棵树的答案 ③递归到它的子树求解
推荐一篇博客:http://www.cnblogs.com/chty/p/5912360.html
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 10010
#define inf 100000000
using namespace std;
int head[N],deep[N],d[N],vis[N],son[N],f[N],root,sum,ans,n,m;
struct node{
int to,t,pre;
};node e[N*];
void add(int i,int x,int y,int z){
e[i].to=y;
e[i].t=z;
e[i].pre=head[x];
head[x]=i;
}
void getroot(int x,int fa){
son[x]=;f[x]=;
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]||e[i].to==fa) continue;
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[x]);
if(f[x]<f[root])root=x;
}
void getdeep(int x,int fa){//求出x的子树上的节点的深度(x深度不一定为0)
deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]||e[i].to==fa) continue;
d[e[i].to]=d[x]+e[i].t;
getdeep(e[i].to,x);
}
}
int cal(int x,int v){//求出x的子树上的答案,但是对于本题目多计算了两个点在同一条子树上的情况
d[x]=v;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int l=,r=deep[],tot=;
while(l<r){
if(deep[r]+deep[l]<=m) tot+=r-l,l++;
else r--;
}
return tot;
}
void solve(int x){//求出x的子树上的答案
ans+=cal(x,);
vis[x]=;
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]) continue;
ans-=cal(e[i].to,e[i].t);
sum=son[e[i].to];
root=;
getroot(e[i].to,);
solve(root);
}
}
int main(){
while(scanf("%d%d",&n,&m)){
if(!n&&!m) break;
ans=root=;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(i*-,x,y,z);add(i*,y,x,z);
}
sum=n;f[]=inf;
getroot(,);
solve(root);
printf("%d\n",ans);
}
return ;
}

tree(poj 1741)的更多相关文章

  1. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  2. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  3. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  4. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  5. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  6. 【PAT】1043 Is It a Binary Search Tree(25 分)

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  7. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  8. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  9. 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

    [101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...

随机推荐

  1. asp.net 线程批量导入数据,ajax获取执行状态

    最近做了一个批量导入功能,长时间运行,没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能. 通过线程执行导入,并把正在执行的状态存入session,既共享执行状态,通过a ...

  2. Redis java操作客服端——jedis

    1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...

  3. AJPFX简述JavaStringBuffer方法

    以下是StringBuffer类支持的主要方法: 序号 方法描述 1 public StringBuffer append(String s)将指定的字符串追加到此字符序列. 2 public Str ...

  4. Spring数据访问1 - 数据源配置及数据库连接池的概念

    无论你要选择哪种数据访问方式,首先你都需要配置好数据源引用. Spring中配置数据源的几种方式 通过在JDBC驱动程序定义的数据源: 通过JNDI查找的数据源: 连接池的数据源: 对于即将发布到生产 ...

  5. [BZOJ2761][JLOI2011]不重复数字 暴力

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2761 直接暴力. #include<cstdio> #include<c ...

  6. 安装mysql时 make 时 提示 redeclaration of C++ built-in type ‘bool’ 错误

    安装mysql时 make 时 提示 redeclaration of C++ built-in type ‘bool’ 错误 由于gcc-c++是在./configure后进行编译的 重新./con ...

  7. ubuntu上部署windows开发的dotnet core程序

    目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目 ...

  8. COGS 827. [Tyvj Feb11] 网站计划

    输入文件:web.in   输出文件:web.out   简单对比时间限制:1 s   内存限制:128 MB 描述 Description     Tyvj的Admin--zhq同学将在寒假开始实行 ...

  9. makefile vpath变量

    在讲vpath之前,我们首先了解以下makefile文件. 在类Unix系统中,当我们使用源码编译某个软件的时候,我们会使用confiure,make,make install这三个命令,其中cofi ...

  10. php bz2扩展安装

    php bz2扩展安装 2017年09月22日 14:14:36 Cookie_1030 阅读数:1781   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...