Triangulation
Time limit3sMemory limit128 MB
Given a triangulated colored convex polygon, find the maximum number of triangulation diagonals that can be cut without separating same-colored triangles.
- Level
Medium6 of 10
- Topics
- Union-find, Graph, DFS
- Solved
- No attempts yet
Problem
In computational geometry, a triangulation is a set of triangles satisfying the following two conditions:
- The three vertices of every triangle are vertices of the given polygon.
- No two triangles overlap in their interiors, and the union of all triangles equals the whole polygon.
A convex polygon is a polygon with at least three sides in which every interior angle is less than 180 degrees. A straight line that splits a convex polygon into two convex polygons is called a cut line of that polygon.
You are given a triangulated convex polygon in which the interior of each triangle is painted with a color . You want to draw as many cut lines as possible so that no two points of the same color are ever separated into different pieces. What is the maximum number of cut lines you can draw?
If a cut line passed through the interior of a triangle it would split two same-colored points of that triangle, so every cut line must coincide with exactly one of the internal diagonals of the triangulation.
Input
The first line contains the number of polygon vertices . ()
Each of the next lines describes one triangle with four integers : the triangle has polygon vertices , , as its three corners, and its interior is painted with color . ()
The given input is always a valid triangulation satisfying the conditions above.
Output
Print, on a single line, the maximum number of cut lines that can be drawn.