Under construction

Still being built. Things may change

Triangulation and Triangle Counts

Time limit1sMemory limit128 MB

Summary
Decide whether a number sequence matches the per-vertex triangle counts of some polygon triangulation and print its triangles.
Level

Medium7 of 10

Topics
Backtracking, Graph, Combinatorics
Solved
No attempts yet

Problem

A triangulation of a polygon is a set of segments joining vertices that cuts the polygon into triangles. Every segment has to lie inside the polygon, and no two segments may meet anywhere except at a vertex. Triangulating a polygon with nn vertices always produces n−2n-2 triangles.

Start at one vertex, walk around the boundary, and write down how many triangles touch each vertex in turn. The list you get is the triangle count sequence. All three sequences below are triangle count sequences. A and B come from a polygon with 6 vertices, C from a polygon with 12 vertices.

  • A: 1 3 1 3 1 3
  • B: 2 2 1 4 1 2
  • C: 1 2 3 2 1 6 1 2 3 2 1 6

Given a sequence of NN positive integers, write a program that decides whether some triangulation has that sequence as its triangle count sequence. If one does, print all of its triangles.

Input

The first line contains the number of test cases PP. (1≤P≤10001 \le P \le 1000)

Each of the next PP lines holds one test case. The first integer on a line is the test case number KK, and the second is the length of the sequence NN. (4≤N≤204 \le N \le 20) The remaining NN integers are the triangle count sequence, and all of them are positive.

Output

For each test case print the test case number KK, a space, and the verdict. Print the capital letter N when no triangulation has the given sequence as its triangle count sequence, and the capital letter Y when one does.

After a Y, print the triangles on the next N−2N-2 lines, one per line. Vertices are numbered 1 through NN in the order the sequence gives them, and the three vertex numbers of one triangle are printed in increasing order. Sort the triangles lexicographically. When k<l<mk < l < m is one triangle and r<s<tr < s < t is another, the first comes before the second if

k<r OR (k==r AND l<s) OR (k==r AND l==s AND m<t)

At most one triangulation fits a given sequence, so the output is fixed.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    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
    
  2. Example 2

    Input
    4
    1 4 2 1 2 1
    2 4 1 2 1 2
    3 4 1 1 1 1
    4 4 1 2 1 3
    
    Expected output
    1 Y
    1 2 3
    1 3 4
    2 Y
    1 2 4
    2 3 4
    3 N
    4 N