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

Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001). 
Define dist(u,v)=The min distance between node u and v. 
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k. 
Write a program that will count how many pairs which are valid for a given tree. 

Input

The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8
/*
poj 1741 树的点分治(入门) problem:
给一棵边带权树,问两点之间的距离小于等于K的点对有多少个 solve:
随便找的博客学习下
大致思路,对当前树先求其重心为根节点,然后找出所有点到根节点的距离. 然后就能计算出有多少的的和
<= k. 但是这两个点有可能来自同一个子树,所以在后面计算中减去,就能计算出过当前根节点的所有对.
以同样的方法计算子树的情况,递推下去就行.
重心是为了让最大子树的节点数最小,从而增加效率.
(个人理解)
hhh-2016-08-22 20:00:18
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
#define inf 0x3FFFFFFFFFFFFFFFLL
using namespace std;
const int maxn = 100010; struct node
{
int to,w;
node() {};
node(int v,int _w):to(v),w(_w) {};
}; vector<node> q[maxn];
int n,k,s[maxn],f[maxn],root,d[maxn],ans,limit;
int Size;
bool vis[maxn];
vector<int> ta; void get_root(int now,int fa)
{
int v;
s[now] = 1,f[now] = 0;
for(int i = 0;i < q[now].size();i++)
{
if( (v=q[now][i].to) == fa || vis[v])
continue;
get_root(v,now);
s[now] += s[v];
f[now] = max(f[now],s[v]);
}
f[now] = max(f[now],Size - s[now]);
if(f[now] < f[root]) root = now;
} void dfs(int now,int fa)
{
int v;
ta.push_back(d[now]);
s[now] = 1;
for(int i = 0;i < q[now].size();i++)
{
if( (v=q[now][i].to) == fa || vis[v])
continue;
d[v] = d[now] + q[now][i].w;
dfs(v,now);
s[now] += s[v];
}
} int cal(int now,int begi)
{
ta.clear(),d[now] = begi;
dfs(now,0);
sort(ta.begin(),ta.end());
int cnt = 0;
for(int l = 0,r=ta.size()-1;l<r;)
{
if(ta[l] + ta[r] <= limit) cnt += (r-l++);
else r --;
}
return cnt;
} void work(int now)
{
int v;
ans += cal(now,0);
vis[now] = 1;
for(int i = 0;i < q[now].size(); i++)
{
if(!vis[v = q[now][i].to])
{
ans -= cal(v,q[now][i].w);
f[0] = Size = s[v];
get_root(v,root = 0);
work(root);
}
}
} int main()
{
while(scanf("%d%d",&n,&limit) == 2)
{
if(!n && !limit)
break;
for(int i = 0;i <= n;i++) q[i].clear();
memset(vis,0,sizeof(vis)); int a,b,c;
for(int i = 1;i < n;i++)
{
scanf("%d%d%d",&a,&b,&c);
q[a].push_back(node(b,c));
q[b].push_back(node(a,c));
}
f[0] = Size = n;
get_root(1,root = 0);
ans = 0;
work(root);
printf("%d\n",ans);
}
return 0;
}

  

poj 1741 树的点分治(入门)的更多相关文章

  1. POJ 1741 树的点分治

    题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include & ...

  2. POJ 1741 Tree (树的点分治入门)

      Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16172   Accepted: 5272 Descripti ...

  3. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  4. poj 1741 树的分治

    思路:这题我是看 漆子超<分治算法在树的路径问题中的应用>写的. 附代码: #include<iostream> #include<cstring> #includ ...

  5. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  6. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  7. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  8. poj1741_Tree(树的点分治入门题)

    题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include< ...

  9. POJ 1741 树上 点的 分治

    题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重 ...

随机推荐

  1. 201621123060《JAVA程序设计》第三周学习总结

    1. 本周学习总结 1.1写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等. 关键词:类.方法.属性.对象.多态.继承.封装.面向对象.> 1.2 用思维导图或者Onenote或其 ...

  2. Tornado 网站demo 三

    模板 修改index.py #!/usr/bin/env Python # coding=utf-8 import tornado.web import methods.readdb as mrd c ...

  3. python 使用Nginx和uWSGI来运行Python应用

    参考:http://zmrenwu.com/post/20/ uWSGI是一个Web应用服务器,它具有应用服务器,代理,进程管理及应用监控等功能.它支持WSGI协议,同时它也支持自有的uWSGI协议, ...

  4. bzoj千题计划271:bzoj4869: [六省联考2017]相逢是问候

    http://www.lydsy.com/JudgeOnline/problem.php?id=4869 欧拉降幂+线段树,每个数最多降log次,模数就会降为1 #include<cmath&g ...

  5. Python之旅.第三章.函数3.26

    一.函数: 1.为什么要有函数?什么是函数? 1.组织结构不清晰,可读性差 2.代码冗余 3.管理维护的难度极大,扩展性 具备某一个功能的工具就是程序的中函数 事先准备工具的过程---->函数的 ...

  6. Python扩展模块——selenium的使用(定位、下载文件等)

    想全面的使用selenium可以下载<selenium 2自动化测试实战-基于Python语言>PDF的电子书看看 我使用到了简单的浏览器操作,下载文件等功能... 推荐使用firefox ...

  7. MySQL Group Relication 部署环境入门篇

      一:环境介绍   cenos 6.7 版本 数据库的版本5.7.19 二:部署规划单机多实例的部署   端口号 数据目录  group_repplicatoon 通信接口   3307 /data ...

  8. angular2 学习笔记 ( 状态管理 state management )

    更新 : 2017-12-29  ng5 移除 zone.js https://zhuanlan.zhihu.com/p/29577461 zone 的用途就是拦截游览器事件, 比如 click, a ...

  9. docker实践

    我的docker 学习笔记2   ps axf docker run -d cyf:sshd /usr/sbin -D   docker  ps docker-enter.sh 686 ps axf ...

  10. JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson

    java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...