The problem setting is exactly same as the perfect matching. That is, find a subset of edges, there are no edges have common nodes and all nodes are covered by one edge. Since the graph is general, Blossom algorithm which can provide the maximum matching in O(VE) time is enough.
Here, I would like to introduce a simpler way to solve this problem. Of course, it is not some strange greedy algorithms which may passed because we can’t design cases for all wrong solutions (contestants are always creative :D). Anyway, let me introduce the simple solution – Tutte matrix. We can randomly choose the X[i][j]. Since we only need to check whether its determinant is zero, we can module all number during Gaussian Elimination (used to calculate the determinant) by a big prime, such as 10^9 + 7. Due to the randomness, we can solve this problem with a high probability. This solution is O(V^3 log MOD), where MOD is the big prime number we chosen. O(log MOD) is caused by the calculating the inverse or elimination by subtracting.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.
Thanks for this very interesting problem and editorial… It was a joy to attempt to solve this problem during contest time and it is even better to read editorial and learn that there was a simple solution after all, which even used a standard algorithm.
I’m very interested now in reading more about the Blossom Algorithm and how it works so I can try to adapt it to solve this problem.
Do you happen to know of better resources to read about Blossom Algorithm besides wikipedia page?
I wanted to try Hungarian algorithm in this problem, but first had no time, and then got stucked with step 5, “Cover the zero elements with the minimum number of lines it is possible to cover them with.”, but to do this I have to solve matching problem as a subproblem (recursion?)…
I tried following strategy (though I never got it correct). Can anybody point out why is this algorithm incorrect? What is wrong?
Before doing anything ensure that total no. of vertex is even otherwise output NO.
Lets work on a single connected component of our graph. The same procedure can be applied to all the components.
Find out all the leaf nodes (vertex with degree 1)
Now keep on removing the leaf edge one by one. Since, each leaf node(node with degree 1) can only be matched with the node it is allready connected with. So, at each step we will remove 2 nodes from the graph. So, the no. of nodes in the graph will always remain even. (This step may give arise to more leaf nodes. They must also be removed)
Repeat step 2 until following conditions are met:
If no leaves are left then we either have exhausted the graph or all we are left with a cycle or vertices with degree 0.
If the graph is exhausted then we are done(current component is matchable).
If any vertex with degree=0 left output NO.
If a cycle left then find the length of the cycle, If the length is odd then output NO otherwise the current component is matchable.
Something like this pictorially :
This is a very simple example. Here vertex 1 & 8 are the what I called leaf nodes. And edges {(1, 2), (7,8)} are what I called as leaf edges.
Why is this strategy incorrect?
If we try to find if each of the connected components in the given graph has a Hamiltonian Path or not and also check if each of the components have even number of vertices, will this suffice to solve the problem ? Is this approach correct ?
I didn’t use a prime modulus or anything like that. I just chose random integers as variables of the Tutte matrix, and did the GEM on doubles. Chances are small that there’d always be an undetectably non-zero (or zero) solution
So I just try this an (insert constant here) amount of times and decide if the determinant can be non-zero.
That being said, this problem would’ve done better in a short contest. It’s hard to make worst-case test data, so wrong/slow solutions could pass, I think. Also, I solved it using Google Search algorithm, where I googled “perfect matching general graph” and got to Tutte matrix in no time. Overall, deciding if there’s a perfect matching in a graph sounds like a standard problem (and therefore solvable with hardly any effort by finding the solution), so it is a bit of a waste.
I think this problem tested google searching skills more than the programming knowledge. I know this was not intended but all one had to do to solve this problem was to figure out that it is a maximum matching problem in general graphs, search the same, find out about blossom algorithm and then search blossom algorithm code. Nevertheless it led me to many new algorithms and concepts. I read in norbert blum’s paper how a O(m*root(n)) complexity algorithm can find maximum matching in a general graph. If anyone solved the problem using this, please share your code.
what i could derive from other contestants solution that they were selecting vertex having minimum size and then marking it,can somebody expain this concept to me as editorial does not clarifies the concept clearly(atleast to me)?
Most of the accepted code used maximum matching for bipartite graph algorithm… But there may be general graph in test case so how they were passed…
I too did a lot of Google searching for this problem but couldn’t clearly follow the blossom algorithm so tried brute forcing and much to my surprise it got accepted. Initially check if the number of vertices are even. If true, then check if any vertex has degree less than one. If all vertices have degree greater than zero, then sort the vertices based on their degrees and then try to find a solution by removing edges. Try this for every possible combination until a certain set of edge removals satisfy.
The Hungarian algorithm is for bipartite graphs only (how do you convert a matrix to a multipartite graph, anyway?). And bipartite matching can be solved in O(N^3) using network flows, which is much simpler to code.
My idea was to have 2N vertexes let say v1 and v2 for each v, and there is edge between v1[i] and v2[j] iff there is edge v[i] v[j] in original graph…
I had initially thought of an approach exactly like yours. However i later realized that the the graph remaining is guaranteed to have atleast 1 cycle and will not always have exactly 1 cycle.There are many cases in which the remaining graph will consist of multiple cycles joined together for which your algorithm fails.
The approach will be correct however finding a Hamiltonian path in a graph is an NP-complete problem so it would have been difficult to get it accepted within the time limits.