POJ1741
树分治,每次把每个点的deep取出,排序。。。尺取法。。。
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <algorithm>
# include <string.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
}
const int MAXN(20010), INF(2147483647);
int n, cnt, fst[MAXN], to[MAXN], nxt[MAXN], w[MAXN], k;
int size[MAXN], rt, sz, mx[MAXN], vis[MAXN];
ll ans, d[MAXN];
IL void Add(RG int u, RG int v, RG int f){ nxt[cnt] = fst[u]; to[cnt] = v; w[cnt] = f; fst[u] = cnt++; }
IL void Getroot(RG int u, RG int fa){
size[u] = 1; mx[u] = 0;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]] || to[e] == fa) continue;
Getroot(to[e], u);
size[u] += size[to[e]];
mx[u] = max(mx[u], size[to[e]]);
}
mx[u] = max(mx[u], sz - size[u]);
if(mx[u] < mx[rt]) rt = u;
}
IL void Getdeep(RG int u, RG int fa, RG ll dis){
d[++cnt] = dis;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]] || to[e] == fa) continue;
Getdeep(to[e], u, dis + w[e]);
}
}
IL ll Calc(RG int u, RG int f){
cnt = 0; Getdeep(u, 0, f);
sort(d + 1, d + cnt + 1);
RG ll ret = 0, l = 1, r = cnt;
while(l < r) if(d[r] + d[l] <= k) ret += r - l, l++; else r--;
return ret;
}
IL void Solve(RG int u){
ans += Calc(u, 0); vis[u] = 1;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]]) continue;
ans -= Calc(to[e], w[e]);
rt = 0; sz = size[to[e]];
Getroot(to[e], u);
Solve(rt);
}
}
int main(RG int argc, RG char* argv[]){
n = Read(); k = Read(); mx[0] = INF;
while(n && k){
Fill(fst, -1); Fill(vis, 0); rt = ans = cnt = 0; sz = n;
for(RG int i = 1, u, v, f; i < n; i++) u = Read(), v = Read(), f = Read(), Add(u, v, f), Add(v, u, f);
Getroot(1, 0); Solve(rt);
printf("%lld\n", ans);
n = Read(); k = Read();
}
return 0;
}
POJ1741的更多相关文章
- 【poj1741】 Tree
http://poj.org/problem?id=1741 (题目链接) 题意 给出一个n个节点的带权树,求树上距离不超过K的所有点对的个数. solution 点分治裸题.所谓的点分治,就是对于 ...
- poj1741 bzoj2152
树分治入门 poj1741是男人八题之一,经典的树分治的题目这里用到的是点分治核心思想是我们把某个点i作为根,把路径分为过点i和不过点i先统计过点i这样的路径数,然后在统计其子树中的答案,这样就不断地 ...
- 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 ...
- [poj1741][tree] (树/点分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
随机推荐
- 一个备份mysql 数据库的脚本
# 获取当前系统日期,格式为: 2009-2-21DATE=`date "+%F"` # 定义mysql 服务的主目录 DB_DIR=/usr # 定义备份后的路径BAK_DIR= ...
- Zabbix 3.0 监控Web
zabbix 界面配置 触发器添加
- 使用IPTABLES限制IP上传下载速度,如何用iptables限速?
怎样使用IPTABLES限制IP上传下载速度,如何用iptables限速?我们先来看范例: iptables限制某IP的上传速度为1000KB/秒(8Mbps,流入服务器带宽),即在此IP所在的服务器 ...
- map,vector 等容器内容的循环删除问题(C++)
map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...
- 五分钟了解Hash算法
Hash算法详解 想象一下如果高级语言(Java,C++ ,C#)中如果没有实现类似List.Map等数据结构,企业级应用开发将是多么痛苦的事吧? Key-Value这种数据结构对于数据处理非常方便. ...
- Git环境配置+VSCode中文乱码问题
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html 1.VSCode中文乱码问题 (files.autoguessEncoding= ...
- 记一次 bug 修复 , 未将对象引用实例化
我们对默认值的使用技巧中,同一个组件, 升级版本,增加新的配置字段,执行新的逻辑. 老版本,没有类似的配置字段,走原始逻辑. 在类的构造中,添加了这么一句代码, 运行后,报错,没看出问题原因: boo ...
- 《android开发艺术探索》读书笔记(六)--Drawable
接上篇<android开发艺术探索>读书笔记(五)--RemoteViews [BitmapDrawable] 简单的图片 <!xml version="1.0" ...
- 编码问题 php字符编码转换类
各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...
- Redis 设置开机启动
1. 将下列代码保存为文件redis, 置于 /etc/init.d 下面 ########################### # chkconfig: 2345 90 10 redis服务必须在 ...