题面

思路

本蒟蒻第一次学点分治,正遇模板题,留个模板代码

\(Code\)

#include<cstdio>
#include<algorithm>
using namespace std; const int N = 1e4 + 5;
int len , d[N] , cnt , n , use[N] , h[N] , tot , size , son[N] , siz[N] , rt , ans; struct edge{
int to , nxt , w;
}e[2 * N]; inline void add(int x , int y , int z)
{
e[++tot].to = y;
e[tot].w = z;
e[tot].nxt = h[x];
h[x] = tot;
} inline void getrt(int x , int fa)
{
son[x] = 0 , siz[x] = 1;
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (use[v] || v == fa) continue;
getrt(v , x);
siz[x] += siz[v];
son[x] = max(son[x] , siz[v]);
}
son[x] = max(son[x] , size - siz[x]);
rt = son[x] < son[rt] ? x : rt;
} inline void getdis(int x , int fa , int dis)
{
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (use[v] || v == fa) continue;
d[++cnt] = dis + e[i].w;
getdis(v , x , d[cnt]);
}
} inline int binary(int l , int r , int x)
{
int mid , res = 0;
while (l <= r)
{
mid = (l + r) >> 1;
if (d[mid] <= x) res = mid , l = mid + 1;
else r = mid - 1;
}
return res;
} inline int getans(int x , int dis)
{
d[cnt = 1] = dis;
getdis(x , 0 , dis);
sort(d + 1 , d + cnt + 1);
int l = 1 , r , res = 0;
while (len - d[l] >= d[l] && l < cnt)
{
r = binary(l + 1 , cnt , len - d[l]);
if (r > l) res += r - l;
l++;
}
return res;
} inline void divide(int x)
{
use[x] = 1 , ans += getans(x , 0);
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (use[v]) continue;
ans -= getans(v , e[i].w);
size = siz[v] , rt = 0;
getrt(v , x) , divide(rt);
}
} int main()
{
scanf("%d%d" , &n , &len);
int u , v , w;
for(register int i = 1; i < n; i++)
{
scanf("%d%d%d" , &u , &v , &w);
add(u , v , w) , add(v , u , w);
}
size = n;
son[0] = 2e9;
getrt(1 , 0) , divide(rt);
printf("%d" , ans);
}

JZOJ 1166. 树中点对距离的更多相关文章

  1. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  2. hihoCoder week11 树中的最长路

    题目链接: https://hihocoder.com/contest/hiho11/problem/1 求树中节点对 距离最远的长度 #include <bits/stdc++.h> u ...

  3. Leetcode——863.二叉树中所有距离为 K 的结点

    给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K . 返回到目标结点 target 距离为 K 的所有结点的值的列表. 答案可以以任何顺序返回. 示例 1: 输 ...

  4. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  5. Prim算法和Kruskal算法(图论中的最小生成树算法)

    最小生成树在一个图中可以有多个,但是如果一个图中边的权值互不相同的话,那么最小生成树只可能存在一个,用反证法很容易就证明出来了. 当然最小生成树也是一个图中包含所有节点的权值和最低的子图. 在一个图中 ...

  6. codeforces D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

  7. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  8. HDU 5593 ZYB's Tree 树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5593 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  9. Codeforces Round #270(利用prim算法)

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...

  10. [洛谷P2024/POJ1182]食物链 - 带偏移量的并查集(2)

    Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的 ...

随机推荐

  1. SQL语句查询关键字前期数据准备

    前期数据准备 create table emp( id int primary key auto_increment, name varchar(20) not null, gender enum(' ...

  2. Day29:StringBuilder详解

    StringBuilder 1.1 StringBuilder概述 我们先对普通的String字符串对象建立进行内存分析: public class Demo{ public static void ...

  3. (四) 一文搞懂 JMM - 内存模型

    4.JMM - 内存模型 1.JMM内存模型 JMM与happen-before 1.可见性问题产生原因 下图为x86架构下CPU缓存的布局,即在一个CPU 4核下,L1.L2.L3三级缓存与主内存的 ...

  4. Linux—软件管理

    Linux 软件管理 1.软件管理简介 Redhat和Centos中软件管理是依靠软件包管理器(RPM)来实现的. RPM(Redhat Package Manager)软件包管理器提供了在linux ...

  5. Spring 6 源码编译和高效阅读源码技巧分享

    一. 前言 Spring Boot 3 RELEASE版本于 2022年11月24日 正式发布,相信已经有不少同学开始准备新版本的学习了,不过目前还不建议在实际项目中做升级,毕竟还有很多框架和中间件没 ...

  6. JS执行机制及ES6

    一.JS执行机制 JS语言有个特点是单线程,即同一时间只能做一件事.单线程就意味着,所有的任务需要排队,前一个任务结束,才会执行后一个任务,可能造成页面渲染不连贯. 为了解决这个问题,利用多核CPU的 ...

  7. Django框架路由层-无名有名分组-无名有名分组反向解析

    目录 一:路由层 1.路由匹配(错误演示) 2.路由匹配错误原因 3.路由匹配(解决方式1) 4.settings配置文件控制自动添加斜杠匹配 5.url方法第一个参数是正则表达式(正规使用url) ...

  8. 使用Google OR-Tools分析过去20年中国金融资产最佳配置组合

    前两天,在朋友圈里看到一张截至2022年Q2的金融资产历年收益图如下,图中列举了国内从2005年到2022年近20年主要的金融资产历年收益率,随产生想法分析和验证下面几个问题: 过去20年,基于怎样的 ...

  9. js修改数组中的属性名

    将数组 [{id:"1",name:"AAA"}] 修改为 ===> [{id:"1",text:"AAA",va ...

  10. 如何在C#中接受或拒绝 Excel 中的修订

    修订功能可以跟踪文档所有的修改,了解修改的过程,这对于团队协同文档编辑.审阅是非常有用的一个功能.将工作簿发送给他人审阅时,我们可以开启修订功能,共享工作簿被修改后,用户查看文档时可以选择接受或者拒绝 ...