33 lines
669 B
Python
33 lines
669 B
Python
|
import fileinput
|
||
|
from collections import defaultdict
|
||
|
|
||
|
def build_graph():
|
||
|
graph = defaultdict(list)
|
||
|
for line in fileinput.input():
|
||
|
a, b = line[5], line[36]
|
||
|
graph[b].append(a)
|
||
|
graph[a]
|
||
|
|
||
|
return graph
|
||
|
|
||
|
|
||
|
def main():
|
||
|
graph = build_graph()
|
||
|
|
||
|
order = []
|
||
|
to_consider = sorted(graph.keys())
|
||
|
while to_consider:
|
||
|
for i, node in enumerate(to_consider):
|
||
|
parents = graph[node]
|
||
|
if any(parent in to_consider for parent in parents):
|
||
|
continue
|
||
|
|
||
|
order.append(node)
|
||
|
del to_consider[i]
|
||
|
break
|
||
|
|
||
|
print("".join(order))
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|