树分治,每次把每个点的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的更多相关文章

  1. 【poj1741】 Tree

    http://poj.org/problem?id=1741 (题目链接) 题意 给出一个n个节点的带权树,求树上距离不超过K的所有点对的个数. solution  点分治裸题.所谓的点分治,就是对于 ...

  2. poj1741 bzoj2152

    树分治入门 poj1741是男人八题之一,经典的树分治的题目这里用到的是点分治核心思想是我们把某个点i作为根,把路径分为过点i和不过点i先统计过点i这样的路径数,然后在统计其子树中的答案,这样就不断地 ...

  3. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  4. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  5. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  6. [bzoj1468][poj1741]Tree_点分治

    Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...

  7. Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)

    [POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...

  8. poj1741(入门点分治)

    题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...

  9. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

  10. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

随机推荐

  1. 前端js代码优化

    今天给大家分享下js代码优化的相关技巧. 1.使用"+"转换为数值     我们平时开发过程中需要将数字字符串创转为number类型,大多数都会用JavaScript parseI ...

  2. XAMPP简介、安转、使用

    虽然没有写Mac安装方法及使用, 但方法也都大相径庭, 殊途同归而已. XAMPP简介 XAMPP是一款开源.免费的网络服务器软件,经过简单安装后,就可以在个人电脑上搭建服务器环境.本文为大家介绍Wi ...

  3. 阿里云 virtual memory exhausted: 无法分配内存

    在阿里云买了个云服务器,内存1G.编译php时出现下面的错误: virtual memory exhausted: Cannot allocate memory 问题原因:由于物理内存本身很小,且阿里 ...

  4. HP中spl_autoload_register函数的用法

    spl_autoload_register(PHP 5 >= 5.1.2) spl_autoload_register - 注册__autoload()函数 说明bool spl_autoloa ...

  5. hdu4143 A Simple Problem

    A Simple Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  6. appium+Python 启动app(一)

    当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...

  7. MVC5中使用Log4Net

    最早搜到的是这篇: http://www.codeproject.com/Articles/823247/How-to-use-Apache-log-net-library-with-ASP-NET- ...

  8. 机器学习之Adaboost (自适应增强)算法

    注:本篇博文是根据其他优秀博文编写的,我只是对其改变了知识的排序,另外代码是<机器学习实战>中的.转载请标明出处及参考资料. 1 Adaboost 算法实现过程 1.1 什么是 Adabo ...

  9. django-表单

    一.从Request对象中获取信息 1.URL相关的信息 属性/方法 说明 举例 request.path 除域名以外的请求路径,以正斜杠开头 "/hello/" request. ...

  10. Nginx和Apache有什么区别?

    Nginx抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能. Apache  rewrite ,比nginx 的rewrite ...