Tree

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 25772   Accepted: 8566

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.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; struct Edge{
int to,nxt,w;
}e[N<<];
int head[N],son[N],f[N],deth[N],d[N];
bool vis[N];
int ans,tot,Root,sum,n,k; void init() {
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
ans = tot = Root = ;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
}
void getroot(int u,int pa) {
son[u] = ;f[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa||vis[v]) continue;
getroot(v,u);
son[u] += son[v];
f[u] = max(f[u],son[v]);
}
f[u] = max(f[u],sum-son[u]);
if (f[u] < f[Root]) Root = u;
}
void getdeth(int u,int pa) {
deth[++deth[]] = d[u];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (v==pa||vis[v]) continue;
d[v] = d[u] + w;
getdeth(v,u);
}
}
int calcc(int u,int w) {
deth[] = ;
d[u] = w;
getdeth(u,);
sort(deth+,deth+deth[]+);
int l = ,r = deth[],ret = ;
while (l < r) {
if (deth[l] + deth[r] <= k) ret += r-l,l++;
else r--;
}
return ret;
}
void work(int u) {
ans += calcc(u,);
vis[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;
if (vis[v]) continue;
ans -= calcc(v,w);
sum = son[v];
Root = ;
getroot(v,);
work(Root);
}
}
int main() {
while (scanf("%d%d",&n,&k)!=EOF && !(n==&&k==)) {
init();
for (int a,b,c,i=; i<n; ++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
f[] = 1e9;
sum = n;
getroot(,);
work(Root);
printf("%d\n",ans);
}
return ;
}

POJ1741 Tree (点分治)的更多相关文章

  1. [POJ1741]Tree(点分治)

    树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...

  2. [poj1741]Tree(点分治+容斥原理)

    题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...

  3. [bzoj1468][poj1741]Tree[点分治]

    可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...

  4. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

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

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

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

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

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

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

  8. POJ1741 tree 【点分治】

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25286   Accepted: 8421 Description ...

  9. POJ1741 Tree(树的点分治基础题)

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

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

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

随机推荐

  1. js为页面元素添加水印

    近期有需求为页面部分区域添加上水印,通过在网上找到了js为页面添加水印的方法,后来经过自己的改进,可以实现为页面部分元素添加水印,最终效果如下图: 代码如下: function watermark(s ...

  2. 【干货】JavaScript DOM编程艺术学习笔记7-9

    七.动态创建标记 在文档中不写占位图片和文字代码,在能调用js的情况下动态创建,文档支持性更好. 在原来的addLoadEvent prepareGallery showPic的基础上增加函数prep ...

  3. C#中 String和string的区别

    string 是 System.String的别名,所以相当于声明了 using string = System.String 再者,string是C#的关键字,String是System下的类名.

  4. 卸载VS2013 2015

    我有两个VS,特别讨厌,每当使用window程序删除时候,就出现 停止工作! 然后从知乎上发现了这个 https://github.com/Microsoft/VisualStudioUninstal ...

  5. use scanner/smb/smb_version

    use scanner/smb/smb_version msf auxiliary(smb_version) > set RHOSTS 172.16.21.170RHOSTS => 172 ...

  6. hihocoder第三十六周 二分查找

    题目链接:http://hihocoder.com/contest/hiho36/problem/1 , 一个比较简单的二分. 算法: 由于数据量比较大,O(nlogn)无法通过,所以不能先排序再查找 ...

  7. 机器学习-octave使用

    1 == 2    % false 1 ~=2     % true % 隐藏版本,只显示>> . PS1('>> '); % 输出两位小数格式 disp(sprintf('2 ...

  8. Android(java)学习笔记89:Bundle和Intent类使用和交互

    1. Bundle 和 Intent:    Bundle只是一个信息的载体 将内部的内容以键值对组织 ,Intent负责Activity之间的交互自己是带有一个Bundle的.Intent.putE ...

  9. Android(java)学习笔记88:BaseAdapter适配器重写之getView()

    1. BaseAdapter适配器重写 之getView(): (1)View getview(int position, View convertview, ViewGroup parent ) 第 ...

  10. 漫谈 Clustering (番外篇): Vector Quantization

    在接下去说其他的聚类算法之前,让我们先插进来说一说一个有点跑题的东西:Vector Quantization.这项技术广泛地用在信号处理以及数据压缩等领域.事实上,在 JPEG 和 MPEG-4 等多 ...