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
function Get ( graph , a ) { return ( graph [ a ] = graph [ a ] == a ? a : Get ( graph , graph [ a ] ) ) ; } function Union ( graph , a , b ) { a = Get ( graph , a ) ; b = Get ( graph , b ) ; graph [ a ] = b ; } function Queries ( queries , N , M ) { let graph = new Array ( N ) . fill ( 2 ) ; for ( let i = 1 ; i <= N + 1 ; i ++ ) { graph [ i ] = i ; } for ( let query of queries ) { if ( query [ 0 ] == 1 ) { Union ( graph , query [ 1 ] , query [ 1 ] + 1 ) ; } else { let a = Get ( graph , query [ 1 ] ) ; if ( a == N + 1 ) document . write ( - 1 + " " ) ; else document . write ( graph [ a ] + " " ) ; } } } let N = 5 ; let queries = [ [ 2 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] , [ 2 , 3 ] , ] ; let Q = queries . length ; Queries ( queries , N , Q ) ;