PT07Z - Longest path in a tree

You are given an unweighted, undirected tree. Write a program to output the length of the longest path (from one node to another) in that tree. The length of a path in this case is number of edges we traverse from source to destination.

Input

The first line of the input file contains one integer N --- number of nodes in the tree (0 < N <= 10000). Next N-1 lines contain N-1 edges of that tree --- Each line contains a pair (u, v) means there is an edge between node u and node v (1 <= u, v <= N).

Output

Print the length of the longest path on one line.

Example

Input:
3
1 2
2 3

Output:
2

Added by:Thanh-Vy Hua
Date:2007-03-28
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Co-author Amber

hide comments
2015-07-12 23:55:11
DFS and DFS :D
2015-07-12 21:57:35 Ankur Arora
kindly
d
o
n
t
see
comments

for
solving this
you should know
famous
graph
algorithms
.
.
.
.
.
still
.

.
.
2015-07-03 17:20:14
Ohh yeah (BFS & BFS) :D
2015-06-29 12:25:03 Deeksha
take 1 as start node..

Last edit: 2015-06-29 20:39:20
2015-06-25 10:13:11 karan
(BFS&BFS) :D

Last edit: 2015-06-25 10:13:34
2015-06-21 20:32:12 Sukeesh
BFS + BFS ! .. AC in one go .. :)
2015-06-19 12:36:31 Ankush
BFS + BFS :D
2015-06-15 16:21:01 BALMUKUND SINHA
getting tle
2015-06-12 19:43:18 Anant Kumar
this is a good problem
2015-04-08 01:51:00 Mitch Schwartz
That's not true either. I think what you mean to say is that it implies that if the test data is not wrong, then it is incomplete. (This is ignoring the possibility of a faulty judge, for simplicity.)

Weak test data is of course a valid quality concern. There are various factors to consider when it comes to that. I don't want to go into details at the moment, but consider some examples of weakness:

1) test data allows a greedy approach to pass, even though that approach will not always produce correct answers.
2) constraints state n <= 10^8, but in the test data we actually have n <= 10^6.
3) test data is missing the case n=0.
4) perfect squares are a tricky case, but they are not included in the test data.
5) test data was generated randomly according to uniform distribution, leading to a situation where 99% of cases can be solved in a trivial way while the other 1% are hard.

I think it's important to consider the nature and severity of the weakness before changing the test data or requesting for it to be changed. Also, consider that many problems were published many years ago, and the problem setter may be inactive. In some cases, it could be a good option to create a new and improved version of the problem instead of trying to modify the old one.

Last edit: 2015-04-08 02:03:28
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.