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. 循环语句(while语句和do...while语句)

    1.while语句:如果条件成立,就继续循环,直到条件不成立为止.格式如下: while (条件) {               循环体(语句或语句块) } 2.do…while语句:如果条件成立, ...

  2. 重新理解《务实创业》---HHR计划--以太一堂第三课

    第一节:开始学习 1,面对创业和融资,我们应该如何从底层,理解他们的本质呢?(实事求是) 2,假设你现在要出来融资,通常你需要告诉投资人三件事:我的市场空间很大,我的用户需求很疼,我的商业模式能跑通. ...

  3. nginx sendfile 相关知识

    https://blog.csdn.net/wm_1991/article/details/51916027

  4. 【PAT甲级】1022 Digital Library (30 分)(模拟)

    题意: 输入一个正整数N(<=10000),接下来输入N组数据,ID,书名,作者,关键词,出版社,出版年份. 然后输入一个正整数M(<=1000),接下来输入查询的数据,递增输出ID,若没 ...

  5. mysql mvcc 的理解

    mvcc 全称 multiple version concurrency control 多版本并发控制,是数据库领域比较常用的一种非锁并发技术. mysql 的innodb中,在RR.RC级别会使用 ...

  6. R-CNN算法中NMS的具体做法

    假设有20类,2000个建议框,最后输出向量维数2000*20,则每列对应一类,一行是各个建议框的得分,NMS算法步骤如下: ① 对2000×20维矩阵中每列按从大到小进行排序: ② 从每列最大的得分 ...

  7. IOS 错误原因

    当xcode提示以下错误时,很可能的原因是由于ViewController中的View在Controller中连接了outlet,然后又删除了Controller中对应的属性,导致xcode找不到这个 ...

  8. Python中安装框架如何换源以及升级

    想安装tornado框架,但总是有奇怪错误,如下: 如果按照默认的下载源,就会死活不成功,出现 Traceback (most recent call last): File "e:\pyt ...

  9. 十六 Spring的JDBC模版入门,默认连接池

    Spring是EE开发一站式框架,有EE开发的每层的解决方案,Spring对持久层也提供了解决方案:ORM模块和JDBC的模版

  10. 十一 队列 Queue

    队列:  一种先进先出的数据结构  FIFO 数组队列的实现: