Please read the new rule regarding the restriction on the use of AI tools. ×

Pranshu_Pandya's blog

By Pranshu_Pandya, history, 12 months ago, In English

Hello everyone!

I recently come across an interesting tree problem.

There is an undirected tree, T, with edges of length 1. Initially, each of the nodes stores a value equal to the node number. Shuffle these values among the nodes in such a way that no node stores a value equal to its node number. There are two values to determine:

1) The minimum possible sum of the distances between the initial and final positions of all values.

2) The maximum possible sum of the distances between the initial and final positions of all values.

1<=number of nodes<=1e5.

I was able to solve the case of minimum possible sum by applying maximum matching. But I am not able to come up with an efficient solution for 2nd case. I am also struggling to prove its correctness for the n^2 solution(my intution is repatedly finding diameter and swapping the node at diameter and removing them after adding their distances). I discussed the probelm with many of my friends(some are CMs) but we are not able to come up with efficient algo for second case.

It would be great if someone can help in solving the second case.

  • Vote: I like it
  • -11
  • Vote: I do not like it

»
12 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Someone just asked about the same problem here on CF a week or two ago.

The problem is the same as BOI 2020 Village (Minimum) and BOI 2020 Village (Maximum).

You can read the tutorial here.