Read the checking program (88 lines of Python)
"""Check a simple graph has neither a clique nor an independent set of the forbidden sizes."""
import json
import sys
MAX_BYTES = 7000
CLIQUE = 5
INDEPENDENT = 5
MAX_VERTICES = 45
def has_clique(neighbors, candidates, need):
# Greedily color the remaining induced subgraph. A clique uses at most
# one vertex of each color, giving a safe upper bound in each search branch.
if need <= 1:
return bool(candidates)
if candidates.bit_count() < need:
return False
remaining = candidates
ordered = []
color = 0
while remaining:
color += 1
available = remaining
while available:
vertex = available & -available
adjacent = neighbors[vertex.bit_length() - 1]
ordered.append((vertex, adjacent, color))
remaining ^= vertex
available &= ~(vertex | adjacent)
for vertex, adjacent, bound in reversed(ordered):
if bound < need:
return False
if has_clique(neighbors, candidates & adjacent, need - 1):
return True
candidates ^= vertex
return False
def verify(data):
lines = data.splitlines()
if not lines or len(lines[0]) > 2 or not lines[0].isdigit():
raise ValueError("The first line must give the vertex count as a decimal integer.")
n = int(lines[0])
if not 1 <= n <= MAX_VERTICES:
raise ValueError("The number of vertices is out of range.")
if len(lines) - 1 > n * (n - 1) // 2:
raise ValueError("The submission contains too many edge rows.")
neighbors = [0] * n
for line in lines[1:]:
tokens = line.split()
if len(tokens) != 2:
raise ValueError("Each edge must specify exactly two vertex labels.")
if any(len(t) > 2 or not t.isdigit() for t in tokens):
raise ValueError("Vertex labels must be nonnegative decimal integers.")
a, b = map(int, tokens)
if a == b or max(a, b) >= n:
raise ValueError("An edge uses a loop or an out-of-range vertex label.")
if neighbors[a] & (1 << b):
raise ValueError("An edge occurs more than once.")
neighbors[a] |= 1 << b
neighbors[b] |= 1 << a
vertices = (1 << n) - 1
if has_clique(neighbors, vertices, CLIQUE):
raise ValueError("The graph contains a forbidden clique.")
complement = [vertices ^ (1 << i) ^ mask for i, mask in enumerate(neighbors)]
if has_clique(complement, vertices, INDEPENDENT):
raise ValueError("The graph contains a forbidden independent set.")
return n
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("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()