分治的时候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. RQNOJ 57 找啊找啊找GF:01背包

    题目链接:https://www.rqnoj.cn/problem/57 题意: sqybi在七夕这天太无聊了,所以他想去给自己找GF. 总共有n个MM. 搞定第i个MM要花费rmb[i]块大洋.rp ...

  2. JS遍历获取多个控件(使用索引‘i’)

    1.n个tid="n1"的input.n个tid="n2"的input.n个tid="n3"的input---循环遍历 ; i <= ...

  3. 分享知识-快乐自己:Hibernate中的 quert.list() 与 quert.iterate() 方法区别

    区别如下: quert.list() : 1):每次都是通过一条语句直接操作数据库取出所有的数据返回(并且将对象存入hibernate缓存中): 2):不会从一二级缓存中查询数据: 3):之执行一条S ...

  4. win32com操作word(2):常用用法

    一.对象的位置关系: 1.Range属性位于(部分): Selection__Section__Cell__Paragraph__Table__Bookmark__Comment__Row__List ...

  5. ACM学习历程—Hihocoder编程之美测试赛B题 大神与三位小伙伴(组合数学 )

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给你一个m x n (1 <= m, n <= 100)的矩阵A (0<=aij<=10000),要 ...

  6. Codeforces 756C Nikita and stack

    Codeforces 756C Nikita and stack 题目大意: 给定一个对栈进行操作的操作序列,初始时序列上没有任何操作,每一次将一个本来没有操作的位置变为某一操作(push(x),po ...

  7. 洛谷P1474货币系统——背包方案计数

    题目:https://www.luogu.org/problemnew/show/P1474 完全背包,注意方案计数的方法. 代码如下: #include<iostream> #inclu ...

  8. 对于makefile传递参数的一些问题

    makefile变量说明: 1.总控Makefile中使用“-e”参数覆盖下一层Makefile中的变量. 2.父级Makefile向子级Makefile传送变量方式:export <varia ...

  9. GBK点阵显示字库的制作和使用

    转自:http://blog.csdn.net/exbob/article/details/6539643 GBK编码共收录汉字21003个.符号883个,并提供1894个造字码位,简.繁体字融于一库 ...

  10. openstack 虚拟机添加网卡

    Openstack dashborad是没有给虚拟机添加网卡这个功能的,但是后台是有这行的接口的. 首先我们创建一个虚拟机,这个虚拟机制11.11.11网段的如图: