Minimum time required to color all edges of a Tree | Java program for the above approach ; 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
import java . util . * ; class GFG { static int ans = 0 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > edges [ ] = new Vector [ 100000 ] ; static void Add_edge ( int u , int v ) { edges [ u ] . add ( v ) ; edges [ v ] . add ( u ) ; } static void minTimeToColor ( int node , int parent , int arrival_time ) { int current_time = 0 ; for ( int x = 0 ; x < edges [ node ] . size ( ) ; x ++ ) { if ( edges [ node ] . get ( x ) != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . max ( ans , current_time ) ; minTimeToColor ( edges [ node ] . get ( x ) , node , current_time ) ; } } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < edges . length ; i ++ ) edges [ i ] = new Vector < Integer > ( ) ; int A [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; for ( int i = 0 ; i < 3 ; i ++ ) { Add_edge ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; System . out . print ( ans + "NEW_LINE"); } }