I try it using a BFS. Starting in a node with degree 1, setting the node as visited and its neighbors, adding in the queue the neighbors of neighbors of node. But, i was received only wrong answer with this strategy :(
Link for the problem: http://www.spoj.com/problems/IMPER/
sorry for my bad english.
Your approach will lead to WA, consider the following test case:
10
1 2 3 4 2 6 7 8 9
-1
What is the right answer?
To solve this problem you need to learn what is and how to calculate the diameter of a tree. I won't link any tutorial, since I don't know about any particularly good, but googling it should be enough.
This is the code I got Accepted with: Don't click me
oow, thank you so much for advice.
After looking for the diameter of a tree, this problem look so easy xD
Thanks very much :D
I got Accepted in 2 OJ and there is one thing I can't understand:
Why does this case return 2 instead of 3?
7
1 2 1 4 1 6
Thanks!
Damn!
I was simulating the problem in a wrong way.
Thanks a lot!
One of the tags of this post is "need help" XD .