BENEFACT - The Benefactor

Another chapter of the municipal chronicles of a well known unbelievable lordly major town (if this town is not well known to you, you might want to solve problem CSTREET first) tells us the following story:

Once upon a time the citizens of the unbelievable lordly major town decided to elect a major. At that time this was a very new idea and election campaigns were completely unknown. But of course several citizens wanted to become major and it didn't took long for them to find out, that promising nice things that never will become real tends to be useful in such a situation. One candidate to be elected as a major was Ivo sometimes called the benefactor because of his valuable gifts to the unbelievably lordly major towns citizens.

One day before the election day Ivo the benefactor made a promise to the citizens of the town. In case of his victory in the elections he would ensure that on one of the paved streets of the town street lamps would be erected and that he would pay that with his own money. As thrifty as the citizens of the unbelievable lordly major town were, they elected him and one day after the elections they presented him their decision which street should have street lamps. Of course they chose not only the longest of all streets but renamed several streets so that a very long street in the town existed.

Can you find how long this street was? To be more specific, the situation is as follows. You are presented a list of all paved streets in the unbelievable lordly major town. As you might remember from problem CSTREET in the town the streets are paved in a way that between every two points of interest in the town exactly one paved connection exists. Your task is to find the longest distance that exists between any two places of interest in the city.


The first line of input contains the number of testcases t.
The first line of each testcase contains the number of places (2 <= n <= 50000) in the town. Each street is given at one line by two places (1 <= a, b <= n) and the length of the street (0 <= l < 20000).


For each testcase output one line which contains the maximum length of the longest street in the city.


1 2 3
2 3 4 
2 6 2
6 4 6
6 5 5


hide comments
Omar: 2017-10-16 19:32:19

Diameter of a tree with path coast , you just have to make sure of different test cases that will make you have wrog answer in case of not cleaning your array for every test case.

mohit_97: 2017-10-04 11:28:29

A simple double dfs technique to be used!

up79: 2017-06-09 08:21:20

a good one :)
spoiler : because its a tree so your target is to find the diameter of a tree :P
and there can be many test cases so dont forget to clear your vector list :)

divya_28: 2017-06-06 19:50:00

simple BFS+BFS!!

Shubham Jadhav: 2017-05-09 05:46:32

Since it is a tree, #edges = #nodes - 1

Last edit: 2017-05-09 05:51:00
gboduljak: 2017-02-16 15:25:31

problem shouldn't be categorized as shortest path

emtapcode: 2017-01-26 04:03:07

how to read input? how to know many edge?

vivace: 2017-01-19 22:10:38

why is question tagged as shortest path??

hasan356: 2016-12-29 19:17:17

solve first PT07Z....similar problem!!

chickadee: 2016-12-10 21:12:44

Is it allowed to visit same vertex twice? (i.e. add cycles to the path as long as no edges are traversed twice) I'm assuming the answer is "no" (a street cannot cross itself), but would be nice to clarify it in the problem statement.

Added by:Simon
Time limit:10s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL GOSU JS-RHINO PERL6
Resource:Ulm Algorithm Course SoSe 2005