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. 改写画质、突破性能, Unity 全面升级!

    技术变革,时代更迭.从<神庙逃亡>.<暗影之枪>等主流手游到独立联网的大型游戏,从绚丽多彩的影视动画到具备极致体验的运输建筑制造行业,从传统的2D 到立体3D 乃至沉浸式的VR ...

  2. win10常用快捷键总结

    前言: 很多快捷键在不同版本系统基本相同的,但是今天推送的这篇文章更多的介绍 win10快捷键,微软也是大力推广 旗舰系统 win10 ,所以大家提前升级,提前学习还是有必要的.毕竟2020年微软会放 ...

  3. php的弱类型比较

    1.==和=== ==为弱相等,也就是说12=="12" --> true,而且12=="12cdf" --> true,只取字符串中开头的整数部分 ...

  4. Spark入门:第2节 Spark集群安装:1 - 3;第3节 Spark HA高可用部署:1 - 2

    三. Spark集群安装 3.1 下载spark安装包 下载地址spark官网:http://spark.apache.org/downloads.html 这里我们使用 spark-2.1.3-bi ...

  5. ES6新语法概览

    简介 ES6是JavaScript语言的新一代标准,加入了一些新的功能和语法,正式发布于2015年6月,亦称ES2015:该标准由ECMA(欧洲计算机制造联合会)的第39号技术专家委员会(TC39)制 ...

  6. Pytorch model saving and loading 模型保存和读取

    It is really useful to save and reload the model and its parameters during or after training in deep ...

  7. MyEclipse JSP页面中文字符不能保存

      问题: 就是写HTML+JSP代码时有些中文,保存时提示sava could not be completed. Reason: some characters cannot be mapped ...

  8. mongoDB 分片集群常用指令

    1.帮助指令:sh.help(),查看有哪些指令及其含义和使用方法. mongos> sh.help() sh.addShard( host )                       se ...

  9. pycharm安装报错Non-zero exit co?

    pycharm安装第三方库时报错Non-zero exit co? 原因:版本找不到 解决思路: 1.用pip install pandas报错,尝试用python -m pip install -- ...

  10. SVM数学原理推导

    //2019.08.17 #支撑向量机SVM(Support Vector Machine)1.支撑向量机SVM是一种非常重要和广泛的机器学习算法,它的算法出发点是尽可能找到最优的决策边界,使得模型的 ...