传送门:http://poj.org/problem?id=1741

题意:

求树上两点间路径长度小于k的点对个数

题解:

参考资料

守望的淀粉质略解:https://www.luogu.org/blog/user9012/dian-fen-zhi-lve-xie

粉红兔大佬的淀粉质:https://www.cnblogs.com/PinkRabbit/p/8593080.html

算法步骤

  1. 计算重心位置
  2. 计算答案
  3. 分治子问题继续求解(1~3)

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
int n, k, Ans;struct EDGE {int v, nxt, w;
} edge[maxn << 1];
int head[maxn], tot;
void add_edge(int u, int v, int w) {edge[tot].v = v;edge[tot].w = w;edge[tot].nxt = head[u];head[u] = tot++;
}
bool vis[10001];
int Root, Tsiz, siz[10001], wt[10001];
int arr[10001], cnt;void GetRoot(int u, int f) {siz[u] = 1; wt[u] = 0;for(int i = head[u]; i != -1; i = edge[i].nxt) {int v = edge[i].v;if(v != f && !vis[v]) {GetRoot(v, u);siz[u] += siz[v];wt[u] = max(wt[u], siz[v]);}}wt[u] = max(wt[u], Tsiz - siz[u]);if(wt[Root] > wt[u]) Root = u;
}void Dfs(int u, int D, int f) {arr[++cnt] = D;for(int i = head[u]; i != -1; i = edge[i].nxt) {int v = edge[i].v;if(v != f && !vis[v]) {Dfs(v, D + edge[i].w, u);}}
}int calc(int u, int D) {cnt = 0; Dfs(u, D, 0); int l = 1, r = cnt, sum = 0;sort(arr + 1, arr + cnt + 1);for(;; ++l) {while(r && arr[l] + arr[r] > k) --r;if(r < l) break;sum += r - l + 1;}return sum;
}void DFS(int u) {Ans += calc(u, 0); vis[u] = 1;for(int i = head[u]; i != -1; i = edge[i].nxt) {int v = edge[i].v;if(!vis[v]) {Ans -= calc(v, edge[i].w);Root = 0, Tsiz = siz[v], GetRoot(v, 0);DFS(Root);}}
}int main() {
#ifndef ONLINE_JUDGEFIN
#endifwhile(~scanf("%d%d", &n, &k) && n && k) {tot = Ans = 0;memset(vis, 0, sizeof(vis));memset(head, -1, sizeof(head));for(int i = 2, u, v, w; i <= n; i++) {scanf("%d%d%d", &u, &v, &w);add_edge(u, v, w);add_edge(v, u, w);}wt[0] = INF;Tsiz = n;GetRoot(1, 0);DFS(Root);printf("%d\n", Ans - n);}return 0;
}

转载于:https://www.cnblogs.com/buerdepepeqi/p/11172718.html

POJ1741 点分治模板相关推荐

  1. POJ - 1741 Tree(点分治模板题)

    题目链接:点击查看 题目大意:给出一棵 n 个节点的树,现在定义 dis( x , y ) 为点 x 和点 y 之间的路径长度,现在问 dis ( x , y ) <= k 的点对有多少 题目分 ...

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

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

  3. The 2019 ACM-ICPC Shannxi J. And And And (点分治模板题)

    J. And And And A tree is a connected graph without cycles. You are given a rooted tree with nn nodes ...

  4. POJ1741 Tree(点分治)

    嘟嘟嘟 没错,这一道最经典的点分治模板题. 题意:求树上两点间距离\(\leqslant k\)的点对个数. 点分治这东西我好早就听说了,然后一两个月前也学了一下,不过只是刷了个模板,没往深处学. 对 ...

  5. 树分治树链剖分相关题目讨论

    预备知识 树分治,树链剖分 poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs.c ...

  6. P4178 Tree (点分治)

    题意: 给定一棵 n 个节点的树,每条边有边权,求出树上两点距离小于等于 k 的点对数量. 题解: 根点分治模板提很相似,只不过这个题目让你去维护小于等于k距离点的个数,这个时候我们还是要用到桶的思想 ...

  7. POJ 1741 Tree(点分治)

    题意 一棵 \(n\) 个节点的树,求两点间距离不超过 \(k\) 的点对对数. 思路 点分治模板题,点分治一般用来解决树上的路径问题,核心在于找出重心,算经过重心的合法路径,然后以重心把树劈成若干个 ...

  8. Bzoj 2152: 聪聪可可(点分治)

    2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Submit: 2683 Solved: 1420 [Submit][Status][Discuss ...

  9. P2634 [国家集训队]聪聪可可(点分治做法)

    P2634 [国家集训队]聪聪可可 题意: 一颗n个点的树,问其中两点之间的边上数的和加起来是3的倍数的点对有多少个? 输出这样的点对所占比例 题解: 因为是求三的倍数,我们num来记录%3=0,1, ...

  10. 2018CCF-CSP 5.二次求和(点分治)

    5.二次求和 暴力 首先观察询问,树上链u→vu\to vu→v点权加,显然可以用树上差分LOJ dfs序4 O(1)O(1)O(1)完成此操作,然后考虑对这些权值对答案的影响? 设经过某点uuu符合 ...

最新文章

  1. 2021华为软件精英挑战赛(附赠线下判题器链接)——经历
  2. 包含绑定变量的sql进行调优需注意一点
  3. 业务模块化打造单体和分布式部署同步支持方案
  4. 第三次预作业20155231邵煜楠:虚拟机上的Linux学习
  5. java jedis_Java操作Redis之Jedis用法详解
  6. 【转载】水木算法讨论题
  7. android的cantext对象,安卓Android Context类实例详解
  8. 服务器不能安装exe文件,云服务器安装exe文件
  9. 点云质量评估_点云配准中常用的评价指标
  10. 关联性——组内相关系数
  11. 计算机培训心得ppt展示,ppt制作学习心得
  12. 线性代数笔记5.3实对称矩阵的对角化
  13. “0元送设计”如何换来70亿营收?尚品宅配的新零售数字化增长研究
  14. ucloud的弹性计算
  15. win7需要计算机管理员权限,解决方案:Win7安装软件需要管理员权限解决方案
  16. SQLServer之修改PRIMARY KEY
  17. DropDownMenu下拉菜单
  18. vue中引用高德地图根据经纬度计算两地距离
  19. 【Hinton大神新作】Dynamic Routing Between Capsules阅读笔记
  20. 小学生python游戏编程arcade----excel调用

热门文章

  1. 「代码随想录」121. 买卖股票的最佳时机【贪心】【动态规划】力扣/leetcode详解
  2. poj 2387 Til the Cows Come Home spfa基础题,入门,我的第一个
  3. Illustrator 教程,如何在 Illustrator 文档中缩放和平移?
  4. Mac新手使用技巧——AirDrop
  5. 「Photoshop 入门教程」如何在Mac版 Photoshop 中打开图像?
  6. 如何在苹果Mac中使用聚焦搜索 NTFS 格式磁盘?
  7. OmniPlan Pro 4 for Mac(项目流程管理)
  8. 如何在Mac上管理辅助功能键盘的选项?
  9. 解决IIShalders错误,解决IISmodules错误
  10. playbook管理配置文件