Query to find the maximum and minimum weight between two nodes in the given tree using LCA . | C # Program to find the maximum and minimum weight between two nodes in the given tree using LCA ; Math . Log ( MAX ) ; Array to store the level of each node ; List 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 ; Initialising lca values with - 1 Initialising MinWeight values with int . MaxValue Initialising MaxWeight values with int . MinValue ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 4 } ; Query 3 : { 3 , 5 }
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 1000 ; static readonly int log = 10 ; static int [ ] level = new int [ MAX ] ; static int [ , ] lca = new int [ MAX , log ] ; static int [ , ] minWeight = new int [ MAX , log ] ; static int [ , ] maxWeight = new int [ MAX , log ] ; static List < int > [ ] graph = new List < int > [ MAX ] ; static int [ ] weight = new int [ MAX ] ; private static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } static void addEdge ( int u , int v ) { graph [ u ] . Add ( v ) ; graph [ v ] . Add ( u ) ; } static void dfs ( int node , int parent , int h ) { lca [ node , 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { minWeight [ node , 0 ] = Math . Min ( weight [ node ] , weight [ parent ] ) ; maxWeight [ node , 0 ] = Math . Max ( weight [ node ] , weight [ parent ] ) ; } for ( int i = 1 ; i < log ; i ++ ) { if ( lca [ node , i - 1 ] != - 1 ) { lca [ node , i ] = lca [ lca [ node , i - 1 ] , i - 1 ] ; minWeight [ node , i ] = Math . Min ( minWeight [ node , i - 1 ] , minWeight [ lca [ node , i - 1 ] , i - 1 ] ) ; maxWeight [ node , i ] = Math . Max ( maxWeight [ node , i - 1 ] , maxWeight [ lca [ node , i - 1 ] , i - 1 ] ) ; } } foreach ( int i in graph [ node ] ) { if ( i == parent ) continue ; dfs ( i , node , h + 1 ) ; } } static void findMinMaxWeight ( int u , int v ) { int minWei = int . MaxValue ; int maxWei = int . MinValue ; if ( level [ u ] > level [ v ] ) swap ( u , v ) ; for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v , i ] != - 1 && level [ lca [ v , i ] ] >= level [ u ] ) { minWei = Math . Min ( minWei , minWeight [ v , i ] ) ; maxWei = Math . Max ( maxWei , maxWeight [ v , i ] ) ; v = lca [ v , i ] ; } } if ( v == u ) { Console . Write ( minWei + " ▁ " + maxWei + " STRNEWLINE " ) ; } else { for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( v == - 1 ) v ++ ; if ( lca [ v , i ] != lca [ u , i ] ) { minWei = Math . Min ( minWei , Math . Min ( minWeight [ v , i ] , minWeight [ u , i ] ) ) ; maxWei = Math . Max ( maxWei , Math . Max ( maxWeight [ v , i ] , maxWeight [ u , i ] ) ) ; v = lca [ v , i ] ; u = lca [ u , i ] ; } } if ( u == - 1 ) u ++ ; minWei = Math . Min ( minWei , Math . Min ( minWeight [ v , 0 ] , minWeight [ u , 0 ] ) ) ; maxWei = Math . Max ( maxWei , Math . Max ( maxWeight [ v , 0 ] , maxWeight [ u , 0 ] ) ) ; Console . Write ( minWei + " ▁ " + maxWei + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < graph . Length ; i ++ ) graph [ i ] = new List < int > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 3 ) ; weight [ 1 ] = - 1 ; weight [ 2 ] = 5 ; weight [ 3 ] = - 1 ; weight [ 4 ] = 3 ; weight [ 5 ] = - 2 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < log ; j ++ ) { lca [ i , j ] = - 1 ; minWeight [ i , j ] = int . MaxValue ; maxWeight [ i , j ] = int . MinValue ; } } dfs ( 1 , - 1 , 0 ) ; findMinMaxWeight ( 1 , 3 ) ; findMinMaxWeight ( 2 , 4 ) ; findMinMaxWeight ( 3 , 5 ) ; } }