day 23, bron kerbosch is more efficient than me

This commit is contained in:
setop 2024-12-26 00:35:38 +01:00
parent ff5432d8ea
commit 679d4788d4
2 changed files with 23 additions and 1 deletions

View File

@ -0,0 +1,23 @@
from collections import defaultdict as DD
import sys
G = DD(set)
for line in sys.stdin.read().strip().split('\n'):
a, b = line.split('-')
G[a].add(b)
G[b].add(a)
def bron_kerbosch(r, p, x):
if len(p) == 0 and len(x) == 0:
yield r
else:
pivot = max(p | x, key=lambda x: len(G[x]))
for v in p - G[pivot]:
neighbours = G[v]
yield from bron_kerbosch(r | {v}, p & neighbours, x & neighbours)
p.remove(v)
x.add(v)
clique = max(bron_kerbosch(set(), set(G.keys()), set()), key=len)
print(','.join(sorted(clique)))

View File

@ -1,7 +1,6 @@
import sys
from collections import defaultdict as DD
D = DD(set)
S = set()
for i in sys.stdin.read().strip().split('\n'):