C# program:
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 ) ; } }

Index/key variables: MAX, i, node, parent, u, v
Accumulator variables: i, j, u, v
Iterator variables: i, j
Class/struct names: GFG