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 ;
}

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

[bzoj 1468][poj 1741]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(点分治点对<=k)

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

  4. POJ 1741 Tree ——点分治

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

  5. [poj 1741]Tree 点分治

    题意 求树上距离不超过k的点对数,边权<=1000 题解     点分治.     点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...

  6. POJ - 1741 - Tree - 点分治 模板

    POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...

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

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

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

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

  9. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

随机推荐

  1. a标签点击之后有个虚线边框,怎么去掉

    1.行内处理方式1 <a hidefocus="true" href="#"></a> 2.行内处理方式2,让a标签获得焦点就失去焦点, ...

  2. linux--centos服务器配置

    配置Centos服务器   1. 安装centos 安装时注意分区留下一个/data区给网站内容存储. 2. 配置网络连接 默认centos网络连接不是开机启动的,需要配置. vi /etc/sysc ...

  3. [github项目]基于百度地图二次开发实现的车辆监管(包含车辆定位、车辆图片和方向控制,电子围栏,图形绘制等功能)前端实现(不包含后端实现)

    前言:基于百度地图javascript版本开发,百度地图中所用的key已承诺仅用于测试,不用于商业用途 注:本文所有代码可以到github上进行下载,github地址:http://map.eguid ...

  4. sqlmap详细使用 [精简]

    1. 基础用法: 一下./sqlmap.py 在kali和backtrack中使用sqlmap的时候,直接用:sqlmap ./sqlmap.py -u “注入地址” -v 1 –dbs   // 列 ...

  5. Jquery操作Table

    Jquery 操作 Html Table 是很方便的,这里对表格的基本操作进行一下简单的总结. 首先建立一个通用的表格css 和一个 表格Table: table { border-collapse: ...

  6. B树和B+树的总结

    B树 为什么要B树 磁盘中有两个机械运动的部分,分别是盘片旋转和磁臂移动.盘片旋转就是我们市面上所提到的多少转每分钟,而磁盘移动则是在盘片旋转到指定位置以后,移动磁臂后开始进行数据的读写.那么这就存在 ...

  7. 关于CSS样式优先级学习心得

    1.未重复时候,只要有都有格式显示 2.重复时,看权值: 权值:标签 1 <类10< ID 100 PS:(*权值 > 继承(表格属性一般无法继承,有些浏览器也不支持表格继承父标签) ...

  8. 宿主机共享文件夹给不同Linux虚拟机的方法

    一.Windows/Linux宿主机共享文件夹给VMWare中的Linux虚拟机 1.能安装vmware tools1)在vmware的ubuntu中安装vmware tools2)在vmware中开 ...

  9. dd的用法

    1.生成一个空的,大小为1G的文件(有洞的文件)$ dd if=/dev/zero of=winxp.img bs=1k seek=1024k count=1 2.读软盘,并以16进制保存到文件中#d ...

  10. Python原理 -- 内存管理

    语言的内存管理是语言设计的一个重要方面. 它是决定语言性能的重要因素. 无论是 c语言 的手工管理, 还是 Java 的垃圾回收, 都成为语言最重要的特种. 以下以 python 为例, 说明一门动态 ...