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

思路:点分治板子题,提供两个blog
https://blog.csdn.net/qq_39553725/article/details/77542223https://www.cnblogs.com/bztMinamoto/p/9489473.html
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e4+; struct Node {
int v, next, val;
} Nodes[maxm*]; int head[maxm], cnt, siz[maxm], mxson[maxm], dis[maxm], root, mxsum, rootsum, points, n, k;
bool vis[maxm];
LL ans; void init() {
ans = ; cnt = ;
memset(vis, false, sizeof(vis)), memset(head, , sizeof(head));
} void addedge(int u, int v, int val) {
Nodes[++cnt].v = v;
Nodes[cnt].val = val;
Nodes[cnt].next = head[u];
head[u] = cnt;
} void getroot(int u, int fa) {
mxson[u] = , siz[u] = ;
for(int i = head[u]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(v == fa || vis[v]) continue;
getroot(v, u);
siz[u] += siz[v];
mxson[u] = max(mxson[u], siz[v]);
}
mxson[u] = max(mxson[u], rootsum - siz[u]);
if(mxson[u] < mxsum) {
root = u, mxsum = mxson[u];
}
} void getdist(int u, int fa, int dist) {
dis[++points] = dist;
for(int i = head[u]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(v == fa || vis[v]) continue;
getdist(v, u, dist+Nodes[i].val);
}
} int solve(int rt, int val) {
points = ;
getdist(rt, , val);
int l = , r = points, t = ;
sort(dis+, dis++points);
while(l <= r) {
if(dis[l] + dis[r] <= k) {
t += r-l;
l++;
} else
r--;
}
return t;
} void Divide(int rt) {
ans += solve(rt, );
vis[rt] = true;
for(int i = head[rt]; i; i = Nodes[i].next) {
int v = Nodes[i].v;
if(vis[v]) continue;
ans -= solve(v, Nodes[i].val);
rootsum = siz[v];
root = ; mxsum = 0x3f3f3f3f;
getroot(v, );
Divide(root);
}
} int main() {
ios::sync_with_stdio(false), cin.tie();
while(cin >> n >> k && n+k) {
init();
int u, v, val;
for(int i = ; i < n-; ++i) {
cin >> u >> v >> val;
addedge(u, v, val), addedge(v, u, val);
}
mxsum = 0x3f3f3f3f; rootsum = n;
getroot(,);
Divide(root);
cout << ans << "\n";
}
return ;
}

												

Day8 - F - Tree POJ - 1741的更多相关文章

  1. Tree POJ - 1741【树分治】【一句话说清思路】

    因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...

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

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

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

                                                                                                 POJ 174 ...

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

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

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

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

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

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

  7. 点分治——POJ 1741

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

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

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

  9. [atcoder contest 010] F - Tree Game

    [atcoder contest 010] F - Tree Game Time limit : 2sec / Memory limit : 256MB Score : 1600 points Pro ...

随机推荐

  1. Pycharm创建一个Django项目

    1.创建项目 如果本地没有安装与所选python版本对应Django版本,pycharm会自动下载相应的版本: 创建后运行项目,默认页面为http://127.0.0.1:8000/,打开后: 出现上 ...

  2. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  3. leetcode #980 不同路径||| (java)

    在二维网格 grid 上,有 4 种类型的方格: 1 表示起始方格.且只有一个起始方格.2 表示结束方格,且只有一个结束方格.0 表示我们可以走过的空方格.-1 表示我们无法跨越的障碍.返回在四个方向 ...

  4. C++打开文件夹

    https://bbs.csdn.net/topics/392055617?page=1   见2楼 system("start \"\" \"文件夹路径\&q ...

  5. nacos集群配置

    一.    环境准备 Nacos 依赖 java环境来运行.如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用: 64 bit OS,支持 Lin ...

  6. CSP2019 括号树

    Description: 给定括号树,每个节点都是 ( 或 ) ,定义节点的权值为根到该节点的简单路径所构成的括号序列中不同合法子串的个数(子串需要连续,子串所在的位置不同即为不同.)与节点编号的乘积 ...

  7. MyEclipse和Eclipse中常用的快捷键

    ##########################快捷键分类速查##########################     *******常用类********[Ctrl+O]   显示类中方法和 ...

  8. CS231n -Assignments 1 Q1 and Q2

    前言 最近在youtube 上学习CS231n的课程,并尝试完成Assgnments,收获很多,这里记录下过程和结果以及过程中遇到的问题,我并不是只是完成需要补充的代码段,对于自己不熟悉的没用过的库函 ...

  9. 132、Java面向对象之static关键字四(定义一个数学的加法操作)

    01.代码如下: package TIANPAN; class MyMath { // 数学操作类,类中没有属性 public static int add(int x, int y) { // 只是 ...

  10. redis之Hash类型常用方法总结

    redis之Hash类型常用方法总结 格式: 存--HMGET key field [field ...] 取--HMGET key field [field ...] M:表示能取多个值,many ...