Source: CTST-2022-P7
Category: Existence and Construction

Problem: Find all positive integers $(m,n)$ such that in a grid table formed by $m+1$ horizontal lines and $11$ vertical lines, it is possible to add a diagonal to some of the unit squares (it is allowed to add none, but not allowed to add two in one square), satisfying the condition that the entire figure can be drawn in one stroke and return to the starting point.

Answer: m=n

Reference solution:
Let the grid be of size $m \times n$. Since there are $11$ vertical lines, the number of columns is $n = 10$.

When $m=n$, add a diagonal from the upper left to the lower right in every small square except those on the main diagonal from the upper left to the lower right. In this case, each vertex has even degree, so the entire graph can be drawn in a single stroke and return to the starting point.

Consider only the added diagonals. Because after adding the diagonals each vertex has even degree and the added diagonals do not intersect at interior points, we have: at the four corner vertices of the grid no diagonal is added; at boundary vertices that are not corners exactly one diagonal is added; at interior vertices an even number of diagonals is added. If four diagonals meet at the same interior vertex, they can be split into two non-intersecting broken lines. Thus the whole set of added diagonals consists of some pairwise disjoint cycles and broken lines that connect non-corner boundary vertices of the grid. 

Color the vertices alternately black and white; then each such broken line passes through vertices of only one color. These broken lines cannot connect two vertices on the same side of the grid, for otherwise the segment between those two vertices would contain an odd number of vertices on that side, making it impossible to pairwise connect the vertices by non-intersecting broken lines. They also cannot connect two vertices on opposite sides: suppose the two vertices lie on the top and bottom sides; then the broken line would separate the grid into a left part and a right part. The left part would contain an odd number of vertices on the boundary; after removing the two corner vertices the number remains odd, and these odd vertices cannot be paired by non-intersecting broken lines. 

Hence non-corner vertices on the top and bottom sides can only be connected to non-corner vertices on the left and right sides. Consequently, we have $m=n$. 

Since $n = 10$, we conclude that $m = 10$, and the required pair is $(10, 10)$.

Grading guidelines:
(Partial) 1. Reformulated the condition as an Eulerian circuit problem: the drawing is possible iff every vertex of the resulting planar graph has even degree (connectedness addressed or assumed). 2. Constructed a valid configuration for the case m=10 (i.e. a 10×10 grid): e.g. add the same-direction diagonal in all unit squares except those on the main diagonal, and checked that all vertices have even degree. 3. From the parity constraints at vertices, deduced basic structural facts about the set of added diagonals: corners cannot be incident to added diagonals; each non-corner boundary vertex must be incident to exactly one added diagonal; each interior vertex must be incident to an even number of added diagonals. (Almost) 1. Proved the necessity m=10 (equivalently m=n with n=10) via the standard decomposition of diagonals into disjoint cycles and nonintersecting boundary-to-boundary broken lines, but left one key step insufficiently justified (e.g. why a broken line cannot connect two boundary vertices on the same/opposite side). 2. Completed the parity/coloring and planar-separation argument forcing m=n and hence m=10, but did not explicitly provide (or fully verify) a construction for m=10. 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For the specific case of a $518 \times 518$ grid of squares (i.e., $518$ rows and $518$ columns of unit squares), provide an explicit construction of the added diagonals such that every vertex in the resulting graph has an even degree, allowing an Eulerian circuit.

Construction requirements:
- Output a single Python expression (e.g., a list comprehension) that evaluates to a list of diagonals.
- Each diagonal must be represented as a list of two vertices: `[[r1, c1], [r2, c2]]`.
- Use 0-based coordinates, where `r` is the row index (0 to 518) and `c` is the column index (0 to 518).
- The top-left vertex of the grid is `[0, 0]` and the bottom-right is `[518, 518]`.
- A diagonal in the square at row `i` and column `j` (where 0 <= i, j <= 517) must connect either `[i, j]` to `[i+1, j+1]` or `[i+1, j]` to `[i, j+1]`.
- The expression must not contain assignments, loops (other than in comprehensions), or any non-expression content.
- Put ONLY the Python expression inside the construction block.

Reference construction:
[[[i, j], [i + 1, j + 1]] for i in range(518) for j in range(518) if i != j]

Verifier code:
import sys

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return

    try:
        # Evaluate using eval() to support list comprehensions as requested
        diagonals = eval(payload)
    except Exception as e:
        print(f"Failed to evaluate input: {e}")
        return

    if not isinstance(diagonals, list):
        print("Evaluated result is not a list.")
        return

    squares_used = set()
    N = 518
    
    # Initialize degrees for all vertices in the (N+1) x (N+1) grid
    degrees = {}
    for r in range(N + 1):
        for c in range(N + 1):
            if (r == 0 or r == N) and (c == 0 or c == N):
                degrees[(r, c)] = 2
            elif r == 0 or r == N or c == 0 or c == N:
                degrees[(r, c)] = 3
            else:
                degrees[(r, c)] = 4

    # Process each diagonal
    for idx, diag in enumerate(diagonals):
        if not isinstance(diag, list) or len(diag) != 2:
            print(f"Diagonal at index {idx} is not a list of two vertices: {diag}")
            return
        
        v1, v2 = diag
        if not isinstance(v1, list) or len(v1) != 2 or not isinstance(v2, list) or len(v2) != 2:
            print(f"Vertices of diagonal at index {idx} are not valid lists of length 2: {diag}")
            return
            
        r1, c1 = v1
        r2, c2 = v2
        
        if type(r1) is not int or type(c1) is not int or type(r2) is not int or type(c2) is not int:
            print(f"Coordinates of diagonal at index {idx} must be integers: {diag}")
            return

        # Determine the square (i, j) this diagonal belongs to
        i = min(r1, r2)
        j = min(c1, c2)
        
        if not (0 <= i < N and 0 <= j < N):
            print(f"Diagonal at index {idx} is out of bounds for a {N}x{N} grid of squares: {diag}")
            return
            
        # Check if it's a valid diagonal for square (i, j)
        set1 = {(i, j), (i + 1, j + 1)}
        set2 = {(i + 1, j), (i, j + 1)}
        endpoints = {(r1, c1), (r2, c2)}
        
        if endpoints != set1 and endpoints != set2:
            print(f"Diagonal at index {idx} is not a valid diagonal for square ({i}, {j}): {diag}")
            return
            
        # Check for multiple diagonals in the same square
        if (i, j) in squares_used:
            print(f"Multiple diagonals placed in square ({i}, {j}).")
            return
            
        squares_used.add((i, j))
        
        # Increment degree for both endpoints
        degrees[(r1, c1)] += 1
        degrees[(r2, c2)] += 1

    # Verify that all vertices have an even degree
    for (r, c), deg in degrees.items():
        if deg % 2 != 0:
            print(f"Vertex ({r}, {c}) has an odd degree: {deg}.")
            return

    print("True")

if __name__ == "__main__":
    verify()