Editorial for COCI '23 Contest 4 #3 Lepeze
Submitting an official solution before solving the problem yourself is a bannable offence.
Firstly, a few remarks: is the number of vertices in a polygon.
represents a diagonal with its
endpoints in vertices
and
. To avoid some edge cases, sides of the polygon are regarded as diagonals.
Basic observation is that removing a diagonal uniquely determines which diagonal must be added.
Let's focus on a particular, arbitrary triangulation that, without loss of generality, we are trying to
transform into a fan triangulation at vertex .
Lemma 1. If diagonals have an endpoint in vertex
, number of operations needed is
.
is obviously a lower bound. If
, we are done. Otherwise, there exists a vertex
such
that diagonal
is not present. Let
be the largest integer less than
such that there exists
a diagonal
. Observe that such an integer exists as diagonal
exists. Similarly, let
be the smallest integer larger than
such that diagonal
exists. Observe that such an
integer exists as diagonal
exists. Claim is that diagonal
exists. Namely, it is known
that every triangulation of every polygon has at least two "ears", where an "ear" is a vertex if it is an
endpoint of no diagonal. Now it is clear, by the way we defined numbers
and
, that vertex
is an
"ear" in a polygon with vertices
. If a vertex is an "ear", there exists a diagonal
between its neighbors, namely between
and
. Let's look at diagonals from vertex
. Let
be the
largest integer less than
such that diagonal
exists. Such integer exists as
and
diagonal
exists. Analogously, there exists a diagonal
. Now it is clear that
by removing diagonal
we must add diagonal
, that is it is possible to increase
number of diagonals with one of its endpoints in vertex
and we are done.
Let ,
and
be integers from previous paragraph. By "fixing" diagonal
, (at most)
two new intervals were created, namely
and
that have the same characteristics of an
interval
, such as having a unique diagonal that is possible to "fix". Observations so far motivate the
definition of the following graph: vertices are diagonals that need to be "fixed" together with a dummy
vertex. At the start, some diagonals are immediately "fixable" so we add a directed edge from dummy
vertex to these diagonals. When we "fix" one diagonal, we add a directed edge to new diagonals that
become "fixable". We created a directed acyclic graph of dependencies between diagonals. If there is a
path from vertex
to vertex
, it means that diagonal
must be "fixed" before diagonal
. Also, the
graph is a rooted tree with root being the dummy vertex and we are looking for the number of topological
orders of this tree. Next lemma proves useful with calculating this number:
Lemma 2. Given a rooted tree with vertices where
-th vertex has its subtree size
, number of
topological order is
.
This is a well-known fact, and as such, left as an exercise for the reader. For further information refer to the following Codeforces blog: link.
To efficiently calculate the product in the denominator, let's take a step back. We associate a diagonal
that must be removed with the diagonal that it "fixes". Using previous notation, we associate the diagonal
with the diagonal
. But, by the way we chose vertices
and
, every vertex
between them does not have a diagonal with its endpoint in vertex
and every such vertex corresponds
to a diagonal in the subtree of the diagonal
. Observing one diagonal
(
), we
see that it contributes to vertices
with a factor of
, and to all other
vertices (other than
and
) with a factor of
. If we use a segment tree that keeps a product of
numbers in an interval, the operation of removing one and adding another diagonal is a few updates in a
segment tree with complexity
if we precompute inverses. We calculate the number of diagonals
from each vertex explicitly and precompute needed factorials. Total time complexity is
or
depending on complexity of the precomputation of inverses.
Comments