Topological Sorting by recursive

import sys
P=["0 "]+[l[3:]for l in sys.stdin]
while 1:k=[i for i,p in enumerate(P)if all(P[int(j)]=="0"for j in p.split())][0];print k;P[k]="0"

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page