Code Festival B Union Find by mitchs

a,*b=open(0)
p={}
def f(x):
 if x!=p.get(x,x):p[x]=x=f(p[x])
 return x
for s in b:
 q,a,b=map(f,s.split())
 if'0'<q:print('YNEOS'[a!=b::2])
 else:p[b]=a

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

download

return to the top page