Tree
Time Limit: 1000MS   Memory Limit: 30000K
     

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

Source

树分治模板题;

分治算法在树的路径问题中的应用

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e4+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct is
{
int v,w,nex;
}edge[N<<];
int head[N],edg;
void add(int u,int v,int w)
{
edge[++edg]=(is){v,w,head[u]};head[u]=edg;
} int son[N],msi[N],d[N];
int vis[N],deep[N];
int n,K,ans,root,sum;
void groot(int u,int fa)
{
son[u]=,msi[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(v==fa||vis[v])continue;
groot(v,u);
son[u]+=son[v];
msi[u]=max(msi[u],son[v]);
}
msi[u]=max(msi[u],sum-son[u]);
if(msi[u]<msi[root])root=u;
}
void gdeep(int x,int fa)
{
deep[++deep[]]=d[x];
for(int i=head[x];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fa||vis[v])continue;
d[v]=d[x]+w;
gdeep(v,x);
}
} int rootans(int x,int base)
{
d[x]=base;deep[]=;
gdeep(x,);
sort(deep+,deep++deep[]);
int ans=,l=,r=deep[];
while(l<r)
{
if(deep[l]+deep[r]<=K)
{
ans+=r-l;
l++;
}
else r--;
}
return ans;
}
void dfs(int u)
{
ans+=rootans(u,);
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(vis[v])continue;
ans-=rootans(v,w);
root=;sum=son[v];
groot(v,u);
dfs(root);
}
}
void init(int n)
{
memset(head,-,sizeof(head));
memset(msi,,sizeof(msi));
memset(son,,sizeof(son));
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
memset(deep,,sizeof(deep));
sum=n;root=;edg=;
msi[]=inf;
ans=;
}
int main()
{
while(~scanf("%d%d",&n,&K))
{
if(n==&&K==)break;
init(n);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
groot(,);
dfs(root);
printf("%d\n",ans);
}
return ;
}

poj 1744 tree 树分治的更多相关文章

  1. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  2. POJ 1741 Tree 树分治

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

  3. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  4. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  5. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  6. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  7. POJ 1741 Tree ——点分治

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

  8. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  9. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

随机推荐

  1. python--表达式(运算表达式)

    运算表达式 python 的表达式包括:算术运算符,赋值运算符,比较运算符,成员运算符 算术运算符 运算符 描述 + 加 - 两个对象相加 - 减 - 得到负数或是一个数减去另一个数 * 乘 - 两个 ...

  2. [转载]css3的一个控制背景的属性,background-size可以缩放大小啦

    background-size需要两个值,它的类型可以是像素(px).百分比(%)或是auto,还可以是cover和contain.第一个值为背景图的width,另外一个值用于指定背景图上的heigh ...

  3. java中BufferedImage类的用法

    1. BufferedImage是Image的一个子类,Image和BufferedImage的主要作用就是将一副图片加载到内存中. BufferedImage生成的图片在内存里有一个图像缓冲区,利用 ...

  4. 有道词典翻译(携带请求头和post参数请求)

    一.静态爬取页面信息 有道翻译网址:http://fanyi.youdao.com/ 在翻译中输入python 找到接口和请求的方式 参数是From Data类型 需要把参数数据转换为字典, 复制粘贴 ...

  5. RTSP 与 RTMP 协议 (转)

    源: RTMP协议与RTSP协议比较 RTSP 与 RTMP 协议 RTSP Spec中文版(1-11) RTSP协议 流媒体之rtsp篇 H264视频传输.编解码----RTSP协议

  6. No symbol "xxx" in current context解决

    当gdb的版本低于相应的gcc版本的时候,就会出现debug的时候出现No symbol "*" in current context或者The address where a.o ...

  7. hibernate validator自定义校验注解以及基于服务(服务组)的校验

    hibernate validator是Bean Validation 1.1 (JSR 349) Reference Implementation,其广泛的应用在mvc的参数校验中,尤其是使用服务端 ...

  8. 《web前端设计基础——HTML5、CSS3、JavaScript》 张树明版 简答题简单整理

    web前端设计基础——HTML5.CSS3.JavaScript 简答题整理 第一章 (1)解释一下名词的含义:IP地址.URL.域名   iP定义了如何连入因特网,以及数据如何在主机间传输的标准. ...

  9. 查看Linux服务器是否是虚拟机

    dmidecode -s system-product-name 物理机: [root@swnode1]# dmidecode -s system-product-name I840-GS 虚拟机: ...

  10. bzoj 1420 Discrete Root - 原根 - exgcd - BSGS

    题目传送门 戳我来传送 题目大意 给定$k, p, a$,求$x^{k}\equiv a \pmod{p}$在模$p$意义下的所有根. 考虑模$p$下的某个原根$g$. 那么$x  = g^{ind_ ...