Skip to content

Instantly share code, notes, and snippets.

@dezgeg
Created December 23, 2024 10:02
Show Gist options
  • Save dezgeg/0407c08cde9c98720ddf73db2f53d9ac to your computer and use it in GitHub Desktop.
Save dezgeg/0407c08cde9c98720ddf73db2f53d9ac to your computer and use it in GitHub Desktop.
import sys
from collections import defaultdict
from functools import cache
neighbors = defaultdict(lambda: set())
nodes = set()
for line in sys.stdin:
a,b = line.strip().split('-')
neighbors[a].add(b)
neighbors[b].add(a)
nodes.add(a)
nodes.add(b)
@cache
def doit(ns):
return max(map(lambda p: frozenset([p]) | doit(ns & neighbors[p]), ns), key=len, default=frozenset())
print(','.join(sorted(doit(frozenset(nodes)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment