1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int maxn=1e6+5;
  6. struct asd{
  7. int from,to,next,val;
  8. }b[maxn];
  9. int head[maxn],tot=1;
  10. void ad(int aa,int bb,int cc){
  11. b[tot].from=aa;
  12. b[tot].to=bb;
  13. b[tot].val=cc;
  14. b[tot].next=head[aa];
  15. head[aa]=tot++;
  16. }
  17. int n,k,ans,Tsiz,Root;
  18. int siz[maxn],wt[maxn],a[maxn];
  19. bool vis[maxn];
  20. int cnt;
  21. void get_root(int now,int fa){
  22. siz[now]=1;
  23. wt[now]=0;
  24. for(int i=head[now];i!=-1;i=b[i].next){
  25. int u=b[i].to;
  26. if(vis[u] || u==fa) continue;
  27. get_root(u,now);
  28. siz[now]+=siz[u];
  29. wt[now]=max(wt[now],siz[u]);
  30. }
  31. wt[now]=max(wt[now],Tsiz-siz[now]);
  32. if(wt[Root]>wt[now]) Root=now;
  33. //printf("Root=%d\n",Root);
  34. }
  35. void dfs(int now,int fa,int d){
  36. a[++cnt]=d;
  37. //printf("%d %d\n",now,a[cnt]);
  38. for(int i=head[now];i!=-1;i=b[i].next){
  39. int u=b[i].to;
  40. //printf("%d %d\n",now,cnt);
  41. if(u!=fa && !vis[u]) dfs(u,now,d+b[i].val);
  42. }
  43. }
  44. int js(int now,int d){
  45. cnt=0;
  46. dfs(now,0,d);
  47. sort(a+1,a+cnt+1);
  48. /*for(int i=1;i<=cnt;i++){
  49. printf("%d %d\n",now,a[i]);
  50. }*/
  51. //printf("now=%d cnt=%d\n",now,cnt);
  52. int sum=0;
  53. for(int i=1,j=cnt;;i++){
  54. //printf("now=%d a[%d]=%d a[%d]=%d\n",now,i,a[i],j,a[j]);
  55. while(j && a[i]+a[j]>k) j--;
  56. if(i>j) break;
  57. sum+=j-i+1;
  58. }
  59. //printf("sum=%d\n",sum);
  60. return sum;
  61. }
  62. void dfss(int now){
  63. //printf("ans=%d\n",ans);
  64. ans+=js(now,0);
  65. vis[now]=1;
  66. for(int i=head[now];i!=-1;i=b[i].next){
  67. int u=b[i].to;
  68. if(!vis[u]){
  69. ans-=js(u,b[i].val);
  70. Root=0;
  71. Tsiz=siz[u];
  72. get_root(u,0);
  73. dfss(Root);
  74. }
  75. }
  76. }
  77. int main(){
  78. while(scanf("%d%d",&n,&k)!=EOF && n){
  79. ans=0;
  80. tot=1;
  81. memset(head,-1,sizeof(head));
  82. memset(&b,0,sizeof(struct asd));
  83. memset(vis,0,sizeof(vis));
  84. for(int i=1;i<n;i++){
  85. int aa,bb,cc;
  86. scanf("%d%d%d",&aa,&bb,&cc);
  87. ad(aa,bb,cc),ad(bb,aa,cc);
  88. }
  89. wt[0]=0x3f3f3f3f;
  90. Root=0;
  91. Tsiz=n;
  92. get_root(1,0);
  93. //printf("Root=%d\n",Root);
  94. dfss(Root);
  95. printf("%d\n",ans-n);
  96. }
  97. return 0;
  98. }

点分治模板 POJ 1741的更多相关文章

  1. 树的点分治 (poj 1741, 1655(树形dp))

    poj 1655:http://poj.org/problem?id=1655 题意: 给无根树,  找出以一节点为根,  使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...

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

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

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

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

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

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

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

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

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

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

  7. 点分治——POJ 1741

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

  8. 数据结构(树,点分治):POJ 1741 Tree

      Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). D ...

  9. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

随机推荐

  1. PAT 换个格式输出整数

    让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n,换个格式来输出任一个不超过 3 位的正整数.例如 234 应该被输出为BBSSS1234,因为它有 ...

  2. Tidyverse| XX_join :多个数据表(文件)之间的各种连接

    本文首发于公众号:“生信补给站” Tidyverse| XX_join :多个数据表(文件)之间的各种连接 前面分享了单个文件中的select列,filter行,列拆分等,实际中经常是多个数据表,综合 ...

  3. Java对象实例化的过程

    1.先为对象分配空间,并按属性类型默认初始化 ps:八种基本数据类型,按照默认方式初始化,其他数据类型默认为null 2.父类属性的初始化(包括代码块,和属性按照代码顺序进行初始化) 3.父类构造函数 ...

  4. js实现初始化调用摄像头

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  5. [每日一题2020.06.11]Codeforces Round #644 (Div. 3) H

    A-E见 : 这里 题目 我觉得很有必要把H拿出来单独发( 其实是今天懒得写题了 ) problem H 一个从 1 到 $ 2^m - 1$ 的长度为m的连续二进制序列, 删去指定的n个数, 问剩余 ...

  6. TensorFlow从0到1之TensorBoard可视化数据流图(8)

    TensorFlow 使用 TensorBoard 来提供计算图形的图形图像.这使得理解.调试和优化复杂的神经网络程序变得很方便.TensorBoard 也可以提供有关网络执行的量化指标.它读取 Te ...

  7. TensorFlow从0到1之常量、变量和占位符详解(6)

    最基本的 TensorFlow 提供了一个库来定义和执行对张量的各种数学运算.张量,可理解为一个 n 维矩阵,所有类型的数据,包括标量.矢量和矩阵等都是特殊类型的张量.   TensorFlow 支持 ...

  8. Dubbo——服务引用

    文章目录 引言 正文 服务订阅 Invoker的创建 单注册中心的Invoker创建 Dubbo直连的Invoker创建 创建代理类 引言 上一篇我们分析了服务发布的原理,可以看到默认是创建了一个Ne ...

  9. 错误 C2679二进制“没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换

    错误 C2679二进制“没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换 严重性 代码 说明 项目 文件 行 禁止显示状态错误 C2679 二进制“<<”: ...

  10. 腾讯云Redis混合存储版重磅推出,万字长文助你破解缓存难题!

    导语 | 缓存+存储的系统架构是目前常见的系统架构,缓存层负责加速访问,存储层负责存储数据.这样的架构需要业务层或者是中间件去实现缓存和存储的双写.冷热数据的交换,同时还面临着缓存失效.缓存刷脏.数据 ...