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

题意:

有一棵树,求满足x到y距离小于等于m的无序点对(x,y)的个数。

思路:

第一次写树上的乱搞系列。。。

对于此题,树上的分治。愚见如下。

假设有如图:

以root为根,点对(x,y)有四种情况。

  • 先说第四种,第四种是D-B-A-root-I..-X(经过root),dis(D,X)>m,无效,此处不考虑了。
  • 第一种:A-root-I,经过root,而且dis(A,I)<=m,有效,累加。

  • 第二种:B-A-C,不经过root,但是dis(B,root)+dis(root,C)<=m,有效,累加。但是在当A为根时,点对(B,C)距离满足要求,肯定还会被算一次,所以此时需要减去儿子为根的某些部分。
  • 第三种:D-B-A-C,不经过root,而且dis(D,root)+dis(root,C)>m,所以此处不累加。但是dis(B,A)+dis(A,C)<=m,当root下降到某点时会累加,这也是第二种需要减去的原因,防止累加两次。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 20010
using namespace std;
int m,head[N],to[N],len[N],next[N],cnt,sz[N];
int deep[N],root,vis[N],son[N],sn,d[N],tot,ans;
//son[]最大儿子树,sz[]子树。
//vis[]表示是否做为根使用过。
void add(int x,int y,int z)
{
to[++cnt]=y,len[cnt]=z,next[cnt]=head[x],head[x]=cnt;
}
void getroot(int u,int fa)
{
son[u]=,sz[u]=;
for(int i=head[u];i;i=next[i])
if(to[i]!=fa&&!vis[to[i]]){
getroot(to[i],u);sz[u]+=sz[to[i]];
son[u]=max(son[u],sz[to[i]]);
}
son[u]=max(son[u],sn-sz[u]);
if(son[root]>son[u]) root=u;
}
void getdeep(int x,int fa)
{
d[++tot]=deep[x];
for(in
t i=head[x];i;i=next[i])
if(to[i]!=fa&&!vis[to[i]])
deep[to[i]]=deep[x]+len[i],getdeep(to[i],x);
}
int calc(int x)
{
tot=,getdeep(x,),sort(d+,d+tot+);
int i=,j=tot,sum=;
while(i<j) { //保证了不重复
if(d[i]+d[j]<=m) sum+=j-i,i++ ; //d[]+d[]<m的个数。利用了双指针的思想
else j--;
}
return sum;
}
void dfs(int u)
{
deep[u]=;vis[u]=;ans+=calc(u);
for(int i=head[u];i;i=next[i])
if(!vis[to[i]]){
deep[to[i]]=len[i];ans-=calc(to[i]);//居然是抽屉原理。。。 细思极恐
sn=sz[to[i]];root=;getroot(to[i],);dfs(root);
}
}
int main()
{
int n,i,x,y,z;
while(~scanf("%d%d",&n,&m)&&(n||m)){
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
cnt=; ans=;
for(i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z)
add(x,y,z);add(y,x,z);
}
root=; son[]=0x7fffffff; sn=n;
getroot(,); dfs(root);
printf("%d\n",ans);
}
return ;
}

POJ1741 Tree(树的点分治基础题)的更多相关文章

  1. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

  2. 【poj1741】Tree 树的点分治

    题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  3. hdu 4812 D Tree(树的点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total ...

  4. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  5. [poj1741][tree] (树/点分治)

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

  6. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

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

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

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

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

  9. POJ1741(SummerTrainingDay08-G 树的点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23380   Accepted: 7748 Description ...

随机推荐

  1. 解决Command "laravoole" is not defined.

    版权声明:本文为博主原创文章,未经博主允许不得转载.  GitHub地址:https://github.com/garveen/laravoole 先来执行正常的安装流程: 安装 要开始,将larav ...

  2. lua(仿类)

    Account = { balance = } function Account:deposit(v) self.balance = self.balance + v end function Acc ...

  3. 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数

    [BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...

  4. 【python】-- 基于Django的杂货铺

    Django的杂货铺 此篇文章保存基于Django而实现的各种小功能示例 1.验证码 + Session 这个是在前端图片验证码的生成,再配合Session进行后端校验的功能示例 import ran ...

  5. Linux中源码包安装

    1.准备环境 a.因为是编译安装,所以需要安装gcc编译器 b.下载源码包 2.注意事项 a.源代码保存位置 /usr/local/src/ b.软件安装位置 /usr/local/ c.如何确定安装 ...

  6. Call method 的使用

    SAP学习日志---Call method 的使用 以及常见错误 转载▼   可以通过以下方法 call method 1. 进入全局类中 找到方法,拖到程序中 2. 使用pattern 中的 AAB ...

  7. QT5使用Webkti

    Qt 5.3 使用原来的QT4.8.4项目时QWebView .QWebFrame等类无法编译通过. 出现原因:QWebView .QWebFrame.QWebPage.QWebInspector等这 ...

  8. Shell传参的多种方式

    Shell 传参的多种方式 使用$1 $2 这种类似占位符的方式 # 命令行调用 start.sh 8080 9090 # 脚本中获取 port1=$1 # 8080 port2=$2 # 9090 ...

  9. [转]Mysql命令

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  10. python 3 递归调用与二分法

    递归调用与二分法 1.递归调用 递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身. 示例: def age(n): if n == 1: return 18 # 结束条件 return a ...