Editorial for Google Code Jam '22 Round 1C Problem C - Intranets
Submitting an official solution before solving the problem yourself is a bannable offence.
We use terminology for graphs. The vertices are the machines and the edges are the
links.
Test Set 1
Let's consider the process of assigning priorities to the edges one by one, from the highest priority to the lowest. For simplicity, we number the -th highest priority as priority
so that the smaller the number, the higher the priority.
Suppose we have assigned the highest priorities to
edges, and we need to assign the priority
to a new edge. How can we do this?
We have three types of choices. Suppose the new edge with the priority is
. Let
be the set of vertices that the edges with highest
priorities connect with.
- If the vertices
and
don't occur in the previously assigned edges. I.e.
, the number of such edges is
. In this case,
and
form a new intranet. So, the number of intranets increase by
, and
equals
.
- If
, the number of such edges is
. In this case,
joins the intranet in which
is located. So, the number of intranets does not change, and
equals
.
- If
, the number of such edges is
. In this case, the edge is not activated. So, the number of intranets do not change, and
equals
.
Then, we can use a dynamic programming approach to solve Test Set 1. Let denote the probability that we assign the highest
priorities to the edges, the set of vertices introduced by these
edges has the size of
, and
intranets have been formed. The transition function can be deduced from the discussion above. The time complexity is
and it is enough to pass Test Set 1.
We can speed this up to by dropping
from the keys. Let
denote the probability that after assigning the highest
priorities for some
, the set of vertices introduced by these
edges has the size of
, and
intranets have been formed. The transition is to assign the highest priority among the edges of the types 1 and 2 above. The probability of there being a new intranet is
as we are no longer interested in priorities of the edges of the type 3 above.
Observing the graph
From the solution for Test Set 1, we see that each intranet corresponds to a pair of vertices such that both
and
activate the edge
. This fact can also be proved directly, and we describe it here.
Let's fix an assignment of priorities and consider the directed graph where the vertices are the machines and the edges are such that machine
uses the links connecting machines
and
. Since this graph is a functional graph (each vertex has outdegree
), each connected component contains exactly one cycle (and possibly some chains of nodes leading into the cycle).
A crucial observation is that the length of a cycle cannot be or more. Assume the contrary: that vertices
(
) form a cycle in this order, and let the priority of the link connecting
and
be
(indices are modulo
). Note that those links are distinct. Since machine
uses the link with highest priority, we have
. This gives us
, a contradiction. As a self loop is also impossible, we conclude that every cycle in the graph has length
.
Test Set 2
Let's call the set of edges activated by both of their endpoints an active matching as they form a matching. Now the problem is to compute the probability that the size of the active matching is exactly .
For a matching , let
be the probability that the active matching is
. Since it is not easy to compute
directly, we apply the inclusion-exclusion principle technique and so let
be the probability that the active matching contains
, that is,
. By inverting this, we have
. Our answer is then calculated as follows:
All that remains is to compute . Of course, this depends only on
, and we want to compute it when
, for each
. There are
possible orders of priorities assigned to
. Let's fix one of them and let the edges in
be
from the lowest priority to the highest. Then the condition that the active matching contains
is equivalent to, for each
, edge
has the highest priority among edges touching at least one of
. Hence we have:
Here we note that the denominators can be factorized to see that they are not divisible by . The number of matchings of size
is
, and this completes the
time solution (the divisions can be done efficiently by using the factorization, though this is not necessary).
In fact, we can find the answers for at the same time in
time: The transformation from
to
can be represented by a convolution and we can use FFT.
Comments