Editorial for DMOPC '20 Contest 7 P6 - Maou and Division
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
First, let's consider the generalization of this problem to any weighted graph :
find the maximum, over all 2-colouring of vertices,
of the minimum weight edge between two vertices of the same colour.
Let us define to be the subgraph of
with all edges with weight at most
.
If
is bipartite, then by using the 2-colouring,
we know the answer for
is strictly greater than
.
So, the answer is the minimum
such that
is not bipartite
(i.e. has an odd cycle).
There are multiple ways to solve this problem for graphs in time.
- Binary search combined with a linear-time check on if a graph is bipartite.
Find the minimum spanning tree (MST), and use the 2-colouring of the tree. Let
be the answer we get with this algorithm, and
, be the actual answer. Clearly
.
However, we determined
by finding an edge of weight
between two nodes of the same colour. Using the path in the MST, this results in an odd cycle with all edges having weight at most
, so
. Therefore
.
We can use a variant of Kruskal's algorithm with an augmented disjoint-set data structure. The data structure needs to maintain a locally valid colouring of each component. This is a relatively standard idea with multiple ways to implement it.
We add the edges in increasing order of weight, and using the data structure, we stop as soon as we have added an odd cycle.
For this subtask, it should be enough to use any of these approaches
on the complete graph with weights equal to distances.
The time complexity is .
Subtask 2
This subtask is intended for solutions that have complexity
with a high constant factor.
Here I will present an approach that is ideal if you have access to a book of template geometry code. In this solution, you can use the relevant geometry algorithms as a black-box without understanding how they work.
The approach is to use the method we proved earlier
of finding the minimum spanning tree,
and using its 2-colouring.
Given points in the plane, it is a standard problem to find
the Euclidean MST.
A way to solve it is to use the fact that the Delaunay triangulation
is guaranteed to contain the Euclidean MST.
Since there is an algorithm to find the Delaunay triangulation
in
time (albeit with a high constant),
we can find the 2-colouring in
.
The last step is to find the closest pair of points for each colour.
Again, we can use a black-box
algorithm for this.
Subtask 3
There are several ways to solve the problem fully.
The general approach is to efficiently find a subgraph
with edges that contains
for some
,
so that
is not bipartite.
Then, we can run one of the
graph algorithms
to solve the problem in
time.
The hard part is finding a sufficiently high
such that
only has
edges, and doing this efficiently.
Indeed, it is not obvious why this is guaranteed to be possible with only
edges!
A good place to look for inspiration is in algorithms
that find the closest pair of points in time.
They use an important property:
in a
square,
there can be at most
points
before you get a pair within distance
of each other.
We can extend this to the following property:
in a square,
there can be at most
points
before there is a triple of points such
that all edges of the resulting triangle
have length at most
.
This is useful because the existence of such a triangle
implies that
is not bipartite.
There are many ways to exploit this property,
but here I will just describe one solution.
The algorithm extends the line-sweep
algorithm for finding the closest pair of points.
It maintains a distance that can only decrease,
and a set of all points within distance
from the sweep-line.
Given a point on the sweep-line,
we can efficiently find all points within
distance
of the point.
If we maintain
correctly,
there will be at most
such points.
Then in
time we can loop through the points
and see if there is a triangle with a smaller maximum edge length.
If so, then we update our distance
accordingly.
As we go, we can also directly construct our graph
by adding edges from the point to every
other point within distance
.
This algorithm runs in
time.
Of course, there are other ways to find a good distance
and efficiently construct
.
To pass this subtask, it is important to both
have time complexity
and a good constant factor.
Comments