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
tanjiro_59: 2021-10-11 07:26:58

Thanks for your comments. AC at the first time using double BFS to find Diameter of Tree

Last edit: 2021-12-13 08:43:27
musky: 2021-08-06 00:15:24

Boom!!! I'm a faggot.

Last edit: 2021-08-06 01:55:49
karan2604: 2020-09-09 21:02:33

where i can find editorial of spoj problems...??

fuadul_hasan: 2020-06-15 09:58:31

any one plz tell me why its work . or suggest me any editorial plz

gaur_gaur: 2020-05-31 07:59:21

AC in one go :)
just double bfs easy

gaur_gaur: 2020-05-31 07:58:27

AC in one go :)

mdyamin: 2020-03-30 19:53:35

Multiply -1 to edges weight's then run any shortest path algorithm then multiply the result with -1 again. That's why it is tagged as shortest path though we have to find longest path.

abhi1999: 2020-03-29 10:38:19

Run a bfs from any starting node and find the node at largest distance.From that node again run a BFS and again find the distance to the farthest node.this new distance is the longest path in the graph.
Since this is a tree,therefore to any node there is only one path.thus BFS is same as DIJSKTRA.
that's y it is tagged as == > #shortest-path #dfs

can anyone tell me why this aproach is working

purplecs: 2019-11-24 18:51:25

Ac in one go ;';

dungnh516: 2019-07-03 18:19:45

double BFS --> Easy man

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