Problem Link:
author: Tasnim Imran Sunny
tester: Roman Rubanenko
editorialist: Utkarsh Lath
Difficulty:
Hard
Pre-requisites:
inclusion-exclusion, dynamic-programming, combinatorics, bipartite-graphs
Problem:
Given 3 * N matrix, whose first two rows are filled with permutations of [1…N], and third row is empty, find the number of ways of filling the third row such that each number(between 1 to N) appears at most once in each row and each column. The part of matrix already filled satisfies this property.
Explanation:
The current problem can be translated to finding the number of perfect matchings in a given (N-2)-regular N*N bipartite graph.
The vertices on left hand side of bipartite-graph represent positions in the third row. The vertices on the right hand side represent colors that can be filled into some position. The edges are assignments compatible with the first two rows (i.e. if first row is array A* and second row is array B*, then there is an edge from i to all colors except A* and B*).
Coloring the third row of our matrix is equivalent to choosing exactly one edge(from the above graph G) incident to each position. The manner in which we have put edges ensure that C* ∉ {A, B}*. To make sure that C ≠ C[j], the edges chosen should form a perfect matching.
Therefore, we need to find number of perfect matchings in a given (N-2)-regular bipartite graph G.
Let me define another related graph G’ which has same set of vertices as G, and all left to right edges that are not in G (i.e. vertex i on LHS has edge to vertices A* and B* on RHS).
The first step towards solution is realizing that the graph G’ has very simple structure. Every vertex has degree 2, so it is a union of disjoint cycles. If suppose we were asked to count number of matchings in G’, it would be far simpler. Given the size of all cycles of G’, the number of k-matchings can be found with a simple DP(details given later). Let ϕ(k, G’) denote number of k-matchings in G’.
Let F(k) = The number of perfect matchings in G U G’ such that exactly k edges belong to G’ and N-k edges belong to G.
Any such matching consists of a k-matching in G’. So, can we use the value of ϕ(k, G’) ?
The only problem in using ϕ(k, G’) is that we dont know the number of ways to match the remaining vertices only using edges from G. To overcome this, we will consider all matchings of the remaining vertices in G U G’ (which is (N-k)! in number), and then remove those where more than k edges are used from G’.
F(k) = ϕ(k, G’) * (N-k)! - F(k+1) * (k+1 choose k) - F(k+2) * (k+2 choose k) … - F(N) * (N choose k) --(1)
Justification:
ϕ(k, G’) * (N-k)! is the number of perfect matchings in G U G’ where
- k or more edges are chosen from G’
- some k of the matched edges that belong to G’ have been marked (these marked edges come from ϕ(k, G’) term, and the rest come from (N-k)! term).
Therefore, the perfect matchings where exactly k+i edges are used from G’ are counted (k+i choose k) times in the term ϕ(k, G’) * (N-k)!, hence the above recurrence.
If ϕ(k, G’) can be built, then (1) above can be used to find our solution (= F[0]) in O(N^2) time.
Constructing ϕ(k, G’)
ϕ(k, G’) = number of matchings of size k in G’, is coefficient of x^{k} in matching polynomial of G’.
Denote
C(k, n) = number of matchings of size k in a cycle graph with n vertices.
L(k, n) = number of matchings of size k in a line graph with n vertices.
Then we have
L(k, n) = L(k-1, n-2) + L(k, n-1)
In any k-matching, either
- Edge (0,1) is used. The number of ways of matching remaining edges is L(k-1, n-2).
- Edge (0,1) is not used. The number of ways of matching remaining edges is L(k, n-1).
C(k, n) = L(k, n) + L(k-1, n-2)
Because, in any k-matching, either
- the edge (0, 1) is used. Number of ways of matching remaining edges is L(k-1, n-2)
- the edge (0, 1) is not used. Number of ways of matching remaining edges is L(k, n)
Once we construct the arrays C(k, n), ϕ(k, G’) can be found as follows:
Let G’ be a union of l cycles of sizes n_{1}, n_{2}, n_{3} … n_{l}.
Consider any k-matching of G’. It will be a set of k edges of G’, where no two edges have same end point.
The connected components of graph G’ are the cycles. If we pick a matching of size k_{i} from cycle n_{i} and matching of size k_{j} from cycle n_{j}, then the union of these two matching(a matching is a set of independent edges) is also a matching, and its size is k_{i}+k_{j}.
The same idea can be used backwards to say that any k-matching of G’ will consist of a matching of size k_{1} in cycle n_{1}, matching of size k_{2} in n_{2}, …, a matching of size k_{l} in n_{l}, such that k_{1} + k_{2} + … + k_{l} = k. for a given set of values of k_{i}’s, the number of k-matchings of G’ which uses k_{i} edges from cycles n_{i}, is Π ^{l}_{i=1} C(k_{i}, n_{i}). The total number of k-matchings of G’ is
Σ_{k1+k2+…+kl=k} Π ^{l}_{i=1} C(k_{i}, n_{i})
It is easy to see that if C(n) = Σ ^{n}_{k=1} C(k, n) x^{k} is the matching
polynomial of cycles of length n, then
coefficient of x^{k} in C(n_{1}) * C(n_{2}) … C(n_{l}) is Σ_{k1+k2+…+kl=k} Π ^{l}_{i=1} C(k_{i}, n_{i})
Therefore,
ϕ(G’) = Σ ^{n}_{k=1} ϕ(k, G’) x^{k} = C(n_{1}) * C(n_{2}) … C(n_{l})
The term Σ ^{n}_{k=1} ϕ(k, G’) x^{k} is called matching polynomial of graph G’.
In fact, it can be stated in general that if ϕ(G) is matching polynomial of graph G, then
ϕ(G_{1} U G_{2} U G_{3} …) = ϕ(G_{1}) * ϕ(G_{2}) * ϕ(G_{3}) …
The polynomials C(n) can be precomputed in O(N^{2}) time, and the matching polynomial for ϕ(G’) can be constructed in (O(n^{2}) time. The coefficient of x^{k} in ϕ(G’) is ϕ(k, G’).