题目:https://vjudge.net/contest/307753#problem/B

题意:求树中路径和=k的点对是否存在

思路:点分治,这个题其实和上一题洛谷一样,只是这个数据强,我们不能直接预处理所有可能的路径长度,预处理所有路径长度复杂度 O(n^2) ,我们改为直接每次查询都分治一遍,我们只要把solve在O(n)求出来,那时间复杂度就是 O(n*logn*logn),时间上快了很多,其实等于k这个可以使用我们之前的方法。直接把不大于k的路径-小于k的路径=等于k的路径,然后搞一搞就可以了

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<vector>
  7. #define maxn 100005
  8. #define mod 1e19
  9. using namespace std;
  10. typedef long long ll;
  11. ll da;
  12. vector<pair<ll,ll> > mp[maxn];//存下图
  13. bool vis[maxn];//标记曾经使用过的重心
  14. ll maxsize[maxn],dis[maxn],d[maxn];//maxsize 当前节点的最大子树
  15. ll siz[maxn],e[maxn];// dis 到重心的距离 d 出现过的距离
  16. ll n,m,rt,sum,qe; // siz 当前节点的子树个数 e 出现的距离 rt代表当前重心
  17. void find(ll x,ll f){//找出重心
  18. siz[x]=;
  19. maxsize[x]=;
  20. for(int i=;i<mp[x].size();i++){
  21. pair<ll,ll> q=mp[x][i];
  22. if(q.first==f||vis[q.first]) continue;//vis数组标记曾经使用过的重心
  23. find(q.first,x);
  24. siz[x]+=siz[q.first];
  25. maxsize[x]=max(maxsize[x],siz[q.first]);
  26. }
  27. maxsize[x]=max(maxsize[x],sum-siz[x]);//节点总数减去当前的子树数=以当前节点为根的父亲点子树数
  28. if(maxsize[x]<maxsize[rt]){
  29. rt=x;
  30. }
  31. }
  32. void get_dis(ll x,ll f,ll len){
  33. e[++qe]=len;
  34. for(int i=;i<mp[x].size();i++){
  35. pair<ll,ll> q=mp[x][i];
  36. if(q.first==f||vis[q.first]) continue;
  37. dis[q.first]=dis[x]+len;
  38. get_dis(q.first,x,len+q.second);
  39. }
  40. }
  41. ll solve(ll x,ll len){
  42. ll ee=;
  43. qe=;
  44. dis[x]=len;
  45. get_dis(x,,len);
  46. sort(e+,e+qe+);
  47. ll l=,r=qe;
  48. while(l<r){
  49. if(e[l]+e[r]<=m){
  50. ee+=r-l;
  51. l++;
  52. }
  53. else{
  54. r--;
  55. }
  56. }
  57. return ee;
  58. }
  59. void divide(ll x){
  60. da+=solve(x,);
  61. vis[x]=;
  62. for(int i=;i<mp[x].size();i++){
  63. pair<ll,ll> q=mp[x][i];
  64. if(vis[q.first]) continue;
  65. da-=solve(q.first,q.second);
  66. sum=siz[q.first];
  67. rt=;
  68. maxsize[rt]=mod;
  69. find(q.first,x);
  70. divide(rt);
  71. }
  72. }
  73. void init(){
  74. da=;
  75. for(int i=;i<=n;i++) mp[i].clear();
  76. for(int i=;i<=n;i++) vis[i]=;
  77. }
  78. int main(){
  79. while(scanf("%lld%lld",&n,&m)!=EOF)
  80. {
  81. if(n==&&m==) break;
  82. ll a,b,c;
  83. init();
  84. char s[];
  85. for(int i=;i<n-;i++){
  86. scanf("%lld%lld%lld%s",&a,&b,&c,s);
  87. //a++;
  88. //b++;
  89. mp[a].push_back(make_pair(b,c));
  90. mp[b].push_back(make_pair(a,c));
  91. }
  92. scanf("%lld",&m);
  93. sum=n;//当前节点数
  94. rt=;
  95. maxsize[]=mod;//置初值
  96. find(,);
  97. divide(rt);
  98. printf("%lld\n",da);
  99. }
  100. }

POJ 2114 (点分治)的更多相关文章

  1. POJ 2114 点分治

    思路: 点分治 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> u ...

  2. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  3. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

  4. poj 2114 Boatherds 树的分治

    还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...

  5. Poj 2114 Boatherds(点分治)

    Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...

  6. 树分治 点分治poj 2114

    存在2点间距离==k 输出AYE 否则输出NAY #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  8. poj 2114 树的分治 可作模板

    /* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...

  9. POJ 2114 - Boatherds

    原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...

随机推荐

  1. 015-Spring Boot 定制和优化内嵌的Tomcat

    一.内嵌web容器 参看http://www.cnblogs.com/bjlhx/p/8372584.html 查看源码可知提供以下三种: 二.定制优化tomcat 2.1.配置文件配置 通过appl ...

  2. delphi idhttpsever

    http://blog.csdn.net/chelen_jak/article/details/50203809 delphi idhttpsever 2015-12-07 11:36 216人阅读  ...

  3. 关于C++ string 的神奇用法

    c++里有大部分字符的操作都在#include<cstring>这个库中,这个库的函数在考试的时候都是可以用的,这个库里包含了很多字符串操作函数,特别是string这个数据类型特别优美,它 ...

  4. Python3-问题整理

    TypeError: a bytes-like object is required, not 'str' json.decoder.JSONDecodeError: Extra data json文 ...

  5. Git004--版本回退

    Git--版本回退 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  6. Google XSS game writeup

    用过Chrome的应该知道它的XSS Auditor,它可是灭掉了不少XSS代码呢……Google对XSS是很有研究的,不然也不敢大张旗鼓的悬赏(7500刀哦亲),还开发了一个XSS小游戏 http: ...

  7. jmeter 添加header

    接口说明文档: article.fetch(通用转码服务) 通用转码服务,获取任意 url 的正文以及 title 等基本信息,仅支持 post 方法请求. 参数 参数 类型 是否必须 示例 其它说明 ...

  8. [eclipse相关] 001 - 启动+运行优化

    本随笔参考了其他博客内容,且在验证有效之下才或誊抄或摘录或加上自己经验组合而成. 参考博客: 1,http://zwd596257180.gitee.io/blog/2019/04/17/eclips ...

  9. golang简介

    GO语言是Google于2009年推出的一门新的系统编程语言 特点: 静态编译 垃圾回收 简洁的符号和语法 平坦的类型系统 基于CSP的并发模型 高效简单的工具链 丰富的标准库 为什么选择go语言 编 ...

  10. 移动端web整理 移动端问题总结,移动web遇到的那些坑

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对i ...