分治的时候SZ感觉是错的……但是貌似第一次找好重心就够了,之后SZ别太离谱就不会T,重心随一随缘就好……

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e4 + 5;
int n, k, mx, SZ, ans;
struct Edge {
int to, nxt, cost;
}e[maxn << 2];
int head[maxn], tot, size[maxn], root, dis[maxn], vis[maxn];
int l, r, Q[maxn]; void add(int u, int v, int c) {
e[++tot].to = v, e[tot].cost = c, e[tot].nxt = head[u], head[u] = tot;
} void GetRoot(int cur, int fa) {
int tmp = 0;
size[cur] = 1;
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (son == fa || vis[son]) continue;
GetRoot(son, cur);
size[cur] += size[son];
tmp = max(tmp, size[son]);
}
tmp = max(tmp, SZ - size[cur]);
if (tmp < mx) mx = tmp, root = cur;
} void GetDis(int cur, int fa) {
Q[++r] = dis[cur];
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (son == fa || vis[son]) continue;
dis[son] = dis[cur] + e[i].cost;
GetDis(son, cur);
}
} int calc(int cur, int val) {
l = 1, r = 0;
dis[cur] = val;
GetDis(cur, 0);
sort(Q + 1, Q + 1 + r);
int res = 0;
while (l < r) {
if (Q[l] + Q[r] <= k) res += r - l, l++;
else r--;
}
return res;
} void divide(int cur) {
ans += calc(cur, 0);
vis[cur] = 1;
for (int i = head[cur]; i; i = e[i].nxt) {
int son = e[i].to;
if (vis[son]) continue;
ans -= calc(son, e[i].cost);
mx = 2e9, SZ = size[son];
GetRoot(son, 0);
divide(root);
}
} void init() {
ans = 0, tot = 0, mx = 2e9, SZ = n;
for (int i = 1; i <= n; i++) head[i] = 0, vis[i] = 0;
} int main() {
while (scanf("%d %d", &n, &k) == 2 && (n | k)) {
init();
for (int u, v, cost, i = 1; i < n; i++) {
scanf("%d %d %d", &u, &v, &cost);
add(u, v, cost), add(v, u, cost);
}
GetRoot(1, 0);
divide(root);
printf("%d\n", ans);
}
return 0;
}

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

  1. POJ1741 点分治模板

    传送门:http://poj.org/problem?id=1741 题意: 求树上两点间路径长度小于k的点对个数 题解: 参考资料 守望的淀粉质略解:https://www.luogu.org/bl ...

  2. POJ-1741 树上分治--点分治(算法太奇妙了)

    给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...

  3. Codeforces 293E 点分治+cdq

    Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...

  4. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  5. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  6. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  7. [bzoj1468][poj1741]Tree_点分治

    Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...

  8. Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)

    [POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...

  9. poj1741(入门点分治)

    题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...

  10. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

随机推荐

  1. Hive- Hive Web Interface

    当我们安装好hive时候,我们启动hive的UI界面的时候,命令: hive –-service hwi ,报错,没有war包 我们查看hive/conf/hive-default.xml.templ ...

  2. HTML/CSS实现的搜索框

    谷歌和百度首页的搜索框都是<input>+<button>模式的,bing的搜索框感觉要好点儿.简言之,就是将提交按钮放到<input>中,其实这是做不到的,只能伪 ...

  3. IDEA编译less插件LESS CSS Compiler的安装

    1.IDEA插件地址:LESS CSS Compiler 百度云盘下载地址 2.安装Node.js,下载 3.打开idea→settings→plugins 安装:“nodejs”插件,并按以下步骤进 ...

  4. 「LuoguP1429」 平面最近点对(加强版)

    题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...

  5. Mysql源码学习——源码目录结构

    目录清单 目录名 注释 Bdb 伯克利DB表引擎 BUILD 构建工程的脚本 Client 客户端 Cmd-line-utils 命令行工具 Config 构建工程所需的一些文件 Dbug Fred ...

  6. WPF Background的设置有坑

    今天帮忙同事解决在后台绑定时,动态更改控件(Grid)的Background. 有个陷阱,C#有2个命名空间有Brush和Color, 分别为System.Drawing和System.Window. ...

  7. HDU4027(线段树单点更新区间)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  8. 0003_Linux基础之常用命令

    1.pwd:查看当前所在目录 2.cd :切换目录 3.ls:查看当前目录下的文件及文件夹: 4.ls -l :列出当前目录下文件及详细信息         drwxr-xr-x   第一个字符为d则 ...

  9. 面试题: 数据库笔试 sql操作 已看 上课的练习题50sql

    2018/5/31 oracle数据库面试笔试试题总结http://www.yjbys.com/qiuzhizhinan/show-308759.html 1/4Oracle数据库1.基础测试选择在部 ...

  10. FASTQ格式

    FASQT格式是用于存储生物序列(通常是核苷酸序列)及其相应的碱基质量分数的一种文本格式.为简洁起见,序列字母和质量分数均使用单个ASCII字符进行编码.最初由Wellcome Trust Sange ...