Read the checking program (120 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 = 3
INDEPENDENT = 10
MAX_VERTICES = 40
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 has_independent_set(neighbors, need):
n = len(neighbors)
if n < need:
return False
# Meet in the middle. For each allowed subset of the right half,
# tabulate the maximum size of an independent set it contains.
split = n // 2
right_neighbors = [mask >> split for mask in neighbors]
sizes = bytearray(1 << (n - split))
for allowed in range(1, len(sizes)):
first = allowed & -allowed
rest = allowed ^ first
adjacent = right_neighbors[split + first.bit_length() - 1]
sizes[allowed] = max(sizes[rest], 1 + sizes[rest & ~adjacent])
# Enumerate independent subsets of the left half. For each, the right
# half may use only vertices nonadjacent to every chosen left vertex.
def search(remaining, allowed, count):
if count + sizes[allowed] >= need:
return True
if count + remaining.bit_count() + sizes[allowed] < need:
return False
first = remaining & -remaining
rest = remaining ^ first
i = first.bit_length() - 1
return search(rest & ~neighbors[i], allowed & ~right_neighbors[i], count + 1) or search(
rest, allowed, count
)
return search((1 << split) - 1, len(sizes) - 1, 0)
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.")
if has_independent_set(neighbors, 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()