Circumference of a tree, codeforces question. help

Can anyone help me with this question ?
basically, we need to find diameter of tree and multiply it by 3.
This is what I have tried.
to find diameter of a tree, we use dfs once and get node with is at longest path . Then we use dfs once again from the node which was last visited before, to find diameter of a tree. My problem is I’m not able to count the longest path taken in the second dfs.
Can anyone tell me how to do that ?
my submission gives wrong answer on testcase 5.

Hello @siddharth963,

Basically, what you have to do is select any arbitary node, say 1, then do a dfs and find the farthest node from 1. Then again u have to do a dfs from that node and find the farthest node from that point. In this way, you will get the diameter of the tree.

Here’s my code for reference : code

Also, your code is not visible. So if you still face difficulty just run your code on ideone and paste a link here.

I hope that helps, thank you :slight_smile:


THANK YOU, I was getting WA too many times, thanks a lot :smile: understood your code.
I forgot that we can’t view other users code. just updated link.

1 Like