题面

思路

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

\(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. vulnhub靶场渗透实战11-Deathnote

    ​网络模式,怎么方便怎么来. 靶场地址:https://download.vulnhub.com/deathnote/Deathnote.ova 这个靶机很简单.vbox装好.​ 1:主机发现,和端口 ...

  2. ArcGIS API for JS 访问浏览器提示不安全链接的服务时 如何设置

    当我们通过ArcGISServer发布了服务后,首先要在要访问的计算机上通过浏览器访问下服务是否可用.但有时候会出现不安全的标识. 如果这样的链接直接放到代码中,可能会加载失败. 代码如下. var ...

  3. 【大数据面试】Hbase:数据、模型结构、操作、读写数据流程、集成、优化

    一.概述 1.概念 分布式.可扩展.海量数据存储的NoSQL数据库 2.模型结构 (1)逻辑结构 store相当于某张表中的某个列族 (2)存储结构 (3)模型介绍 Name Space:相当于数据库 ...

  4. ATM项目

    ATM项目实战 项目需求分析: 1.注册(密码要加密) 2.登陆 3.查看余额 4.提现(可自定手续费) 5.还款 6.转账 7.查看流水 8.添加购物车功能 (商品可配置) 9.查看购物车功能 10 ...

  5. global与nonlocal、函数名用法、闭包函数、装饰器

    今日内容回顾 目录 今日内容回顾 global与nonlocal 函数名的多种用法 闭包函数 装饰器简介 装饰器推导流程 装饰器模板 装饰器语法糖 练习 global与nonlocal 函数名的多种用 ...

  6. uniapp 微信小程序 根据经纬度解析地址(腾讯地图)

    //引入腾旭地图sdk import QQMapWX from '../../common/qqmap-wx-jssdk.js' onLoad(){ this.getMapAddress() }, m ...

  7. [OpenCV实战]7 使用YOLOv3和OpenCV进行基于深度学习的目标检测

    目录 1 YOLO介绍 1.1 YOLOv3原理 1.2 为什么要将OpenCV用于YOLO? 1.3 在Darknet和OpenCV上对YOLOv3进行速度测试 2 使用YOLOv3进行对象检测(C ...

  8. 为什么 java 容器推荐使用 ExitOnOutOfMemoryError 而非 HeapDumpOnOutOfMemoryError ?

    前言 好久没写文章了, 今天之所以突然心血来潮, 是因为昨天出现了这样一个情况: 我们公司的某个手机APP后端的用户(customer)微服务出现内存泄露, 导致OutOfMemoryError, 但 ...

  9. CVE-2020-13933

    漏洞名称 Apache Shiro 身份验证绕过漏洞复现CVE-2020-13933 利用条件 Apache Shiro < 1.6.0 漏洞原理 Apache Shiro是一个强大且易用的Ja ...

  10. MAC中Flutter下载和安装

    Flutter官网 https://flutter.dev/docs/development/tools/sdk/releases?tab=macos   一.终端下配置系统环境变量和镜像 我的mac ...