Source: CSMO-2018-P14
Category: Graph Theory

Problem: Given an integer $m \\geq 2$, a conference has $3m$ delegates. Every two delegates either shake hands once or do not shake hands. For a positive integer $n \\leq 3m - 1$, if there exist $n$ delegates whose numbers of handshakes are $1, 2, \\cdots, n$ respectively, then the conference is called \"$n$-interesting\". If for every possible $n$-interesting conference, there always exist three delegates who have shaken hands pairwise, find the minimum value of $n$.


Answer: $2m+1$

Reference solution:
Treat the delegates as vertices of a graph; if two delegates have shaken hands, draw an edge between their corresponding vertices. 

First, we show that $n \le 2m$ is not sufficient to guarantee a triangle. Construct a bipartite graph whose vertices are partitioned into two sets $A = \{A_1, A_2, \dots, A_m\}$ and $B = \{B_1, B_2, \dots, B_{2m}\}$. Add edges $A_iB_j$ for all $1\le i \le m$ and $1 \le j \le 2m$ such that $i \le j$. 
In this graph, the degree of $B_j$ is the number of $A_i$ such that $i \le j$, which is $\min(m, j)$. Thus, the degrees of $B_1, B_2, \dots, B_m$ are $1, 2, \dots, m$, respectively. 
The degree of $A_i$ is the number of $B_j$ such that $j \ge i$, which is $2m - i + 1$. Thus, the degrees of $A_m, A_{m-1}, \dots, A_1$ are $m+1, m+2, \dots, 2m$, respectively. 
Therefore, the graph contains vertices with degrees $1, 2, \dots, 2m$, making it $n$-interesting for any $n \le 2m$. Since the graph is bipartite, it contains no triangles. This shows that $n$ must be strictly greater than $2m$.

Now we prove that when $n=2m+1$, the graph must contain a triangle. Suppose for the sake of contradiction that there exists a $(2m+1)$-interesting graph on $3m$ vertices that contains no triangles. 
By the definition of a $(2m+1)$-interesting graph, there exist vertices with degrees $1, 2, \dots, 2m+1$. In particular, there exists a vertex of degree $2m+1$; without loss of generality, let this vertex be $A_1$, and let its neighbors be $B_1, B_2, \dots, B_{2m+1}$. 
Suppose the graph contains no triangles; then the neighbors $B_1, \dots, B_{2m+1}$ must be pairwise nonadjacent. This implies that any vertex $B_k$ can only be adjacent to vertices outside the set $\{B_1, \dots, B_{2m+1}\}$. Since the total number of vertices is $3m$, the number of such available vertices is $3m - (2m+1) = m-1$. Consequently, the degree of each $B_k$ is at most $m-1$.

The $(2m+1)$-interesting condition requires the graph to have vertices with degrees $m, m+1, \dots, 2m$, which constitute $m+1$ distinct values strictly greater than $m-1$. Since the degrees of $B_1, \dots, B_{2m+1}$ are at most $m-1$, the vertices achieving these $m+1$ degrees must be among the remaining vertices. 
Excluding $A_1$ (which has degree $2m+1$) and its neighbors $B_1, \dots, B_{2m+1}$, the number of remaining vertices is exactly $3m - 1 - (2m+1) = m-2$. 
However, it is impossible for $m-2$ vertices to cover the $m+1$ required degrees $m, m+1, \dots, 2m$. This contradicts the graph being $(2m+1)$-interesting. 

Hence, the graph must contain a triangle, satisfying the requirement. The minimum value of $n$ is $2m+1$.

