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
题目分析 : 给定一棵树,以及树上边的关系大小,问你又多少对点的距离是小于等于所给定的 d 的
思路分析 : 树上点分治的板子题,首先寻找树的重心,以重心为根结点,寻求所有符合题意要求的点对,但是这样计算会算出一些不符合题目的点对,在减去即可,此时当遍历到一个新的结点时,此时的情况又可以当成最初的情况,找重心的时候要注意,对它的子树来说,总的结点数是小于 n 的!!!
      最后的复杂度是n logn logn    其中每次快排是nlogn 而递归的深度为logn
代码示例 :
const int maxn = 1e4+5;
const int inf = 0x3f3f3f3f;
#define ll long long int n, m;
struct node
{
int to, cost;
node(int _to = 0, int _cost = 0):to(_to), cost(_cost){}
};
vector<node>ve[maxn];
int root;
int size[maxn], mx[maxn]; // size表示每个结点所连的结点数, mx表示对每个根结点所连的最大结点子树有多少的结点
int balance;
bool done[maxn];
int ans = 0;
int numm; // 表示结点总数 void getroot(int x, int fa){
size[x] = 1, mx[x] = 0; for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
if (to == fa || done[to]) continue;
getroot(to, x);
size[x] += size[to];
mx[x] = max(mx[x], size[to]);
}
mx[x] = max(mx[x], numm-size[x]); // 对子树在寻找子树的重心的过程中,子树的总结点数是会变小的
if (mx[x] < balance) {balance = mx[x], root = x;}
} int cnt = 0;
int dep[maxn];
void dfssize(int x, int fa, int d){
dep[cnt++] = d; for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
int cost = ve[x][i].cost;
if (to == fa || done[to]) continue;
dfssize(to, x, d+cost);
}
} int cal(int x, int d){
cnt = 0;
dfssize(x, x, d);
sort(dep, dep+cnt);
int l = 0, r = cnt-1;
int sum = 0; while(l < r){
if (dep[l]+dep[r] <= m){
sum += r-l;
l++;
}
else r--;
}
//printf("sum = %d \n", sum);
//system("pause");
return sum;
} void dfs(int x){
done[x] = true;
ans += cal(x, 0); for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
int cost = ve[x][i].cost; if (done[to]) continue;
ans -= cal(to, cost);
balance = inf;
numm = size[to]; // 这里是重点,因为这个地方一直T,还以为写的代码有问题
getroot(to, to);
//printf("root = %d\n", root);
dfs(root);
}
} int a, b, w;
int main() {
while(scanf("%d%d", &n, &m) && n+m){
for(int i = 0; i <= 10000; i++) ve[i].clear();
memset(done, false, sizeof(done));
for(int i = 1; i < n; i++){
scanf("%d%d%d", &a, &b, &w);
ve[a].push_back(node(b, w));
ve[b].push_back(node(a, w));
}
ans = 0;
balance = inf;
numm = n;
getroot(1, 1);
//printf("root = %d\n", root);
dfs(root);
printf("%d\n", ans);
}
return 0;
}

树上点分治 poj 1741的更多相关文章

  1. 点分治——POJ 1741

    写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...

  2. 树分治 poj 1741

    n k n个节点的一棵树 k是距离 求树上有几对点距离<=k; #include<stdio.h> #include<string.h> #include<algo ...

  3. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  4. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

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

                                                                                                 POJ 174 ...

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

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

  7. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

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

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

  9. poj 1741 楼教主男人八题之中的一个:树分治

    http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...

随机推荐

  1. linux scull 的设计

    编写驱动的第一步是定义驱动将要提供给用户程序的能力(机制).因为我们的"设备"是计算 机内存的一部分, 我们可自由做我们想做的事情. 它可以是一个顺序的或者随机存取的设 备, 一个 ...

  2. P1034 台阶问题一

    题目描述 有 \(N\) 级的台阶,你一开始在底部,每次可以向上迈最多2级台阶(最少1级),问到达第 \(N\) 级台阶有多少种不同方式. 输入格式 一个正整数 \(N(\le 20)\) . 输出格 ...

  3. 2019-9-2-C#同步方法转异步

    title author date CreateTime categories C#同步方法转异步 lindexi 2019-09-02 12:57:37 +0800 2018-2-13 17:23: ...

  4. linux 在 open 时复制设备

    管理存取控制的另一个技术是创建设备的不同的私有拷贝, 根据打开它的进程. 明显地, 这只当设备没有绑定到一个硬件实体时有可能; scull 是一个这样的"软件"设备 的例子. /d ...

  5. vue-learning:40 - Vuex - 第一篇:概念和基本使用

    vuex 第一篇 目录 vuex概念 state / mapState getter / mapGetter mutation / mapMutation action / mapAction mod ...

  6. Linux 内核 /sys/class类

    我们在本章中要考察最后的设备模型概念是类.一个类是一个设备的高级视图, 它抽象出 低级的实现细节. 驱动可以见到一个 SCSI 磁盘或者一个 ATA 磁盘, 在类的级别, 它们都 是磁盘. 类允许用户 ...

  7. PowerShell 通过 WMI 获取系统信息

    本文告诉大家如何通过 WMI 使用 Win32_OperatingSystem 获取设备厂商 通过下面代码可以获取 系统版本和系统是专业版还是教育版 Get-WmiObject Win32_Opera ...

  8. monaco-editor使用

    monaco-editor是一款非常好用的web代码编辑器,那么如何把他加到自己的项目中呢. 1.下载插件 npm install monaco-editor@0.8.3 2.初始化编辑器值 < ...

  9. Qt中动态链接库的使用

    转自: http://www.qtcn.org/bbs/read.php?tid=14719 现在有些软件有自动升级功能,有些就是下载新的DLL文件,替换原来的动态链接库.MFC好象也有类似机制 Qt ...

  10. Python4_数据库相关操作

    ====================================================== 参考链接: PyCharm IDE 链接sqlite.建表.添加.查询数据:https:/ ...