Tree

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 25772   Accepted: 8566

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

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; struct Edge{
int to,nxt,w;
}e[N<<];
int head[N],son[N],f[N],deth[N],d[N];
bool vis[N];
int ans,tot,Root,sum,n,k; void init() {
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
ans = tot = Root = ;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
}
void getroot(int u,int pa) {
son[u] = ;f[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa||vis[v]) continue;
getroot(v,u);
son[u] += son[v];
f[u] = max(f[u],son[v]);
}
f[u] = max(f[u],sum-son[u]);
if (f[u] < f[Root]) Root = u;
}
void getdeth(int u,int pa) {
deth[++deth[]] = d[u];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==pa||vis[v]) continue;
d[v] = d[u] + w;
getdeth(v,u);
}
}
int calcc(int u,int w) {
deth[] = ;
d[u] = w;
getdeth(u,);
sort(deth+,deth+deth[]+);
int l = ,r = deth[],ret = ;
while (l < r) {
if (deth[l] + deth[r] <= k) ret += r-l,l++;
else r--;
}
return ret;
}
void work(int u) {
ans += calcc(u,);
vis[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (vis[v]) continue;
ans -= calcc(v,w);
sum = son[v];
Root = ;
getroot(v,);
work(Root);
}
}
int main() {
while (scanf("%d%d",&n,&k)!=EOF && !(n==&&k==)) {
init();
for (int a,b,c,i=; i<n; ++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
f[] = 1e9;
sum = n;
getroot(,);
work(Root);
printf("%d\n",ans);
}
return ;
}

POJ1741 Tree (点分治)的更多相关文章

  1. [POJ1741]Tree(点分治)

    树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...

  2. [poj1741]Tree(点分治+容斥原理)

    题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...

  3. [bzoj1468][poj1741]Tree[点分治]

    可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...

  4. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  5. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  6. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  7. [poj1741][tree] (树/点分治)

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

  8. POJ1741 tree 【点分治】

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25286   Accepted: 8421 Description ...

  9. POJ1741 Tree(树的点分治基础题)

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

  10. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

随机推荐

  1. jQuery学习笔记(四)

    动画效果 显示和隐藏效果 无动画效果 显示:show() 隐藏:hide() <!DOCTYPE html> <html lang="en"> <he ...

  2. js禁止浏览器页面后退功能

    js禁止浏览器页面后退功能: <script> $(function(){ ) { //防止页面后退 history.pushState(null, null, document.URL) ...

  3. 【干货】JavaScript DOM编程艺术学习笔记1-3

    从7月29号到8月8号,断断续续地看完了这本书,做了部分实践联系.总体感觉本书真的只能算是个入门,学完之后看到库的那一章才感觉是个大坑,实践中大部分应该都是用的现成的库吧,所以还要重新学习一个库,但是 ...

  4. SpringEL和资源调用

    Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言. Spring开发中经常涉及调用各种资源的情况,包含普通文件.网址.配置文件.系统环境变量等, ...

  5. office密匙

    office 2010 VYBBJ-TRJPB-QFQRF-QFT4D-H3GVB 6QFDX-PYH2G-PPYFD-C7RJM-BBKQ8 BDD3G-XM7FB-BD2HM-YK63V-VQFD ...

  6. TP5.0:的安装与配置

    在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...

  7. [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新、技术支持 or hotfix

    [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新.技术支持 or hotfix https://www.dotblogs.com.tw/mis2000lab/20 ...

  8. hihocoder 1109 堆优化的Prim算法

    题目链接:http://hihocoder.com/problemset/problem/1109 , 最小生成树 + 堆优化(优先队列). 可以用优先队列,也可以自己手动模拟堆,为了练手,我两种都试 ...

  9. 深入理解Java GC

    一.概述 GC(Carbage Collection)垃圾收集器,由JVM自动回收已死亡的对象垃圾. 这也是Java与C++等语言的主要区别之一. 二.如何确认对象已死 1. 引用计数算法 引用计数法 ...

  10. 微服务SpringCloud+Docker入门到高级实战(目录)

    第一章 课程介绍和学习路线 1.微服务架构SpringCloud课程介绍 简介:课程介绍和课程大纲讲解,讲课风格和重点内容理解技巧2.技术选型和学后水平 简介:课程所需基础和技术选型讲解,学完课程可以 ...