G - Degree Harmony 2 Editorial
by
fanchuanyu
Editorial for printing
A slow solution of G
First, to handle the constraint that the degree of the \(i\)-th vertex is at most \(a_i\), we can duplicate the \(i\)-th vertex \(a_i\) times and attempt to model this problem using maximum matching in general graphs.
For an edge from \(u\) to \(v\), we can connect every copy of \(u\) to every copy of \(v\), but only one such edge can be selected in total. To enforce this, we introduce two additional vertices, \(a\) and \(b\).
Vertex \(a\) is connected to all copies of \(u\), vertex \(b\) is connected to all copies of \(v\), and there is also an edge between \(a\) and \(b\). If the edge between \(u\) and \(v\) is not selected, then the edge between \(a\) and \(b\) will be chosen in the matching, contributing one matching edge. If an edge between a copy of \(u\) and a copy of \(v\) is selected, then \(a\) will be matched to one copy of \(u\), and \(b\) will be matched to one copy of \(v\), contributing two matching edges.
Therefore, by running a maximum matching algorithm on this new graph and subtracting \(m\) (the number of original edges), we obtain the answer.
Although the Blossom algorithm theoretically has a high time complexity, its constant factor is small, and it actually ran in only 96 ms.
posted:
last update:
