分析:每次找重心可以发现最多n层,每层复杂度是O(nlogn) 总体时间复杂度是O(nlog^2n)

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <cmath>
  11. #include <stdlib.h>
  12. using namespace std;
  13. typedef long long LL;
  14. const int N=1e4+;
  15. int head[N],tot,n,k,ret,cnt,sz[N],rt,p,dis[N],mx[N];
  16. bool vis[N];
  17. struct Edge{
  18. int v,w,next;
  19. }edge[N<<];
  20. void add(int u,int v,int w){
  21. edge[tot].v=v;
  22. edge[tot].w=w;
  23. edge[tot].next=head[u];
  24. head[u]=tot++;
  25. }
  26. void getsz(int u,int f){
  27. sz[u]=;mx[u]=;
  28. for(int i=head[u];~i;i=edge[i].next){
  29. int v=edge[i].v;if(v==f||vis[v])continue;
  30. getsz(v,u);sz[u]+=sz[v];
  31. if(sz[v]>mx[u])mx[u]=sz[v];
  32. }
  33. }
  34. void getroot(int tp,int u,int f){
  35. mx[u]=max(mx[u],sz[tp]-sz[u]);
  36. if(mx[u]<cnt)cnt=mx[u],rt=u;
  37. for(int i=head[u];~i;i=edge[i].next){
  38. int v=edge[i].v;if(v==f||vis[v])continue;
  39. getroot(tp,v,u);
  40. }
  41. }
  42. void getdis(int u,int f,int d){
  43. dis[++p]=d;
  44. for(int i=head[u];~i;i=edge[i].next){
  45. int v=edge[i].v;if(v==f||vis[v])continue;
  46. getdis(v,u,d+edge[i].w);
  47. }
  48. }
  49. int cal(int u,int d){
  50. int ans=;
  51. p=;
  52. getdis(u,-,d);
  53. sort(dis+,dis++p);
  54. for(int i=,j=p;i<j;++i){
  55. while(dis[i]+dis[j]>k&&i<j)--j;
  56. ans+=j-i;
  57. }
  58. return ans;
  59. }
  60. void dfs(int u){
  61. cnt=n;getsz(u,-);getroot(u,u,-);
  62. ret+=cal(rt,);vis[rt]=true;
  63. for(int i=head[rt];~i;i=edge[i].next){
  64. int v=edge[i].v;if(vis[v])continue;
  65. ret-=cal(v,edge[i].w);
  66. dfs(v);
  67. }
  68.  
  69. }
  70. int main(){
  71. while(~scanf("%d%d",&n,&k),n&&k){
  72. for(int i=;i<=n;++i)head[i]=-,vis[i]=false;
  73. ret=tot=;
  74. for(int i=;i<n;++i){
  75. int u,v,w;scanf("%d%d%d",&u,&v,&w);
  76. add(u,v,w);add(v,u,w);
  77. }
  78. dfs();
  79. printf("%d\n",ret);
  80. }
  81. return ;
  82. }

POJ1751 Tree 树分治的更多相关文章

  1. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  2. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  3. POJ 1741 Tree 树分治

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

  4. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  5. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  6. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  7. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  8. HDU 4812 D Tree 树分治

    题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...

  9. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

随机推荐

  1. 装饰器(python)

    一,装饰器定义:本质就是函数,功能是为其他函数添加新功能原则:1.不修改被装饰函数的源代码(开放封闭原则)2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式3.装饰器=高阶函数+函数嵌套+闭包高 ...

  2. TP-LINK配置公网映射

    公室里的主机获取到的地址是路由器分配的私网地址,通常是192.168.1.x,只有挂在同一个路由器底下的其它主机可以访问,路由器外面的主机是无法访问的.但是有时候我们希望把办公室内的服务器上的服务暴露 ...

  3. java常见日期格式转换以及日期的获取

    package com.test.TestBoot.SingleModel;import java.text.SimpleDateFormat;import java.util.Date;public ...

  4. Python面向对象之多态

    多态 面向对象三大特性 封装 根据职责将属性和方法封装到一个抽象的类中:--定义类的准则 继承 实现代码的重用,相同的代码不需要重复的编写:--设计类的技巧:子类针对自己的需求,编写特定的代码: 多态 ...

  5. HDU 2442

    状态压缩DP , 和HDU2280极其相似 #include <cstdio> #include <cstring> #include <iostream> usi ...

  6. POJ 3348 最直接的凸包问题

    题目大意: 给定一堆树的点,找到能组合成的最大面积,一个物体占50面积,求最多放多少物体 #include <cstdio> #include <cstring> #inclu ...

  7. js数组转换成json串 (JSON.stringify)

    例如: var giftlist[1490011777] = []; giftlist[1490011777]['id'] = 1490011777; giftlist[1490011777]['na ...

  8. ****使用ftp软件上传下载php文件时换行符丢失bug

    在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了,导致php文件 ...

  9. 洛谷——P1044 栈

    P1044 栈——卡特兰数 题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈) ...

  10. [HDU5709]Claris Loves Painting(动态开点线段树+合并)

    题意:有n(<=1e5)个点的树,每个点都有颜色(颜色可能重复),有m(<=1e5)个询问,每次询问(x,d)问在x的子树中,与x的距离不超过d的节点有多少种不同的颜色.强制要求在线. 分 ...