Tree

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 23380   Accepted: 7748

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

Source

 
 //2017-08-09
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; const int N = ;
int n, k, MAX, root, cnt, answer; //链式前向星
int head[N], tot;
struct Edge{
int next, to, w;
}edge[N<<]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} int size[N];//size[i]表示以i为根的子树的大小,包括i。
int maxson[N];//maxson[i]表示以i为根的子树的最大儿子的大小。
int dis[N];//dis[i]表示i到根的距离。
bool vis[N];//vis[i]用来标记i点是否被删除。 void init(int n){
answer = ;
tot = ;
memset(vis, , sizeof(vis));
memset(head, -, sizeof(head));
} //计算出子树的大小
void dfs_size(int u, int fa){
size[u] = ;
maxson[u] = ;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(vis[v] || v == fa)continue;
dfs_size(v, u);
size[u] += size[v];
if(size[v] > maxson[u])
maxson[u] = size[v];
}
} //找子树的重心。最大子树最小的点即为树的重心。
void dfs_root(int r, int u, int fa){
if(size[r] - size[u] > maxson[u])//size[r]-size[u]为u上面的树的尺寸
maxson[u] = size[r] - size[u];
if(maxson[u] < MAX){
MAX = maxson[u];
root = u;
}
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(vis[v] || v == fa)continue;
dfs_root(r, v, u);
}
} //计算出子树中每个点距离重心的距离
void dfs_dis(int u, int d, int fa){
dis[cnt++] = d;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(vis[v] || v == fa)continue;
dfs_dis(v, d+edge[i].w, u);
}
} //计算出以u为根的子树中距离和小于k的点对数
int cal(int u, int d){
int ans = ;
cnt = ;
dfs_dis(u, d, );
sort(dis, dis+cnt);
for(int i = , j = cnt-; i < j; i++){
while(dis[i]+dis[j] > k && i < j)//双指针
j--;
ans += j-i;
}
return ans;
} //分治,找到树的重心,分为经过重心的点对和不经过重心的点对。
void solve(int u){
MAX = n;
dfs_size(u, );
dfs_root(u, u, );
answer += cal(root, );
vis[root] = ;
for(int i = head[root]; ~i; i = edge[i].next){
int v = edge[i].to;
if(vis[v])continue;
answer -= cal(v, edge[i].w);
solve(v);
}
} int main()
{
//freopen("dataIn.txt", "r", stdin);
while(scanf("%d%d", &n, &k)!=EOF){
if(!n && !k)break;
int u, v, w;
init(n);
for(int i = ; i < n-; i++){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
solve();
printf("%d\n", answer);
} return ;
}

POJ1741(SummerTrainingDay08-G 树的点分治)的更多相关文章

  1. 【poj1741】Tree 树的点分治

    题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  2. POJ1741 Tree(树的点分治)

    题目给一棵边带权的树,统计路径长度<=k的点对数. 楼教主男人八题之一,分治算法在树上的应用. 一开始看论文看不懂,以为重心和距离那些是一遍预处理得来的..感觉上不敢想每棵子树都求一遍重心和距离 ...

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

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

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

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

  5. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

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

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

  7. hdu_5314_Happy King(树的点分治)

    题目链接:hdu_5314_Happy King 题意: 给出一颗n个结点的树,点上有权值: 求点对(x,y)满足x!=y且x到y的路径上最大值与最小值的差<=D: 题解: 还是树的点分治,在统 ...

  8. 【CF576E】Painting Edges 线段树按时间分治+并查集

    [CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...

  9. 【bzoj3697】采药人的路径 树的点分治

    题目描述 给出一棵 $n$ 个点的树,每条边的边权为1或0.求有多少点对 $(i,j)$ ,使得:$i$ 到 $j$ 的简单路径上存在点 $k$ (异于 $i$ 和 $j$ ),使得 $i$ 到 $k ...

  10. 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治

    题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...

随机推荐

  1. 使用wget命令爬取整站

    快速上手(整个bootstrap网页全被你抓取下来了~_~) wget -c -r -npH -k -nv http://www.baidu.com 参数说明 -c:断点续传 -r:递归下载 -np: ...

  2. nginx root&alias 文件路径配置

    nginx 指定文件路径有两种方式 root 和 alias,root 与 alias 主要区别在于 nginx 如何解释 location 后面的 uri,这会使两者分别以不同的方式将请求映射到服务 ...

  3. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  4. day 74 vue 2 axios数据请求 以及组件的学习

    前情提要:   vue 学习二: 一: 通过axios实现数据请求 1:json数据语法 json数据对象类似于JavaScript中的对象,但是它的键对应的值里面是没有函数方法的,值可以是普通变量, ...

  5. maven build的常用生命周期

    常用的maven build goals: validate - validate the project is correct and all necessary information is av ...

  6. 安卓APP简单后端的搭建

    写在前面: 此教程没有用到后端框架.只是单纯用servlet做一个例子,如果是学框架可以不用往下看了 本文适合哪些人:懂java的,会写android单机程序,懂得用HTTPClient等发送请求解析 ...

  7. JavaSE-java8-谓词复合的用法

    谓词接口包括三个方法: negate. and 和 or,让你可以重用已有的Predicate来创建更复杂的谓词 一.比如可以用negate方法来返回一个Predicate非 public class ...

  8. ring3下的IAT HOOK

    标 题: [原创]ring3下的IAT HOOK作 者: hostzhen时 间: 2013-03-28,11:30:53链 接: http://bbs.pediy.com/showthread.ph ...

  9. text_edit 未定义解决

    找到文件:admin\controller\setting $data['heading_title'] = $this->language->get('heading_title'); ...

  10. JavaScript -- Window-弹出窗口

    -----033-Window-弹出窗口.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=& ...