Created
December 23, 2024 10:02
-
-
Save dezgeg/0407c08cde9c98720ddf73db2f53d9ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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