Grading guidelines:
(Partial) 1. Reformulated the problem in graph language (delegates = vertices, handshakes = edges) and correctly identified the goal as forcing a triangle in any triangle-free graph having degrees 1,2,...,n among its vertices. 2. Gave a correct triangle-free construction on 3m vertices which is n-interesting for all n≤2m (e.g. an explicit bipartite graph with vertices of degrees 1,2,...,2m), thereby proving n must be >2m. 3. In the n=2m+1 case, proved the key local fact: if a vertex has degree 2m+1 in a triangle-free graph, then its 2m+1 neighbors form an independent set, hence each of those neighbors has degree at most m−1. (Almost) 1. Proved the lower bound via a correct triangle-free construction for all n≤2m, and for n=2m+1 set up the contradiction using a degree-(2m+1) vertex and the bound deg(neighbor)≤m−1, but made a small counting error in the final pigeonhole step (e.g. miscounted remaining vertices or the number of required degree values). 2. Completed the n=2m+1 contradiction argument in essence (neighbors independent, too few remaining vertices to realize degrees m,m+1,...,2m), but did not fully justify one boundary case in the counting (such as treating m=2 separately or checking that the degree-2m+1 vertex is distinct from the vertices intended to realize degrees m..2m). 3. Derived that n≥2m+1 and attempted to show n=2m+1 works with the correct strategy, but left a minor gap in arguing why vertices of degrees m,m+1,...,2m cannot lie among the 2m+1 neighbors (i.e. did not explicitly use the deg≤m−1 bound to exclude them).

Construction task:
For the specific case $m = 789$, determine the maximum integer $K$ such that there exists a $K$-interesting conference where no three delegates have shaken hands pairwise. Provide an explicit construction of the handshake graph for this maximum $K$.

Construction requirements:
- Output a single Python expression (e.g., a list comprehension) that evaluates to a $2367 \times 2367$ adjacency matrix (a list of lists of integers) representing the handshakes between the $3m = 2367$ delegates.
- The matrix must be symmetric, with 0s on the main diagonal and 0s or 1s elsewhere.
- The graph must be triangle-free (no three delegates shake hands pairwise).
- There must exist $K$ delegates whose degrees (number of handshakes) are exactly $1, 2, \dots, K$.
- $K$ must be the maximum possible value for $m = 789$.
- Put ONLY the valid Python expression inside the construction block, without any markdown formatting, explanations, or assignments.

Reference construction:
[[1 if (i < 789 and j >= i + 789) or (j < 789 and i >= j + 789) else 0 for j in range(2367)] for i in range(2367)]

Verifier code:
import sys

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty payload")
        return
        
    try:
        # Evaluate the payload directly to support list comprehensions
        M = eval(payload)
    except Exception as e:
        print(f"Failed to evaluate payload: {e}")
        return

    m = 789
    N = 3 * m  # 2367
    K = 2 * m  # 1578

    # 1 & 2. Check that M is a list of 2367 lists of length 2367, containing only 0 and 1
    if not isinstance(M, list):
        print("M is not a list")
        return
    if len(M) != N:
        print(f"M should have length {N}, got {len(M)}")
        return
        
    for i, row in enumerate(M):
        if not isinstance(row, list):
            print(f"Row {i} is not a list")
            return
        if len(row) != N:
            print(f"Row {i} should have length {N}, got {len(row)}")
            return
        for j, val in enumerate(row):
            # Strictly check for int to reject booleans or other types
            if type(val) is not int:
                print(f"M[{i}][{j}] is not an integer")
                return
            if val not in (0, 1):
                print(f"M[{i}][{j}] is not 0 or 1")
                return

    # 3. Verify symmetry and 0s on the main diagonal
    for i in range(N):
        if M[i][i] != 0:
            print(f"M[{i}][{i}] is not 0")
            return
        for j in range(i + 1, N):
            if M[i][j] != M[j][i]:
                print(f"M is not symmetric at ({i}, {j})")
                return

    # 4 & 5. Verify the degrees
    degrees = [sum(row) for row in M]
    degree_set = set(degrees)
    
    for d in range(1, K + 1):
        if d not in degree_set:
            print(f"Degree {d} is missing from the graph")
            return

    # 6. Verify that the graph contains no triangles
    neighbors = [set() for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if M[i][j] == 1:
                neighbors[i].add(j)
                
    for i in range(N):
        ni = neighbors[i]
        for j in ni:
            if i < j:
                # If the intersection of neighbors[i] and neighbors[j] is not empty, a triangle exists
                if not ni.isdisjoint(neighbors[j]):
                    print(f"Triangle found containing edge ({i}, {j})")
                    return

    print("True")

if __name__ == "__main__":
    verify()