hiho#1050: The longest road in the tree

Source: Internet
Author: User

#1050: The longest time limit in a tree:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Last said, Little Ho got a binary tree toy, the toy is connected by small balls and sticks, and in the process of dismantling it, Little Ho found that he can not only piece together into a two-pronged tree! can also be pieced together into a multi-fork tree-well, is actually more ordinary trees.

But anyway, little Ho's favorite toy has been upgraded, so he is more admiringly (in fact, the ball and sticks have what fun is it =). The toy tree in Small Ho's hands is now pieced together by n balls and N-1 sticks, all of which are marked by small ho with different numbers, and these numbers are all out of 1. Within the range of N, each stick is connected to two different balls, and there are no more than two different paths between any two spheres that can reach each other. In a word, it's a pretty fun toy!

But little hi saw small ho this way, think he is so addicted to it is not a good thing, so he thought to find some more questions let him think about it-but to small hi's level, nature is extremely easy!

So this day after breakfast, small hi will be holding a tree toy playing with the joy of Small Ho Road: You say you play this thing every day, I asked you a question, see if you can know? ”

Bad Little Ho didn't want to refuse.

"Then you continue to play, a time to return home when I do not call on you ~" Little hi Serious way.

Eh No, no, no, no, you said you said, I'm listening. "The Little Ho, who has always been used to opening follow patterns, cries out," he said.

Small hi satisfied nodded, then said: "This is right, my question is very simple, that is-- you this tree which two nodes in the distance between the longest?" Of course, the distance here refers to the number of sticks that go through one node to another. ”。

"Ah?" Little Ho looked down at the toy tree in his hand and was puzzled.

Hint One: The road always has the vertex, the path is no exception! Input

Each test point (input file) has and has only one set of test data.

The first behavior of each set of test data is an integer n, meaning as described earlier.

Each group of test data of the line 2~n, each line describes a stick, wherein the I+1 Act Two integer Ai,bi, which represents the number of two balls connected by the root of the I stick.

For 20% of data, meet n<=10.

For 50% of data, meet n<=10^3.

For 100% of data, meet N<=10^5,1<=ai<=n, 1<=bi<=n

Little hi Tip: Those with array storage tree edge remember to open twice times the size Oh!

Output

For each set of test data, output an integer ans, which indicates the distance between the two nodes farthest from the given tree.

Sample input
81 21 31 44 53 66 77 8
Sample output
6

This problem and hdu2196 simple to find is the longest road of trees

first assume that the tree's longest two leaf nodes are v1,v2, then The existing conclusion that the farthest point from any point u must be (V1,V2), and then

The furthest point from V1 or V2 must be v2 or v1. So after two searches you can find the longest path.

If you want to see the proof, please read my article, there is proof of this conclusion

AC Code:




hiho#1050: The longest road in the 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.