I was trying to solve problem E from last CF round and I got the idea that we can make a bridge tree(compress unbridged-connected nodes into a single node) and then find the diameter of the tree.
I saw some solutions that first find the nodes and compress them and then new dfs to find diameter as usual but I saw the shortest codes submitted and was amazed to see that it just contained a single dfs and was very neat.
I have two questions. Firstly how does this code work without actually finding diameter and how do people come up with such implementation? Do they first think about the whole implementation which is shortest and then code?