POJ1741(点分治)
分治的时候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(点分治)的更多相关文章
- POJ1741 点分治模板
传送门:http://poj.org/problem?id=1741 题意: 求树上两点间路径长度小于k的点对个数 题解: 参考资料 守望的淀粉质略解:https://www.luogu.org/bl ...
- POJ-1741 树上分治--点分治(算法太奇妙了)
给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- 【POJ1741】Tree(点分治)
[POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- poj1741(入门点分治)
题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- linux进程学习-进程描述符,控制块
从数据结构的角度,进程用task_struct结构来描述,称为“进程描述符 (Process Descriptor)”或者“进程控制块(Process Control Block, PCB)”,其包含 ...
- Sed在匹配行前后加入一行
a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)i 插入内容 sed ‘/匹配词/i\要加入的内容’ example.file 将 ...
- Sysctl命令及linux内核参数调整
一.Sysctl命令用来配置与显示在/proc/sys目录中的内核参数.如果想使参数长期保存,可以通过编辑/etc/sysctl.conf文件来实现. 命令格式: sysctl [-n ...
- WC2017 冬眠记
2017年2月3日,为期7天的冬眠营冬令营正式开幕. 前4天我们见到了各种集训队dalao们的华丽身姿 感受到了听课听不懂睡觉又惭愧的无力感 见到了几百号人近一半玩手机,剩下的一半有一半在睡觉,再剩下 ...
- 关于Django ORM filter方法小结
django filter是一个过滤器,相当于SQL的select * from where. filter返回一个QuerySet对象,还可以在该对象上继续进行django orm 该有的操作. 有 ...
- 推荐几个Laravel 后台管理系统
小编推荐几个Laravel 后台管理系统 由百牛信息技术bainiu.ltd整理发布于博客园 一.不容错过的Laravel后台管理扩展包 —— Voyager 简介Voyager是一个你不容错过的La ...
- GBK点阵显示字库的制作和使用
转自:http://blog.csdn.net/exbob/article/details/6539643 GBK编码共收录汉字21003个.符号883个,并提供1894个造字码位,简.繁体字融于一库 ...
- SoapUI测试登录
实际登录过程为: 1. 打开/login页面 2. 登录 3. 自动调整至/dashboard页面 SoapUI的设计上,每一次请求后,都会清理掉cookie,于是设计的测试登录过程如下: 1. po ...
- eclipse必备快捷键
1.[ Ctrl + Shift+ P ],查找括号的开始和闭合 2.[ALT+/],这个快捷键应该没有人不知道 3.[Ctrl+O],显示类中方法和属性的大纲,能快速定位类的方法和属性,在查找Bu ...
- CABasicAnimation动画及其keypath值和作用
//tarnsform放大缩小动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transf ...