Editorial for COCI '21 Contest 3 #2 Cijanobakterije


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

One possible characterization of a tree is that it is an acyclic graph with n nodes and n-1 edges. This means that in the first subtask, the problem is actually to determine the length of the longest chain in a tree. This is a standard problem of finding the diameter of a tree. It can be solved by noticing that all nodes which are furthest away from an arbitrary node are the ends of some longest chain. Therefore, it is sufficient to take an arbitrary node of the tree, find a node which is furthest away from it, and then find the distance from this node to the node furthest away from it.

For the whole solution, one should notice that when connecting two trees, the diameter of the new tree can't be larger than the sum of their diameters (otherwise, we would have a chain in the first or the second tree which is larger than its diameter). On the other hand, a diameter which has the size of the sum of the two diameters is achievable because we can connect the ends of the two diameters. Therefore, by repeatedly connecting the trees, the largest diameter we can obtain is equal to the sum of the diameters of the individual trees, which is also the answer to the problem.


Comments

There are no comments at the moment.