Query to find the maximum and minimum weight between two nodes in the given tree using LCA . | Python3 Program to find the maximum and minimum weight between two nodes in the given tree using LCA ; log2 ( MAX ) ; Array to store the level of each node ; Vector to store tree ; Array to store weight of nodes ; Pre - Processing to calculate values of lca [ ] [ ] , MinWeight [ ] [ ] and MaxWeight [ ] [ ] ; Using recursion formula to calculate the values of lca [ ] [ ] ; Storing the level of each node ; Using recursion formula to calculate the values of lca [ ] [ ] , MinWeight [ ] [ ] and MaxWeight [ ] [ ] ; Function to find the minimum and maximum weights in the given range ; The node which is present farthest from the root node is taken as v If u is farther from root node then swap the two ; Finding the ancestor of v which is at same level as u ; Calculating Minimum and Maximum Weight of node v till its 2 ^ i - th ancestor ; If u is the ancestor of v then u is the LCA of u and v ; Finding the node closest to the root which is not the common ancestor of u and v i . e . a node x such that x is not the common ancestor of u and v but lca [ x ] [ 0 ] is ; Calculating the minimum of MinWeight of v to its 2 ^ i - th ancestor and MinWeight of u to its 2 ^ i - th ancestor ; Calculating the maximum of MaxWeight of v to its 2 ^ i - th ancestor and MaxWeight of u to its 2 ^ i - th ancestor ; Calculating the Minimum of first ancestor of u and v ; Calculating the maximum of first ancestor of u and v ; Driver code ; Number of nodes ; Add edges ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 4 } ; Query 3 : { 3 , 5 }
import sys NEW_LINE MAX = 1000 NEW_LINE log = 10 NEW_LINE level = [ 0 for i in range ( MAX ) ] ; NEW_LINE lca = [ [ - 1 for j in range ( log ) ] for i in range ( MAX ) ] NEW_LINE minWeight = [ [ sys . maxsize for j in range ( log ) ] for i in range ( MAX ) ] NEW_LINE maxWeight = [ [ - sys . maxsize for j in range ( log ) ] for i in range ( MAX ) ] NEW_LINE graph = [ [ ] for i in range ( MAX ) ] NEW_LINE weight = [ 0 for i in range ( MAX ) ] NEW_LINE def addEdge ( u , v ) : NEW_LINE INDENT graph [ u ] . append ( v ) ; NEW_LINE graph [ v ] . append ( u ) ; NEW_LINE DEDENT def dfs ( node , parent , h ) : NEW_LINE INDENT lca [ node ] [ 0 ] = parent ; NEW_LINE level [ node ] = h ; NEW_LINE if ( parent != - 1 ) : NEW_LINE INDENT minWeight [ node ] [ 0 ] = ( min ( weight [ node ] , weight [ parent ] ) ) ; NEW_LINE maxWeight [ node ] [ 0 ] = ( max ( weight [ node ] , weight [ parent ] ) ) ; NEW_LINE DEDENT for i in range ( 1 , log ) : NEW_LINE INDENT if ( lca [ node ] [ i - 1 ] != - 1 ) : NEW_LINE INDENT lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; NEW_LINE minWeight [ node ] [ i ] = min ( minWeight [ node ] [ i - 1 ] , minWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; NEW_LINE maxWeight [ node ] [ i ] = max ( maxWeight [ node ] [ i - 1 ] , maxWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; NEW_LINE DEDENT DEDENT for i in graph [ node ] : NEW_LINE INDENT if ( i == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dfs ( i , node , h + 1 ) ; NEW_LINE DEDENT DEDENT def findMinMaxWeight ( u , v ) : NEW_LINE INDENT minWei = sys . maxsize NEW_LINE maxWei = - sys . maxsize NEW_LINE if ( level [ u ] > level [ v ] ) : NEW_LINE INDENT u , v = v , u NEW_LINE DEDENT for i in range ( log - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( lca [ v ] [ i ] != - 1 and level [ lca [ v ] [ i ] ] >= level [ u ] ) : NEW_LINE INDENT minWei = min ( minWei , minWeight [ v ] [ i ] ) ; NEW_LINE maxWei = max ( maxWei , maxWeight [ v ] [ i ] ) ; NEW_LINE v = lca [ v ] [ i ] ; NEW_LINE DEDENT DEDENT if ( v == u ) : NEW_LINE INDENT print ( str ( minWei ) + ' ▁ ' + str ( maxWei ) ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( log - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) : NEW_LINE INDENT minWei = ( min ( minWei , min ( minWeight [ v ] [ i ] , minWeight [ u ] [ i ] ) ) ) ; NEW_LINE maxWei = max ( maxWei , max ( maxWeight [ v ] [ i ] , maxWeight [ u ] [ i ] ) ) ; NEW_LINE v = lca [ v ] [ i ] ; NEW_LINE u = lca [ u ] [ i ] ; NEW_LINE DEDENT DEDENT minWei = min ( minWei , min ( minWeight [ v ] [ 0 ] , minWeight [ u ] [ 0 ] ) ) ; NEW_LINE maxWei = max ( maxWei , max ( maxWeight [ v ] [ 0 ] , maxWeight [ u ] [ 0 ] ) ) ; NEW_LINE print ( str ( minWei ) + ' ▁ ' + str ( maxWei ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE addEdge ( 1 , 2 ) ; NEW_LINE addEdge ( 1 , 5 ) ; NEW_LINE addEdge ( 2 , 4 ) ; NEW_LINE addEdge ( 2 , 3 ) ; NEW_LINE weight [ 1 ] = - 1 ; NEW_LINE weight [ 2 ] = 5 ; NEW_LINE weight [ 3 ] = - 1 ; NEW_LINE weight [ 4 ] = 3 ; NEW_LINE weight [ 5 ] = - 2 ; NEW_LINE dfs ( 1 , - 1 , 0 ) ; NEW_LINE findMinMaxWeight ( 1 , 3 ) ; NEW_LINE findMinMaxWeight ( 2 , 4 ) ; NEW_LINE findMinMaxWeight ( 3 , 5 ) ; NEW_LINE DEDENT