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. css颜色值设置方式有哪些?以及如何随机一个颜色?

    网页中颜色的使用方式有一下几种 1.颜色名称 ,如red  black white 2.十六进制颜色,网页中常用,每两位代表红绿蓝的值的比例,  如 #ffffff白色   #000000黑色 3.r ...

  2. 一张图搞懂容器所有操作 - 每天5分钟玩转 Docker 容器技术(26)

    前面我们已经讨论了容器的各种操作,对容器的生命周期有了大致的理解,下面这张状态机很好地总结了容器各种状态之间是如何转换的. 如果掌握了前面的知识,要看懂这张图应该不难.不过有两点还是需要补充一下: 可 ...

  3. Iterator invalidation(迭代器失效)

    一.vector 所有读操作.swap.std::swap:都不会引起迭代器失效... clear.operator=.assign:都会引起全部变量迭代器失效 reserve.shrink_to_f ...

  4. SpringMVC的form:form表单的使用

    为什么要使用SpringMVC的form:form表单,有两个原因:一是可以更加快捷的完成表单的开发,比如会替你做好数据类型装换等本来需要你自己动手的工作.其次就是能够更加方便的实现表单回显. 首先要 ...

  5. 9个常用iptables配置实例

    iptables命令可用于配置Linux的包过滤规则,常用于实现防火墙.NAT.咋一看iptables的配置很复杂,掌握规律后,其实用iptables完成指定任务并不难,下面我们通过具体实例,学习ip ...

  6. 【MFC】利用双缓冲和随机函数rand()实现蒲公英飞舞

    原始日期:2014-05-29 22:44 这几天有些懒,几乎没怎么学MFC了,好容易有个题目:用双缓冲实现蒲公英飞舞,想来想去也没想到好方法,索性动手开始 写了 ,这一写,得,出来了,呵呵,无意中产 ...

  7. 原始js调用 选中事件

    curRadio.get(0).checked=true;//原始js调用 选中事件,curRadio是radio单个对象

  8. docker - 启动container时出现 [warning] : ipv4 forwarding is disabled. networking will not work

    起因 今天在一台新的centos宿主机上安装docker,由于关闭了iptables,在此之后启动container的时候会出现警告: WARNING: IPv4 forwarding is disa ...

  9. vue 基础-->进阶 教程(2): 指令、组件

    第二章 建议学习时间4小时  课程共3章 前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零 ...

  10. [leetcode-523-Continuous Subarray Sum]

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...