Description

真·树,问距离不大于 \(k\) 的点对个数.

Sol

点分治.

同上.

Code

  1. /**************************************************************
  2. Problem: 1468
  3. User: BeiYu
  4. Language: C++
  5. Result: Accepted
  6. Time:832 ms
  7. Memory:3804 kb
  8. ****************************************************************/
  9.  
  10. #include<cstdio>
  11. #include<utility>
  12. #include<vector>
  13. #include<queue>
  14. #include<algorithm>
  15. #include<iostream>
  16. using namespace std;
  17.  
  18. typedef pair< int,int > pr;
  19. #define mpr make_pair
  20. #define debug(a) cout<<#a<<"="<<a<<" "
  21. const int N = 40005;
  22.  
  23. int n,m,k,rt,sz,ans;
  24. vector<pr> g[N];
  25. int s[N],f[N],b[N];
  26. vector<int> d;
  27. int q[N],h,t;
  28.  
  29. inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
  30. while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
  31. void GetRoot(int u,int fa){
  32. s[u]=1,f[u]=0;
  33. for(int i=0,v;i<g[u].size();i++) if((v=g[u][i].first)!=fa && !b[v]){
  34. GetRoot(v,u),s[u]+=s[v],f[u]=max(f[u],s[v]);
  35. }f[u]=max(f[u],sz-s[u]);if(f[u]<f[rt]) rt=u;
  36. }
  37. void GetDeep(int u,int fa,int dep){
  38. d.push_back(dep),s[u]=1;
  39. for(int i=0,v;i<g[u].size();i++) if((v=g[u][i].first)!=fa && !b[v])
  40. GetDeep(v,u,dep+g[u][i].second),s[u]+=s[v];
  41. }
  42. int Calc(int u,int w){
  43. d.clear(),GetDeep(u,u,w);
  44. sort(d.begin(),d.end());
  45. int res=0;
  46. for(int l=0,r=d.size()-1;l<r;) if(d[l]+d[r]<=k) res+=r-l,l++;else r--;
  47. return res;
  48. }
  49. void GetAns(int u){
  50. ans+=Calc(u,0);b[u]=1;
  51. for(int i=0,v;i<g[u].size();i++) if(!b[v=g[u][i].first])
  52. ans-=Calc(v,g[u][i].second),f[0]=sz=s[v],GetRoot(v,rt=0),GetAns(rt);
  53. }
  54. int main(){
  55. // freopen("in.in","r",stdin);
  56. n=in();
  57. for(int i=1,u,v,w;i<n;i++) u=in(),v=in(),w=in(),g[u].push_back(mpr(v,w)),g[v].push_back(mpr(u,w));
  58. k=in();
  59. f[rt=0]=sz=n;
  60. GetRoot(1,0);
  61. GetAns(rt);
  62. cout<<ans<<endl;
  63. return 0;
  64. }

  

BZOJ 1468: Tree的更多相关文章

  1. bzoj 1468 Tree(点分治模板)

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

  2. 【刷题】BZOJ 1468 Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  3. bzoj 1468 Tree 点分

    Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1972  Solved: 1101[Submit][Status][Discuss] Desc ...

  4. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  5. BZOJ 1468 Tree 【模板】树上点分治

    #include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...

  6. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

  7. 【BZOJ】1468: Tree(点分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1468 分治真是一门高大上的东西... 好神... 树分治最好资料是:qzc的<分治算法在树的路 ...

  8. [bzoj 1468][poj 1741]Tree [点分治]

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

  9. 【BZOJ 1468】Tree 点分治

    点分治$O(nlogn)$ 坚持到月考结束后新校就剩下我一个OIer,其他人早已停课了,老师估计懒得为我一个人开机房门,让我跟班主任说了一声,今晚就回到了老校,开始了自己都没有想到会来的这么早的停课生 ...

随机推荐

  1. linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本

    手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...

  2. How to know if file is complete on the server using FTP

    This is a very old and well-known problem. There is no way to be absolutely certain a file being wri ...

  3. ILMerge

    ILMerge http://www.microsoft.com/en-hk/download/details.aspx?id=17630 ILMerge 下载地址:http://www.micros ...

  4. Java常见的几种内存溢出及解决方法

    Java常见的几种内存溢出及解决方法[情况一]:java.lang.OutOfMemoryError:Javaheapspace:这种是java堆内存不够,一个原因是真不够(如递归的层数太多等),另一 ...

  5. linux程序调试命令strace

    strace命令用法详解: strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须 ...

  6. 微信C# SDK

    微信C# SDK # 模块功能 DLL 1 基础库 Senparc.Weixin.dll 2 微信公众号 / 微信支付 / JSSDK / 摇周边 / 等等 Senparc.Weixin.MP.dll ...

  7. Spring4学习笔记-AOP

    1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...

  8. struts2上传文件添加进度条

    给文件上传添加进度条,整了两天终于成功了. 想要添加一个上传的进度条,通过分析,应该是需要不断的去访问服务器,询问上传文件的大小.通过已上传文件的大小, 和上传文件的总长度来评估上传的进度. 实现监听 ...

  9. 30秒搭建Github Page

    如果中国每个程序员都写博客,那么中国IT届的春天就来了 原文转自我的前端博客,链接:http://www.hacke2.cn/create-github-page/ 有同学问我的网站是怎么创建的,其实 ...

  10. 【AngularJS】—— 11 指令的交互

    前面基本了解了指令的相关内容: 1 如何自定义指令 2 指令的复用 本篇看一下指令之间如何交互.学习内容来自<慕课网 指令3> 背景介绍 这例子是视频中的例子,有一个动感超人,有三种能力, ...