eolymp
bolt
Try our new interface for solving problems
Problems

Triangle Count Sequences of Polygon Triangulatio

Triangle Count Sequences of Polygon Triangulatio

A \textit{Triangulation} of a polygon is a set of lines between vertices of the polygon that divide the polygon into triangles. The lines must be inside the polygon and may not intersect each other except at vertices of the polygon. For example: \includegraphics{https://static.e-olymp.com/content/86/866d54d9171ba16de4eac726851303285f906fb4.jpg} In the examples, the heavy line is the outline of the polygon and the lighteg lines give the triangulation. In each case a triangulation of a polygon with \textbf{n} vertices will result in \textbf{(n-2)} triangles. The \textit{triangle count sequence} of a triangulation is obtained by starting at some vertex, proceeding around the polygon and for each vertex of the polygon recording the number of triangles of the triangulation incident of that vertex. In the examples above the triangle count sequences starting at the top (left) are: \textbf{A: 1 3 1 3 1 3} \textbf{B: 2 2 1 4 1 2} \textbf{C: 1 2 3 2 1 6 1 2 3 2 1 6} Write a program which takes as input a sequence of \textbf{n }positive integers and determines whether the sequence is the triangle count sequence of a polygon of \textbf{n }vertices. If so, list the triangles of the triangulation in lexicographical order. \InputFile The first line contains the number of data sets \textbf{p }(\textbf{1 }≤ \textbf{p }≤ \textbf{1000}). Each data set shoul be processed identically and indenpendently. Each data set consists of a single line in input. It cintains the data set number \textbf{k }followed be the number, \textbf{n} (\textbf{4} ≤ \textbf{n} ≤ \textbf{20}) of integers in the sequence, followed by \textbf{n }integers of the sequence in order, all separated by a single space. \OutputFile For each data set there may be multiple lines of output. If the input sequence is \textbf{NOT} the \textit{triangle count sequence} of a polygon of \textbf{n }vertices, the single output line consists of the data set number \textbf{k}, followed by a single space followed by the (captail) letter \textbf{N}. Otherwise (the input sequence is the \textit{triangle count sequence} of a polygon of \textbf{n} vertices), the first output line consists of the data set number \textbf{k}, followed by a single space followed by the (captail) letter \textbf{Y}. Following this line are (\textbf{n-2}) lines each containing the indices of the vertices of a triangle of the triangulation, one triangle per line, with the vertex indices in increasing order. The triangle should be sorted in \textit{lexicographic} order least first. That is, k < \textbf{l} < \textbf{m} are the vertex indices of one triangle and \textbf{r} < \textbf{s} < \textbf{t} are the vertex indices of another, then the first precedes the second in \textit{lexicographical} order if: \textbf{k < r OR (k == r AND l < s) OR (k == r AND l == s AND m < t)}
Time limit 1 second
Memory limit 64 MiB
Input example #1
4
1 6 1 3 1 3 1 3
2 6 1 3 2 2 1 3
3 12 1 2 3 2 1 6 1 2 3 2 1 6
4 20 1 2 3 2 1 6 1 2 3 2 1 6 1 3 2 2 1 3 1 2
Output example #1
1 Y
1 2 6
2 3 4
2 4 6
4 5 6
2 N
3 Y
1 2 12
2 3 12
3 4 6
3 6 12
4 5 6
6 7 8
6 8 9
6 9 12
9 10 12
10 11 12
4 N
Source 2013 ACM Greater New York Region, October 27, Problem G