def solution ( n , costs): answer = 0 V = set () for v1 , v2 , cost in costs: V.add(v1) V.add(v2) # collectiong total index and number of islands without duplication # used as unvisited set sortedCosts = sorted (costs , key = lambda x: x[ 2 ]) # make a sorted list, key : node connection, value : cost # it keeps the sequence of each elem in inner list, # but each inner list is sorted by lower cost # means this sort meet the minial cost condition # x:x[2] means, key value to sort by is x[2] item in inner list visited = set () visited.add(V.pop()) # put the first node index and thats it while V: for i in range ( len (sortedCosts)): v1 , v2 , cost = sortedCosts[i] # how to spead and assign multiple items in one line if v1 in visited and v2 in visited: # means same connection with lower cost has already been confirmed # if the connection