E - Minimum Swap 解説
by
spheniscine
Consider a directed graph of \(N\) vertices with edges \(i \rarr P_i\). Because each vertex has an indegree and outdegree of \(1\), this will form one or more components which are all cycles (including the trivial cycle consisting of a self-loop)
Consider what happens to the graph when you swap \(P_i\) and \(P_j\). If \(i\) and \(j\) were in separate cycles, their cycles will merge; if they were in the same cycle, that cycle will split into two cycles.
The identity permutation, which is our goal, would correspond to the graph consisting of all self loops. Given what we know about swapping values, it can thus be seen that \(K = N - \text {\#cycles}\). Additionaly, the operations we want to count has to decrease \(K\) by \(1\), which means that \(\text {\#cycles}\) must increase by \(1\). This only happens if we pick \(i\) and \(j\) to be from the same cycle. Therefore we find all the cycles, and for each cycle size \(c\) we add \({\large \binom c 2} = {\large\frac {c (c-1)} 2}\) to the answer.
投稿日時:
最終更新:
