tourist has pointed out this awesome comment to me, which shows that I did not really have a full understanding of what's going on when writing my solution. Indeed, let us look at the construction process of the new tree more closely and forget about future LCA queries for a second, just trying to keep the property that in the new tree the largest edge weight between any two vertices is the same as in the old tree. Then we can see that whenever we attach two subtrees to a newly created node, we don't really have to use the roots of the subtrees for that, and can use any node instead (because all edges within the subtrees have smaller weight than the currently processed edge, it does not matter which within-subtree edges will end up being used for the paths between the subtrees). And this allows us to keep all subtrees as chains, and connect them to form bigger chains, ultimately creating one big chain.
So it turns out that for any tree, we can construct a chain on the same vertices such that the maximum edge weight on the path between any two vertices is the same in the tree and in the chain, and of course the latter can be found using range-maximum queries. This chain is more or less equivalent to the inorder traversal of the auxiliary tree that was built in my solution, but I think constructing it explicitly is more beautiful and demonstrates a better understanding of the mechanics of the problem.
In an attempt to demonstrate an even better understanding of the mechanics, it seems to me that this construction is somewhat related to the Gomory-Hu construction. In fact, this construction seems to suggest that the Gomory-Hu tree can always be replaced by an equivalent Gomory-Hu chain! However, there seems to be no mention of a "Gomory-Hu chain" on the Internet, so I'm probably missing something here?
Update: Maxim Babenko has clarified the matters here: in the Gomory-Hu tree, for any pair of vertices not just the size of the minimum cut between them is equal to the size of the minimum cut in the original graph (as Wikipedia claims), but also the minimum cut itself (as a partition of the vertex set into two). In a Gomory-Hu chain the size of the cut is indeed still the same, but not the cut itself.
Thanks for reading, and check back next week!
Nicu
ReplyDeleteOn Topcoder: I found an obviously wrong (TLE) submission that simply used backtracking to find the path on the 400. SSRS_ was also in my room, so I figured I should make sure I got this challenge before them. I had a max size test case prepared with a longest path of length 27 so I used that -- unfortunately unsuccessful.
ReplyDeleteAfter SSRS_ passed me, I had two more challenge attempts to try to win which I took and unfortunately ended up in 2nd. Turned out the first submission I challenged was indeed TLE, I just didn't use the right test case.
On Codeforces: https://codeforces.com/blog/entry/71568
I need your help in this pseudocode in pseint
ReplyDeleteuna empresa maneja códigos numéricos con las siguientes características: cada código consta de cuatro dígitos: el primero representa una provincia, el segundo el tipo de operación y los dos últimos, el número de la operación. escriba un programa que lea de teclado un número de cuatro dígitos, y posteriormente imprima en pantalla la siguiente información: por ejemplo si el código es 5922 provincia 5 tipo de operación 9 número de operación 22 en caso de que tenga mas de 4 dígitos en lugar del mensaje anterior, habrá que imprimir en pantalla el siguiente mensaje de error: error código no valido. si tiene menos de 4 dígitos se suponen 0 los primeros.
Help please
Thank you