Minimum Spanning Tree by recursive

import sys
C={}
for q,p,w in sorted(map(str.split,sys.stdin),key=lambda x:int(x[2])):
 Q,P=[set(C.get(x,[x]))for x in p,q]
 for n in P^Q:C[n]=P^Q
 print"TF"[P==Q],q,p,w

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

download

return top