Pagini recente » Cod sursa (job #968716) | Cod sursa (job #727906) | Cod sursa (job #1146611) | Cod sursa (job #1691530) | Cod sursa (job #2946018)
input_file = open("disjoint.in",'r')
output_file = open("disjoint.out", 'w')
nr_sets,nr_op = [int(x) for x in input_file.readline().split()]
fatha = [0 for index_set in range(nr_sets+1)]
h = [0 for elem in fatha]
'''
def interog(x):
if fatha[x] == 0:
return x
fatha[x] = interog(fatha[x])
return fatha[x]
'''
def interog(x):
if fatha[x] == 0:
return x
root = x
while(fatha[root] != 0):
root = fatha[root]
index = x
while(fatha[index] != 0):
c_fatha = fatha[index]
fatha[index] = root
index = c_fatha
return root
def reunion(x,y):
repr_x = interog(x)
repr_y = interog(y)
if h[repr_x] == h[repr_y]:
h[repr_x] += 1
fatha[repr_y] = repr_x
elif h[repr_x] > h[repr_y]:
fatha[repr_y] = repr_x
else:
fatha[repr_x] = repr_y
def op1(x,y):
reunion(x,y)
def op2(x,y):
if interog(x) == interog(y):
return "Da"
return "Nu"
for op_index in range(nr_op):
type_op,x,y = [int(elem) for elem in input_file.readline().split()]
print(fatha)
print(h)
print(type_op,x,y)
if type_op == 1:
op1(x,y)
else:
print(op2(x,y))
output_file.write(op2(x,y)+"\n")