POJ 1741 Tree

Source: Internet
Author: User

Description

Give a tree with n vertices,each edge have 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 are called valid if and only if Dist (u,v) not exceed K.
Write a program that would count how many pairs which is valid for a given tree.

Input

The input contains several test cases. The first line of all test case contains-integers n, K. (n<=10000) The following n-1 lines each contains three int Egers u,v,l, which means there is an edge between node U and V of length L.
The last test was followed by the zeros.

Output

The For each test case output the answer to a single line.

Sample Input

5 41 2 31 3 11 4 23 5 10 0

Sample Output

8

The title means to give a tree with edge, to satisfy the logarithm of dist (u,v) <=k (u,v)

Solution, divide the method, the tree points divide the rule, may consider you and V respectively to cross the node, for Root, to find out all child nodes to its distance d[i], and then statistics d[i]+d[j]<=k logarithm, here can use the monotonous team processing, then we recursively processing Root's child, because this , we forget to be in the same sub-tree, so subtract.
In fact, these are not important, these algorithms on the Internet are said to be clear, but the details are not said, is the time of processing, all require a root,root is the center of the tree, with DP, otherwise it will time out

POJ 1741 Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.