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 ...
随机推荐
- JSON工具类
import java.sql.Timestamp; import java.util.Collection; import java.util.Date; import org.soul.util. ...
- laravel服务容器-----深入理解控制反转(IoC)和依赖注入(DI)
首先大家想一想什么是容器,字面意思就是盛放东西的东西,常见的变量,对象属性都是容器,一个容器能够装什么东西,完全在于你对这个容器的定义.有的容器不仅仅只是存文本,变量,而是对象,属性,那么我们通过这种 ...
- 恢复linux系统文件夹颜色
/etc/DIR_COLORS 默认值 # Background color codes:# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta ...
- Linux 编译安装 php 扩展包 curl
php源码目录:/root/php php编译目录:/usr/local/webserver/php/ curl源码目录:/root/curl 1.curl,主要用于发送http请求,是php的一个扩 ...
- Action里面的自带的字段的含义
- Ubuntu上搭建SVN
参考文档:http://www.linuxidc.com/Linux/2016-08/133961.htm http://www.linuxidc.com/Linux/2015-01/111956.h ...
- 一个逼格很低的appium自动化测试框架
Github地址: https://github.com/wuranxu 使用说明 1. 安装配置Mongo数据库 下载地址 mongo是用来存放元素定位的,截图如下: 通过case_id区分每个ca ...
- ClientURL库-curl_setopt()
这是一个出现得比较突兀的问题: 好好学习使用一下这个库:http://php.net/manual/zh/book.curl.php curl_setopt函数:curl_setopt - 设置一个c ...
- Luogu P1757 通天之分组背包
题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品相互冲突,现在,他想知道最大 ...
- 借鉴mini2440的usb-wifi工具集在Beagleboard上移植无线网卡
配置minicom: sudo yum install minicom sudo minicom -s 选择Serial port setup,此时所示光标在"Change which se ...