Can someone help me in solving this question [BOI16_swap](https://oj.uz/problem/view/BOI16_swap). ↵
↵
<spoiler>↵
I understood that the problem can be solved with dynamic programming with dp[i][j] = minimal permutation of nodes in subtree of i if node i has been set to j. But I am not understanding how is the complexity $O(nlog^2n)$ and also how to efficiently implement this solution. Also there seems to be recursive bruteforce solutions, it would be nice if someone can explain that.↵
</spoiler>↵
↵
Thanks!
↵
<spoiler>↵
I understood that the problem can be solved with dynamic programming with dp[i][j] = minimal permutation of nodes in subtree of i if node i has been set to j. But I am not understanding how is the complexity $O(nlog^2n)$ and also how to efficiently implement this solution. Also there seems to be recursive bruteforce solutions, it would be nice if someone can explain that.↵
</spoiler>↵
↵
Thanks!