Editorial for COCI '19 Contest 1 #4 Trobojnica
Submitting an official solution before solving the problem yourself is a bannable offence.
In order to score of the points, we need to find the necessary and sufficient conditions for the patriotic triangulation to exist.
Claim 1: In each triangulation with there are at least two "ears", i.e., triangles that share two sides with the input polygon.
Sketch of the proof: Induction. Claim holds for a square. Let a diagonal divide the polygon into two smaller polygons. If one of them is is a triangle, that is an ear. Otherwise, we make an inductive argument.
Let there be , and sides with colors , and respectively. It obviously holds that . It is also clear that because otherwise no ear would exist.
Claim 2: If a patriotic triangulation exists, numbers have the same parity.
Sketch of the proof: Double counting the number of edges colored across the triangles:
From which we conclude that has the same parity as . The same holds for and by analogy.
The aforementioned conditions are also sufficient; this is easiest to prove by a constructive algorithm which solves this task.
In short, we will inductively build ears using the sides of two different colors without changing (prove it!) the equal parities of numbers in a new polygon which is missing a side. We also need to watch out whether the new polygon is monochrome.
One nice implementation is as follows:
For each vertex of a polygon we will remember its successor in the current polygon and the edge color which connects them. Searching for a spot to build a new ear is, in fact, simple. We will just circle around and build an ear on a vertex whose predecessor and successor edges are of the same colors and at least one of those colors is the one that is most frequent among the current polygon sides. If each time we start from the place where we've built our last ear, it can be proven that the complexity is an amortized .
Implementations with time complexity of should also receive full score on this problem.
Comments