Maximum weighted edge in path between two nodes in an N | Java implementation to find the maximum weighted edge in the simple path between two nodes in N - ary Tree ; Depths of Nodes ; Parent at every 2 ^ i level ; Maximum node at every 2 ^ i level ; Graph that stores destinations and its weight ; Function to traverse the nodes using the Depth - First Search Traversal ; Condition to check if its equal to its parent then skip ; DFS Recursive Call ; Function to find the ansector ; Loop to set every 2 ^ i distance ; Loop to calculate for each node in the N - ary tree ; Storing maximum edge ; Swaping if node a is at more depth than node b because we will always take at more depth ; Difference between the depth of the two given nodes ; Changing Node B to its parent at 2 ^ i distance ; Subtracting distance by 2 ^ i ; Take both a , b to its lca and find maximum ; Loop to find the maximum 2 ^ ith parent the is different for both a and b ; Updating ans ; Changing value to its parent ; Function to compute the Least common Ansector ;  ; Undirected tree ; Computing LCA
import java . util . * ; import java . awt . Point ; public class Main { static int N = 100005 ; static int [ ] level = new int [ N ] ; static int LG = 20 ; static int [ ] [ ] dp = new int [ LG ] [ N ] ; static int [ ] [ ] mx = new int [ LG ] [ N ] ; static Vector < Vector < Point > > v = new Vector < Vector < Point > > ( ) ; static int n = 0 ; static void dfs_lca ( int a , int par , int lev ) { dp [ 0 ] [ a ] = par ; level [ a ] = lev ; for ( int i = 0 ; i < v . get ( a ) . size ( ) ; i ++ ) { if ( v . get ( a ) . get ( i ) . x == par ) continue ; mx [ 0 ] [ v . get ( a ) . get ( i ) . x ] = v . get ( a ) . get ( i ) . y ; dfs_lca ( v . get ( a ) . get ( i ) . x , a , lev + 1 ) ; } } static void find_ancestor ( ) { for ( int i = 1 ; i < 16 ; i ++ ) { for ( int j = 1 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ; mx [ i ] [ j ] = Math . max ( mx [ i - 1 ] [ j ] , mx [ i - 1 ] [ dp [ i - 1 ] [ j ] ] ) ; } } } static int getMax ( int a , int b ) { if ( level [ b ] < level [ a ] ) { int temp = a ; a = b ; b = temp ; } int ans = 0 ; int diff = level [ b ] - level [ a ] ; while ( diff > 0 ) { int log = ( int ) ( Math . log ( diff ) / Math . log ( 2 ) ) ; ans = Math . max ( ans , mx [ log ] [ b ] ) ; b = dp [ log ] [ b ] ; diff -= ( 1 << log ) ; } while ( a != b ) { int i = ( int ) ( Math . log ( level [ a ] ) / Math . log ( 2 ) ) ; while ( i > 0 && dp [ i ] [ a ] == dp [ i ] [ b ] ) { i -= 1 ; } ans = Math . max ( ans , mx [ i ] [ a ] ) ; ans = Math . max ( ans , mx [ i ] [ b ] ) ; a = dp [ i ] [ a ] ; b = dp [ i ] [ b ] ; } return ans ; } static void compute_lca ( ) { dfs_lca ( 1 , 0 , 0 ) ; find_ancestor ( ) ; } '' ' ▁ Driver ▁ Code ▁ ' '' public static void main ( String [ ] args ) { for ( int i = 0 ; i < LG ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; mx [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { v . add ( new Vector < Point > ( ) ) ; } v . get ( 1 ) . add ( new Point ( 2 , 2 ) ) ; v . get ( 2 ) . add ( new Point ( 1 , 2 ) ) ; v . get ( 1 ) . add ( new Point ( 3 , 5 ) ) ; v . get ( 3 ) . add ( new Point ( 1 , 5 ) ) ; v . get ( 3 ) . add ( new Point ( 4 , 3 ) ) ; v . get ( 4 ) . add ( new Point ( 3 , 4 ) ) ; v . get ( 3 ) . add ( new Point ( 5 , 1 ) ) ; v . get ( 5 ) . add ( new Point ( 3 , 1 ) ) ; compute_lca ( ) ; int [ ] [ ] queries = { { 3 , 5 } , { 2 , 3 } , { 2 , 4 } } ; int q = 3 ; for ( int i = 0 ; i < q ; i ++ ) { int max_edge = getMax ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ; System . out . println ( max_edge ) ; } } }