Statement
Given an undirected weighted tree on 𝑛 nodes, find a permutation 𝑝 which maximises ∑1≤𝑢≤𝑛dis(𝑢,𝑝𝑢). If there are multiple such permutations, find the lexicographically smallest one.
Solution
Observation 1:
In the optimal permutation, paths (𝑢,𝑝𝑢) and (𝑣,𝑝𝑣) intersect for every pair of nodes (𝑢,𝑣).
Proof
Assume that the two paths do not intersect for some optimal 𝑝. If we swap 𝑝𝑢 and 𝑝𝑣, the two paths now intersect and the score strictly increases. So 𝑝 wasn't optimal.
Observation 2:
In the optimal permutation, there exists a node 𝑐 such that all paths (𝑢,𝑝𝑢) cover 𝑐.
Proof
- We know that every pair of paths must intersect.
- Let us root the tree at node 1.
Now, there can be atmost one child 𝑣 such that there exists some path in the subtree of 𝑣 which doesn't come out of the subtree (touch 1) (Since if there are multiple such child nodes, the paths completely contained in their subtrees wouldn't intersect).
-
If there is no such child node, then 1 is the node 𝑐.
Otherwise, if there is such a child node 𝑣, we travel down to it and repeat the same process.
Since this traversal definitely ends at some point of time (it will eventually travel down to a leaf where there will trivially be no such child 𝑣), we will always find such a 𝑐 for an optimal permutation 𝑝.
Now, if we fix such a node 𝑐, the score of the optimal permutation will simply be 2 ⋅∑1≤𝑢≤𝑛dis(c, u). It should be pretty easy to see why this is the case.
Let us assume that we know the node 𝑐 which is to be covered by all paths for now and worry about neither lexicographic minimization, nor maximization of ∑1≤𝑢≤𝑛dis(c, u). Let's just focus on finding some valid permutation.
We root the tree at 𝑐. Now, all that remains is to find a permutation 𝑝 such that lca(𝑢,𝑝𝑢) =𝑐 ∀ 𝑢 ≤𝑛.
We define rep(𝑢) to be the first node on the path from 𝑐 to 𝑢. So for every 𝑢, a restriction on 𝑝𝑢 is that rep(𝑢) ≠rep(𝑝𝑢).
Generalization
This problem can now be generalized to the following:
You are given a complete bipartite graph 𝐺(𝑉,𝐸) (defined on equally sized partitions 𝐴 and 𝐵), and some groups of nodes 𝑆1,𝑆2,…𝑆𝑥 such that 𝑆𝑖 ⊆𝐴 ∪𝐵, 𝑆𝑖 ∩𝑆𝑗 =∅ ∀ 𝑖 ≠𝑗 and 𝑆1 ∪𝑆2⋯ ∪𝑆𝑥 =𝑉. Find a perfect matching on 𝐺 such that there is no edge between two nodes which belongs to the same group of nodes 𝑆𝑖.
How do we solve this problem? Let's think about when the problem is solvable first.
We observe that a necessary condition for being able to find a perfect matching in this graph is the following:
For every group 𝑆𝑖, we must have |𝑆𝑖 ∩𝐴| ≤𝑛 −|𝑆𝑖 ∩𝐵| ⟹ |𝑆𝑖 ∩𝐴| +|𝑆𝑖 ∩𝐵| ≤𝑛 ⟹ |𝑆𝑖| ≤𝑛 (where 𝑛 =|𝐴| =|𝐵|).
Proof
Assume that |𝑆𝑖 ∩𝐴| >𝑛 −|𝑆𝑖 ∩𝐵|, this would mean that it wouldn't be possible to assign a valid node from 𝐵 to every node in |𝑆𝑖 ∩𝐴|. Therefore, a perfect matching wouldn't exist.
The condition |𝑆𝑖| ≤𝑛 ∀ 𝑖 is also sufficient for the existence of a perfect matching.
Proof
Let's assume that the condition is initially satisfied. Now, we can always remove a pair of nodes
(𝑎,𝑏),𝑎 ∈𝐴,𝑏 ∈𝐵 from
𝐴 and
𝐵, and add an edge from between them, such that the condition is satisfied for
𝑛 =|𝐴| −1 too (it holds as an invariant after every move).
How to remove such an edge? We only have to ensure that any 𝑆𝑖 with 𝑆𝑖 =|𝑛| has a vertice removed from it. There can be at most two such groups.
Cases:
- Two such groups: remove one vertex from both of them (such that one belongs to 𝐴 and one belongs to 𝐵). It's always possible to remove such a pair.
- One such group: remove one vertex from this group and one from any other group.
- No such group: remove a pair of vertices from two different groups
We repeatedly perform this operation until all sets are empty, and obtain a perfect matching.
Back to our problem
In our problem, we have the set of permutation indices as 𝐴, the set of permutation values as 𝐵, and the groups of nodes are formed by partitioning nodes on the basis of rep(𝑢).
Let us recall that:
- We don't know what 𝑐 is.
- We have to maximise ∑1≤𝑢≤𝑛dis(c, u).
- We have to lexicographically minimize 𝑝.
It's easy to see that 𝑐 becomes (semi) uniquely defined as (one of) the centroid(s) of the tree because of the necessary condition. This can be easily proved and is left as an exercise to the reader. Maximization becomes meaningless now, and seems ironic because ∑1≤𝑢≤𝑛dis(c, u) is actually minimized when 𝑐 is the centroid.
Now that we know 𝑐, and a necessary and sufficient condition for the existence of a perfect matching given some group restrictions, we can easily find a lexicographically minimum permutation in the following way:
Iterate over indice i from 1 to n:
Find the smallest value v such that rep[i] != rep[v] and the invariant holds if we remove the pair (i, v)
Set p[i] to v
Finding the smallest such 𝑣 can be easily accomplished by using a set and doing some casework. I will not discuss it here because its trivial. There is an annoying edge case wherein we are allowed to have 𝑐 =𝑝𝑐, so take care of that.
My submission can be found here.