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

又一道点分治膜版题
我果然是只会做膜版啊
还是贴上vector造图的代码吧
虽然它RE了
用数组膜你邻接表AC
我也很迷啊 好好好一个小时后的我发现自己一个小时前太sb了
居然忘了复原vector G了
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; inline int read(){
char ch;
int re=;
bool flag=;
while((ch=getchar())!='-'&&(ch<''||ch>''));
ch=='-'?flag=:re=ch-'';
while((ch=getchar())>=''&&ch<='') re=re*+ch-'';
return flag?-re:re;
} struct edge{
int to,w;
edge(int to=,int w=):
to(to),w(w){}
};
const int maxn=;
vector<edge> G[maxn];
int son[maxn],F[maxn];
int d[maxn],deep[maxn];
bool vis[maxn];
int sum,ans,root;
int n,K; inline void add_edge(int from,int to,int w){
G[from].push_back(edge(to,w));
G[to].push_back(edge(from,w));
} bool init(){
n=read();
if(!n) return ;
memset(G,,sizeof G);
memset(vis,,sizeof vis);
K=read();
for(int i=;i<n-;i++){
int from=read(),to=read(),w=read();
add_edge(from,to,w);
}
return ;
} void getroot(int x,int fa){
son[x]=;
F[x]=;
int dd=G[x].size();
for(int i=;i<dd;i++){
edge &e=G[x][i];
if(e.to!=fa&&!vis[e.to]){
getroot(e.to,x);
son[x]+=son[e.to];
F[x]=max(F[x],son[e.to]);
}
}
F[x]=max(F[x],sum-son[x]);
if(F[x]<F[root]) root=x;
} void getdeep(int x,int fa){
deep[++deep[]]=d[x];
int dd=G[x].size();
for(int i=;i<dd;i++){
edge &e=G[x][i];
if(e.to!=fa&&!vis[e.to]){
d[e.to]=d[x]+e.w;
getdeep(e.to,x);
}
}
} int calc(int x,int now){
d[x]=now;
deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int t=;
for(int l=,r=deep[];l<r;){
if(deep[l]+deep[r]<=K){
t+=r-l;
l++;
}
else r--;
}
return t;
} void work(int x){
ans+=calc(x,);
vis[x]=;
int dd=G[x].size();
for(int i=;i<dd;i++){
edge &e=G[x][i];
if(!vis[e.to]){
ans-=calc(e.to,e.w);
sum=son[e.to];
root=;
getroot(e.to,root);
work(root);
}
}
} void solve(){
sum=F[root=]=n;
ans=;
getroot(,);
work(root);
printf("%d\n",ans);
} int main(){
//freopen("temp.in","r",stdin);
while(init())
solve();
return ;
}

他说你任何为人称道的美丽  不及他第一次遇见你

最新文章

  1. linux一句话轻松提权
  2. poj 3264(线段树)
  3. pay-as-you-go
  4. iOS开发--二维码的扫描
  5. JavaScript日期集合(今日,昨日,本周一,周末 ,月初,月末)
  6. php 上传文件实例 注册账号
  7. ☆ ☆ VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程 (转)
  8. jsp学习--JSP运行原理,九大隐式对象和JSP常用标签
  9. 最新的windows xp sp3序列号(绝对可通过正版验证)
  10. js的with语句使用方法
  11. ADF_ADF Faces系列6_ADF数据可视化组件简介之建立Thematic Map Component
  12. 前台实现下载xml功能
  13. SQLite入门与分析(四)---Page Cache之事务处理(3)
  14. Codeforces 442B Andrey and Problem(贪婪)
  15. angularjs i18n
  16. loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
  17. SudaMod-81.0 / crDroidAndroid-8.1(android-8.1.0_r20)红米3 2018年5月3日更新
  18. Linux基础(四)网络设置
  19. 运行时错误 429,ACTIVEX部件不能创建对象的解决方法小结
  20. C语言入门教程-(4)常量和变量

热门文章

  1. JavaScript实现上传图片预览[js前端实现]
  2. Ionic3新特性--页面懒加载2加载其他组件
  3. CEF3 获取Cookie例子 CefCookieManager C++
  4. OkHttp基本使用
  5. sql拼接,String和Stringbuffer的问题
  6. 如何判断img标签是否有src属性
  7. SQL中创建外键约束
  8. WPF MVVM 架构 Step By Step(3)(把后台代码移到一个类中)
  9. Winform中Chart图表的简单使用
  10. phpcms V9 后台验证码图片不显示