I am getting TLE on test 24. How will you choose from which town to start? Because of this I had to start from every town to find the answer. For each town it took me O(n),so for all the towns it becomes O(n^2), causing TLE.
i decompose graph into connected components (by one dfs) after that i count diameter of each component(by one special_dfs) and refresh global_variable if need. my programm works at all my tests and sample test too.