Minimum time required to color all edges of a Tree | Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver Code ; Function call ; Finally , print the answer
let ans = 0 ; let edges = new Array ( 100000 ) ; for ( let i = 0 ; i < 100000 ; i ++ ) edges [ i ] = [ ] ; function Add_edge ( u , v ) { edges [ u ] . push ( v ) ; edges [ v ] . push ( u ) ; } function minTimeToColor ( node , parent , arrival_time ) { let current_time = 0 ; for ( let x = 0 ; x < edges [ node ] . length ; x ++ ) { if ( edges [ node ] [ x ] != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . max ( ans , current_time ) ; minTimeToColor ( edges [ node ] [ x ] , node , current_time ) ; } } } let A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ; for ( let i = 0 ; i < A . length ; i ++ ) { Add_edge ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; document . write ( ans ) ;