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. PAT T1024 Currency Exchange Centers

    krustral算法求最少结点数的最小生成树,用优先队列实时排序,优先选择已经被选中的中心~ #include<bits/stdc++.h> using namespace std; ; ...

  2. [转]利用 Commons-Fileupload 实现文件上传

    转载 Java Web开发人员可以使用Apache文件上传组件来接收浏览器上传的文件,该组件由多个类共同组成,但是,对于使用该组件来编写文件上传功能的Java Web开发人员来说,只需要了解和使用其中 ...

  3. SQL模糊匹配之正则表达式

    −      方括号[ ]:指定一个字符.字符串.匹配他们中的任意一个. −      示例1:查询用户名以J或者以M开头的用户信息 −      SELECT user_name FROM ecs_ ...

  4. C++结构体struct与C语⾔结构体和C++引⽤&与传值的区别

    写再最前面:摘录于柳神的笔记: (1)定义好结构体 stu 之后,使⽤这个结构体类型的时候,C语⾔需要写关键字 struct ,⽽C++⾥⾯可以省 略不写: (2)这个引⽤符号 & 要和C语⾔ ...

  5. 03.Scala编程实战

    Scala编程实战 1.    课程目标 1.1.  目标:使用Akka实现一个简易版的spark通信框架 2.    项目概述 2.1.   需求 Hivesql----------> sel ...

  6. eclipse导入项目上面有个红叉X

    问题: 今天突然想到一个以前做过的项目,想导入到新环境中,发现不管咱整都一个红叉X, 我记得以前好像碰到过类似的问题,当时三秒搞定,谁知道时间一长,三分钟没有搞定. 还是记录下: 一般导入项目出错,肯 ...

  7. Integer a = 200,b=200比较详解

    题记:前几天面试Java基础给来了个面试题Integer a=200,b=200;System.out.println(a==b);当时回答是false,后来面试官又来了一个Integer a=100 ...

  8. 5.使用Redis+Flask维护动态Cookies池

    1.为什么要用Cookies池? 网站需要登录才可爬取,例如新浪微博 爬取过程中如果频率过高会导致封号 需要维护多个账号的Cookies池实现大规模爬取 2.Cookies池的要求 自动登录更新 定时 ...

  9. Django 学习 之ORM聚合查询分组查询与F查询与Q查询

    一.聚合查询和分组查询 1.聚合查询aggregate 关于数据表的数据请见上一篇:Django 学习 之ORM多表操作(点我) aggregate(*args, **kwargs),只对一个组进行聚 ...

  10. SpringBoot项目中自定义注解的使用

    1.定义注解接口 @Documented @Retention(RUNTIME) @Target(METHOD) public @interface MyLog {    String value() ...