Find first undeleted integer from K to N in given unconnected Graph after performing Q queries | Function to perform th Get operation of disjoint set union ; Function to perform the union operation of dijoint set union ; Update the graph [ a ] as b ; Function to perform given queries on set of vertices initially not connected ; Stores the vertices ; Mark every vertices rightmost vertex as i ; Traverse the queries array ; Check if it is first type of the givan query ; Get the parent of a ; Print the answer for the second query ; Driver Code
def Get ( graph , a ) : NEW_LINE INDENT if graph [ graph [ a ] ] != graph [ a ] : NEW_LINE INDENT graph [ a ] = Get ( graph , graph [ a ] ) NEW_LINE DEDENT else : NEW_LINE INDENT return graph [ a ] NEW_LINE DEDENT DEDENT def Union ( graph , a , b ) : NEW_LINE INDENT a = Get ( graph , a ) NEW_LINE b = Get ( graph , b ) NEW_LINE graph [ a ] = b NEW_LINE DEDENT def Queries ( queries , N , M ) : NEW_LINE INDENT graph = [ 0 for i in range ( N + 2 ) ] NEW_LINE for i in range ( 1 , N + 2 , 1 ) : NEW_LINE INDENT graph [ i ] = i NEW_LINE DEDENT for query in queries : NEW_LINE INDENT if ( query [ 0 ] == 1 ) : NEW_LINE INDENT Union ( graph , query [ 1 ] , query [ 1 ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT a = Get ( graph , query [ 1 ] ) NEW_LINE if ( a == N + 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( graph [ a ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE queries = [ [ 2 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] , [ 2 , 3 ] ] NEW_LINE Q = len ( queries ) NEW_LINE Queries ( queries , N , Q ) NEW_LINE DEDENT