分治的时候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. Zabbix数据库清理历史数据

    Zabbix清理历史数据 Zabbix是个很好的监控软件,随着公司监控项目越来越多,数据越来越多,zabbix负载重,可能造成系统性能下降. Zabbix里面最大的表就是历史记录表,history,h ...

  2. 时尚与深度学习系列:Fashion forward: Forecasting visual style in fashion

           https://arxiv.org/pdf/1705.06394.pdf         将深度学习与时尚预测联系在一起,是一个很有趣但是估计结果会没什么成效的话题.因为,时尚预测这一领 ...

  3. 集训Day3

    被疯狂造谣+请家长 但生活还得继续 ...今天的题口胡一下吧明天码 PKUSC2018 D1T1 对于x:若x不翻,则x的一半到x的数都不能翻 若x翻,则x到2x都得翻 剩下随便安排 排列组合一下 P ...

  4. Spring笔记06(Spring AOP的底层实现动态代理)

    1.代理模式readMe: 代理设计模式: 是java中常用的设计模式! 特点: .委托类和代理类有相同的接口或者共同的父类! .代理类为委托类负责处理消息,并将消息转发给委托类! .委托类和代理类对 ...

  5. 51nod_1236_序列求和 V3 _组合数学

    51nod_1236_序列求和 V3 _组合数学 Fib(n)表示斐波那契数列的第n项,Fib(n) = Fib(n-1) + Fib(n-2).Fib(0) = 0, Fib(1) = 1. (1, ...

  6. ubuntu强制卸载软件

    以卸载cups为例子 一:列出软件列表,找到需要卸载的软件的名字命令:dpkg --list

  7. 洛谷P1584 魔杖

    题目描述 Smart在春游时意外地得到了一种好东西——一种非常珍贵的树枝.这些树枝可以用来做优质的魔杖! 选择怎样的切割方式来制作魔杖非常重要,关键问题是——一把魔杖既不能太长.又不能太短,且制作出来 ...

  8. wxPython学习资料

    [译]wxPython布局管理简介 https://www.pystack.org/wxpython_sizer/ 设计器.代码分离 http://book.douban.com/review/578 ...

  9. poj2411铺砖——状压DP

    题目:http://poj.org/problem?id=2411 状态压缩,一行的状态记为一个二进制数,从上往下逐行DP,答案输出最后一行填0的方案数. 代码如下: #include<iost ...

  10. opencv报错 error: (-215) size.width>0 && size.height>0 in function cv::imshow

    使用opencv读取摄像头并且显示事出现此问题: 后来发现是图像为空时的错误,加入: if(!frame.empty()) imshow("video",frame); 完整的代码 ...