Read the checking program (106 lines of Python)
"""Check a simple connected regular graph has the required girth and score its order."""
from collections import deque
import json
import sys
DEGREE = 3
GIRTH = 16
MAX_VERTICES = 936
MAX_BYTES = 11232
def check_girth(graph, girth):
# A shortest cycle is detected by BFS from its least-numbered vertex.
# Ignore smaller vertices so each possible cycle has one such root.
# Only vertices through floor((girth - 1) / 2) need expansion.
# A non-tree edge closes a walk of length d(u) + d(v) + 1.
# The walk contains a cycle of at most that length.
radius = (girth - 1) // 2
even_girth = girth % 2 == 0
seen = [-1] * len(graph)
distance = [0] * len(graph)
parent = [-1] * len(graph)
found = False
for start in range(len(graph)):
seen[start] = start
distance[start] = 0
parent[start] = -1
pending = [start]
for u in pending:
next_depth = distance[u] + 1
# An even cycle can close via a shared vertex just beyond the radius.
can_discover = even_girth or next_depth <= radius
for v in graph[u]:
if v < start:
continue
if seen[v] != start:
if not can_discover:
continue
seen[v] = start
distance[v] = next_depth
parent[v] = u
if next_depth <= radius:
pending.append(v)
elif v != parent[u] and u != parent[v]:
length = next_depth + distance[v]
if length < girth:
raise ValueError("The graph contains a cycle shorter than the required girth.")
found |= length == girth
if not found:
raise ValueError("The graph has no cycle of the required length.")
def verify(data, degree=DEGREE, girth=GIRTH, max_vertices=MAX_VERTICES):
# Row i lists all neighbors of vertex i, so isolated or missing rows
# cannot be hidden by an edge list's implicit numbering.
lines = data.splitlines()
order = len(lines)
if not degree < order <= max_vertices:
raise ValueError("The graph has the wrong number of vertices.")
graph = []
for u, line in enumerate(lines):
entries = line.split()
if len(entries) != degree:
raise ValueError("Every vertex must list the required number of neighbors.")
if any(len(token) > 4 or not token.isdigit() for token in entries):
raise ValueError("Neighbor indices must be short nonnegative decimal integers.")
neighbors = tuple(map(int, entries))
if u in neighbors or len(set(neighbors)) != degree:
raise ValueError("The graph has a loop or a repeated neighbor.")
if any(v >= order for v in neighbors):
raise ValueError("A neighbor index is outside the graph.")
graph.append(neighbors)
if any(u not in graph[v] for u, neighbors in enumerate(graph) for v in neighbors):
raise ValueError("Every listed edge must appear in both directions.")
reached = {0}
pending = deque([0])
while pending:
for v in graph[pending.popleft()]:
if v not in reached:
reached.add(v)
pending.append(v)
if len(reached) != order:
raise ValueError("The graph must be connected.")
check_girth(graph, girth)
return order
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("The submission exceeds the byte limit.")
if not data.isascii() or any(c < 32 and c not in (9, 10, 13) for c in data):
raise ValueError("Use plain ASCII text.")
score = verify(data)
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
else:
result = {"valid": True, "score": score, "message": "The construction is valid."}
print(json.dumps(result, separators=(",", ":")))
if __name__ == "__main__":
main()