题面

思路

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

\(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. 关于pip3 ImportError: cannot import name 'main'的报错的原因及解决办法

    这个问题的出现大多数都是因为你用错误的方法去升级pip3导致的 先来说一下正确的升级方法: python3 -m pip install --upgrade pip 我发现升级后版本变为了 19.x, ...

  2. keras小点记录

    Keras学习小点记录 1.axis(轴) (1)解释 参考链接:https://www.zhihu.com/question/58993137 (2)测试 参考链接:http://keras-cn. ...

  3. JAVA学到方法写了一个四则运算计算器,请教一下有什么需要改进的

    package method; /* * 四则运算计算器 * */ import java.util.Scanner; public class Demo07 { public static void ...

  4. uniapp input框聚焦时软键盘弹起整个页面上滑,固定页面不让上滑问题

    根据需求,软键盘弹起时,不允许页面整体向上滑动 用到的属性是:  :adjust-position="false" uni-app 软键盘顶起底部fixed定位的输入框 页面就不会 ...

  5. Dubbo架构设计与源码解析(三)责任链模式

    作者:周可强 一.责任链模式简介 1.责任链模式定义 责任链(Chain of Responsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前一对 ...

  6. jquerylib表单

    用jquerylib,实现表格添加内容和删除内容 <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  7. [sklearn] 决策树、随机森林、隐马尔可夫模型

    决策树 决策树(Decision Tree)是一种用于处理分类和回归问题的无监督学习算法.如下图所示为某女青年在某相亲网站的相亲决策图.这幅图描述的都是一个非常典型的决策树模型. 通过对其相亲决策的分 ...

  8. [OpenCV实战]17 基于卷积神经网络的OpenCV图像着色

    目录 1 彩色图像着色 1.1 定义着色问题 1.2 CNN彩色化结构 1.3 从 中恢复彩色图像 1.4 具有颜色再平衡的多项式损失函数 1.5 着色结果 2 OpenCV中实现着色 2.1 模型下 ...

  9. 浅谈LCA问题(最近公共祖先)(四种做法)

    [模板]最近公共祖先(LCA) \(update \ 2023.1.3\) 新增了树链剖分 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入格式 第一行包含三个正整数 \ ...

  10. 工作这么多年,我总结的数据传输对象 (DTO) 的最佳实践

    前言 数据传输对象 (DTO) 是一种设计模式,常用于软件开发不同层或者不同系统之间传输数据.DTO 的主要目的是封装数据并防止它被其他层或系统直接访问或修改.通过遵循一组最佳实践,开发人员可以确保他 ...