Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001). 
Define dist(u,v)=The min distance between node u and v. 
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k. 
Write a program that will count how many pairs which are valid for a given tree. 

Input

The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

题目大意:给一个带边权的树,问有多少对点满足dist(i,j)<=k

思路:可以参考2009年的国家集训队论文《分治算法在树的路径问题中的应用》——漆子超。

代码(188MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x7fff7fff; int head[MAXN], size[MAXN], maxSize[MAXN];
int list[MAXN], cnt;
bool del[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, k, ecnt; void init() {
memset(head, -, sizeof(head));
memset(del, , sizeof(del));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} void dfs1(int u, int f) {
size[u] = ;
maxSize[u] = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f || del[v]) continue;
dfs1(v, u);
size[u] += size[v];
maxSize[u] = max(maxSize[u], size[v]);
}
list[cnt++] = u;
} int get_root(int u, int f) {
cnt = ;
dfs1(u, f);
int ret, maxr = INF;
for(int i = ; i < cnt; ++i) {
int &x = list[i];
if(max(maxSize[x], size[u] - size[x]) < maxr) {
ret = x;
maxr = max(maxSize[x], size[u] - size[x]);
}
}
return ret;
} void dfs2(int u, int f, int dis) {
list[cnt++] = dis;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f || del[v]) continue;
dfs2(v, u, dis + cost[p]);
}
} int calc(int a, int b) {
int j = b - , ret = ;
for(int i = a; i < b; ++i) {
while(list[i] + list[j] > k && i < j) --j;
ret += j - i;
if(j == i) break;
}
return ret;
} int ans = ; void work(int u, int f) {
int root = get_root(u, f);
del[root] = true;
int last = ; cnt = ;
for(int p = head[root]; ~p; p = next[p]) {
int &v = to[p];
if(del[v]) continue;
dfs2(v, root, cost[p]);
sort(list + last, list + cnt);
ans -= calc(last, cnt);
last = cnt;
}
list[cnt++] = ;
sort(list, list + cnt);
ans += calc(, cnt);
for(int p = head[root]; ~p; p = next[p]) {
int &v = to[p];
if(del[v]) continue;
work(v, root);
}
} int main() {
while(scanf("%d%d", &n, &k) != EOF) {
if(n == && k == ) break;
init();
for(int i = ; i < n; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
ans = ;
work(, );
printf("%d\n", ans);
}
}

POJ 1741 Tree(树的分治)的更多相关文章

  1. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  2. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  3. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  4. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  5. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  6. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  7. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  8. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  9. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  10. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

随机推荐

  1. UIPanGestureRecognizer 拖动TableView改变其高度

    需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...

  2. Windows10 IIS安装php manager和IIS URL Rewrite 2.0组件的方法

    Windows10中自带的Server:Microsoft-IIS///8.5/10上安装.微软脑子秀逗,跳过了9,以为能解决版本识别的问题,没想到弄成10,还是出现了版本识别的问题,真是自己打自己的 ...

  3. Python系列之入门篇——pytables及其客户端

    pytables及其客户端查看 pytables # ubuntu sudo apt-get install python-tables pip install flask flask-httpaut ...

  4. 1.Python是什么

    前言   这里只是根据个人的理解而谈,庸俗浅薄,不是科学定义,也可以认为是假装自己理解啦,掩耳盗铃罢了.知无涯是多么的恐怖,哈哈 计算机语言   此处的语言不同于我们生活中所说的语言,因为生活中的语言 ...

  5. ecshop跨站漏洞详情及修补网站漏洞

    ecshop目前最新版本为4.0,是国内开源的一套商城系统,很多外贸公司,以及电商平台都在使用,正因为使用的人数较多,很多攻击者都在挖掘该网站的漏洞,就在最近ecshop被爆出高危漏洞,该漏洞利用跨站 ...

  6. struts2学习笔记四

    一.contextMap中的数据操作 root根:List 元素1 元素2 元素3 元素4 元素5 contextMap:Map key value application Map key value ...

  7. Chrome浏览器保存微信公众号文章中的图片

    用chrome浏览器打开微信公众号文章中时,另存为图片时保存的是640.webp,不是图片本身,用IE则没有此问题.大部分chrome插件也无法保存图片. 经过多番尝试,找到一款插件可以批量保存微信公 ...

  8. FPGA烧完程序之后,检测不到网口的

    原因:未给phy芯片添加复位 解决方法:在程序顶部添加一个输出信号output e_reset,使其值一直为高. output e_reset, 'b1;

  9. Linux学习-rsyslog.service :记录登录文件的服务

    rsyslog.service 的配置文件:/etc/rsyslog.conf 我们现在知道 rsyslogd 可以负责主机产生的各个信息的登录,而这些信息本身是有『严重等级』之分的, 而且, 这些资 ...

  10. 版本控制工具——Git的拓展使用

    一.使用Github 通过前面两节已经配置了SSH Key与Github上的相关设置,接下来介绍常用的使用 使用Fork克隆一份到本地仓库 之后可以在自己的仓库克隆一份到本地 git clone gi ...