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. vscode中使用markdown

    vscode中使用markdown vscode 是微软推出一款轻量级的文本编辑工具,类似于sublime,由于其拥有丰富的插件,安装使用也非常简单,所以深受广大程序员的喜爱. markdown 是一 ...

  2. java控件之树形结构JTree

    import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; im ...

  3. 提高java编程质量 - (五)switch语句break不能忘以及default不同位置的用法

    先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...

  4. DNS全局负载均衡(GSLB)基本原理

    原理 DNS全局负载均衡通过智能DNS解析来实现,通常在不同的地区设立多个数据中心,每个数据中心又使用多个运营商的线路.目前很多DNS服务商都提供了智能DNS服务,智能DNS通常是利用各运营商分省IP ...

  5. JavaScript中的数组

    数组 (1).数组的定义 数组是值的有序集合 javascript数组是无类型的:数组元素可以是任意类型,并且同一个数组的不同元素也可能有不同的类型. 每个值叫做一个元素,而每个元素在数组中有一个位置 ...

  6. 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

    平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...

  7. C语言中指针和数组的区别

    看<C专家编程>一书,看到数组与指针并不相同一章,遂做了一段测试: 代码: #include <stdio.h> #include <stdlib.h> int m ...

  8. IE6不支持position:fixed的解决方法

    解决IE6不支持position:fixed的方法,非常简单,具体调用请参考下面: /*让position:fixed在IE6下可用! */ .fixed-top /* 头部固定 */{positio ...

  9. win7开启telnet客户端

  10. jrebel配置热部署参数

    jrebel配置热部署参数: -noverify -agentpath:D:/jrebel/lib/jrebel64.dll -Drebel.dirs=E:/workspace/item/src/ma ...