#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
struct asd{
int from,to,next,val;
}b[maxn];
int head[maxn],tot=1;
void ad(int aa,int bb,int cc){
b[tot].from=aa;
b[tot].to=bb;
b[tot].val=cc;
b[tot].next=head[aa];
head[aa]=tot++;
}
int n,k,ans,Tsiz,Root;
int siz[maxn],wt[maxn],a[maxn];
bool vis[maxn];
int cnt;
void get_root(int now,int fa){
siz[now]=1;
wt[now]=0;
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
if(vis[u] || u==fa) continue;
get_root(u,now);
siz[now]+=siz[u];
wt[now]=max(wt[now],siz[u]);
}
wt[now]=max(wt[now],Tsiz-siz[now]);
if(wt[Root]>wt[now]) Root=now;
//printf("Root=%d\n",Root);
}
void dfs(int now,int fa,int d){
a[++cnt]=d;
//printf("%d %d\n",now,a[cnt]);
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
//printf("%d %d\n",now,cnt);
if(u!=fa && !vis[u]) dfs(u,now,d+b[i].val);
}
}
int js(int now,int d){
cnt=0;
dfs(now,0,d);
sort(a+1,a+cnt+1);
/*for(int i=1;i<=cnt;i++){
printf("%d %d\n",now,a[i]);
}*/
//printf("now=%d cnt=%d\n",now,cnt);
int sum=0;
for(int i=1,j=cnt;;i++){
//printf("now=%d a[%d]=%d a[%d]=%d\n",now,i,a[i],j,a[j]);
while(j && a[i]+a[j]>k) j--;
if(i>j) break;
sum+=j-i+1;
}
//printf("sum=%d\n",sum);
return sum;
}
void dfss(int now){
//printf("ans=%d\n",ans);
ans+=js(now,0);
vis[now]=1;
for(int i=head[now];i!=-1;i=b[i].next){
int u=b[i].to;
if(!vis[u]){
ans-=js(u,b[i].val);
Root=0;
Tsiz=siz[u];
get_root(u,0);
dfss(Root);
}
}
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF && n){
ans=0;
tot=1;
memset(head,-1,sizeof(head));
memset(&b,0,sizeof(struct asd));
memset(vis,0,sizeof(vis));
for(int i=1;i<n;i++){
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
ad(aa,bb,cc),ad(bb,aa,cc);
}
wt[0]=0x3f3f3f3f;
Root=0;
Tsiz=n;
get_root(1,0);
//printf("Root=%d\n",Root);
dfss(Root);
printf("%d\n",ans-n);
}
return 0;
}

点分治模板 POJ 1741的更多相关文章

  1. 树的点分治 (poj 1741, 1655(树形dp))

    poj 1655:http://poj.org/problem?id=1655 题意: 给无根树,  找出以一节点为根,  使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...

  2. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  3. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  4. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  5. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  6. poj 1741 楼教主男人八题之中的一个:树分治

    http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...

  7. 点分治——POJ 1741

    写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...

  8. 数据结构(树,点分治):POJ 1741 Tree

      Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). D ...

  9. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

随机推荐

  1. 11.经典O(n²)比较型排序算法

    关注公号「码哥字节」修炼技术内功心法,完整代码可跳转 GitHub:https://github.com/UniqueDong/algorithms.git 摘要:排序算法提多了,很多甚至连名字你都没 ...

  2. 做一个有价值的seoer

    什么是SEO就不用在解释了吧,一般来看这篇文章的都是老鸟了,基础的术语解释也啥意义. 从早期的cms建站,到现在的批量镜像站群等操作,似乎搜索引擎现在都下了重手,前几年还好用的方式现在已经不再是那么理 ...

  3. el-table 表格加图片、加音频、加序号、多级动态表头

    elemnet-ui组件库大家应该不陌生,在展示多条结构类似的数据方面,el-table可谓扛把子,不仅可以把数据展示的整齐,还支持排序.筛选或其他自定义操作.那么,除了上述的基本功能外,你还遇到过哪 ...

  4. 记一次@ResponseBody注解返回中文乱码的问题

    Bug场景 前端AJax $.ajax({ url: '$!{request.getContextPath()}/XX/save.do', type: 'post', dataType: 'json' ...

  5. 纯CSS如何实现状态圆点

    .circle { display: inline-block; width: 12px; height: 12px; } .circle:after { content: ''; margin: 3 ...

  6. 曹工说Redis源码(8)--面试时,redis 内存淘汰总被问,但是总答不好

    文章导航 Redis源码系列的初衷,是帮助我们更好地理解Redis,更懂Redis,而怎么才能懂,光看是不够的,建议跟着下面的这一篇,把环境搭建起来,后续可以自己阅读源码,或者跟着我这边一起阅读.由于 ...

  7. Linux系统使用Nmon监控及分析系统性能

    一.下载nmon(1)查看sever的内核版本:     命令:lsb_release -a执行结果:LSB Version:    :base-4.0-amd64:base-4.0-noarch:c ...

  8. Rust 数据类型

    Rust中的每个值都具有特定的数据类型. 基础类型: 整数,浮点数,布尔值和字符 i8,i16,i32,i64,i64,i128,isize, u8,u16,u32,u64,u64,u128,usiz ...

  9. fork,vfork和clone底层实现

    分类: LINUX2011-10-13 09:33 1116人阅读 评论(0) 收藏 举报 structdstsignalthreadnulldomain fork,vfork,clone都是linu ...

  10. spring boot admin项目的集成和开发

    Spring Boot Admin是一个Github上的一个开源项目,它在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot 应用程序的一 ...