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. RCF的简单使用教程以及什么是回调函数

    RCF的使用教程 RCF(Remote Call Framework)是一个使用C++编写的RPC框架,在底层RCF支持多种传输实现方式(transport implementations). 包括T ...

  2. Bash游戏(51Nod - 1046)

    20180603 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得比赛 ...

  3. ABAP术语-Transaction

    Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/19/1112804.html Logical process in ...

  4. JSON字符串与JS对象格式转换

    JSON通常用于服务器向客户端传送数据,传回来的JSON数据是字符串的形式,所以要转变为JS对象形式才方便我们使用. JSON字符串转变为JS对象:JSON.parse( ); JS对象转变为JSON ...

  5. Python入门 —— 05时间日期处理小结

    此文多涉及基础,如果想要深入理解则到文末,有提供链接 涉及对象 1. datetime 2. timestamp 3. time tuple 4. string 5. date - datetime基 ...

  6. angular 打包

    ERROR in ng:///F:/IDEWorkspace/dsmc/dsmc-front-new/trunk/src/app/routes/city-manage/component-coding ...

  7. colspan和rowspan合并单元格

    最近在回顾html的时候,经常碰到一些table标签的问题,其中大多数都是合并单元格,所以在这里记录下自己的探究过程: <table cellpadding="0" cell ...

  8. 安装使用supervisor来启动服务

    supervisor 使用方法 supervisor(官网)是一个unix的系统进程管理软件,可以用它来管理apache.nginx等服务, 若服务挂了可以让它们自动重启.当然也可以用来实现golan ...

  9. 汇编指令lodsb和stosb、lodsd和stosd

    lodsb指令,将esi指向的地址处的数据取出来赋给AL寄存器,esi=esi+1: lodsw指令则取得是一个字. lodsd指令,取得是双字节,即mov eax,[esi],esi=esi+4: ...

  10. google friendly testing

    https://www.google.com/webmasters/tools/mobile-friendly/?mc_cid=cc21b18877&mc_eid=cf2bbeb9b2 htt ...