树分治,每次把每个点的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. laravel服务容器-----深入理解控制反转(IoC)和依赖注入(DI)

    首先大家想一想什么是容器,字面意思就是盛放东西的东西,常见的变量,对象属性都是容器,一个容器能够装什么东西,完全在于你对这个容器的定义.有的容器不仅仅只是存文本,变量,而是对象,属性,那么我们通过这种 ...

  2. golang的GET请求(类似于PHP的CURL)

    check_url := "https://www.baidu.com" header := make(map[string]string) res, err := util.Hp ...

  3. 在C#中几种常见数组复制方法的效率对比

    原文是在http://blog.csdn.net/jiangzhanchang/article/details/9998229 看到的,本文在原文基础上增加了新的方法,并对多种数据类型做了更全面的对比 ...

  4. php环境下所有的配置文件以及作用

    以下主要是针对linux下的目录(windows也是一样,文件名都一样) Apache:etc/httpd.conf PHP:etc/php.ini   (Apache 正在运行的 PHP 版本) M ...

  5. @Scope注解

    @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)这个是说在每次注入的时候回自动创建一个新的bean实例 @Scope(value=Config ...

  6. Java爬虫----有道翻译初步

    目标:http://fanyi.youdao.com/ 用爬虫实现翻译功能. 利用f12查看网页Network,可以发现 有关翻译的表单请求通过  http://fanyi.youdao.com/tr ...

  7. nyoj49 开心的小明 01背包

    思路:dp(i, j)表示用金钱j去买前i个物品能得到的最大价值.转移方程dp(i, j) = max{dp(i-1, j), dp(i-1, j-p[i]) + p[i]*w[i]}. AC代码 # ...

  8. 模板语言变量,js变量,js自执行函数之前嵌套调用

    1.模板语言变量 前端html页面中展示 {{ nodeIp }} 2.js变量引用模板语言变量 把模板语言变量传递给js,js去执行页面操作(变量的转换,只适用于字符串) var IP = &quo ...

  9. 单例模式,堆,BST,AVL树,红黑树

    单例模式 第一种(懒汉,线程不安全): public class Singleton { private static Singleton instance; private Singleton () ...

  10. Git 用户名和邮箱

    用户名邮箱的作用 用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. github的contributions统计就是按邮箱来统计的. 查看用 ...