Tree
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 14706
Accepted: 4781
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
Source
LouTiancheng@POJ
題意:給定n個點的樹,求其中任意兩個點的距離<=k的對數。 分析:分為兩種情況,一種是不同支的情況,這種好處理一些,直接往上找到公共父親權值相加就可以;另一種就是同支的情況,把重復的權值刪去就行了,只是實現起來麻煩些。另外推薦一篇博客
詳解,我也是看的他的,很厲害。
#include
#include
#include
#include
#include
#include