Given an array of pairs Edges[][], representing edges connecting vertices in a Tree consisting of N nodes, the task is to find the minimum time required to color all the edges of a Tree based on the assumption that coloring an edge requires 1 unit of time.
Note: Multiple edges can be colored on a particular instant, but a node can be part of only one of the edges colored on a particular day.
Examples
Input: Edges[][] = ((1, 2), (3, 4), (2, 3))

Output: 2
Explanation:
Step 1: Color edges (1, 2) and (3, 4)
Step 2: Color edge (2, 3)
Input: Edges[][] = ((1, 2), (1, 3), (1, 4))

Output : 3
Approach: This problem can be solved using DFS(Depth First Search). Follow the steps below to solve the problem:
- Initialize global variables, say ans as 0, to store the minimum time required to color all the edges of a Tree.
- Initialize a variable current_time as 0, to store the time required to color the current edge.
- Iterate over the children of the current node and perform the following steps:
- If the current edge is not visited, i.e the current node is not equal to the parent node:
- Increase current_time by 1.
- Check if the parent edge has been colored at the same time or not. If found to be true, then increase current_time by 1 as a node cannot be part of more than one edge which are being colored at the same time.
- Update ans as maximum of ans and current_time.
- Call the recursive function minTimeToColor for the children of the current node.
- After the end of this function, print ans.
Below is the code for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
vector< int > edges[100000];
void Add_edge( int u, int v)
{
edges[u].push_back(v);
edges[v].push_back(u);
}
void minTimeToColor( int node, int parent,
int arrival_time)
{
int current_time = 0;
for ( auto x : edges[node]) {
if (x != parent) {
++current_time;
if (current_time == arrival_time)
++current_time;
ans = max(ans, current_time);
minTimeToColor(x, node, current_time);
}
}
}
int main()
{
pair< int , int > A[] = { { 1, 2 },
{ 2, 3 },
{ 3, 4 } };
for ( auto i : A) {
Add_edge(i.first, i.second);
}
minTimeToColor(1, -1, 0);
cout << ans << "\n" ;
}
|
Java
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 + "\n" );
}
}
|
Python3
ans = 0
edges = [[] for i in range ( 100000 )]
def Add_edge(u, v):
global edges
edges[u].append(v)
edges[v].append(u)
def minTimeToColor(node, parent, arrival_time):
global ans
current_time = 0
for x in edges[node]:
if (x ! = parent):
current_time + = 1
if (current_time = = arrival_time):
current_time + = 1
ans = max (ans, current_time)
minTimeToColor(x, node, current_time)
if __name__ = = '__main__' :
A = [ [ 1 , 2 ],
[ 2 , 3 ],
[ 3 , 4 ] ]
for i in A:
Add_edge(i[ 0 ], i[ 1 ])
minTimeToColor( 1 , - 1 , 0 )
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int ans = 0;
static List<List< int >> edges = new List<List< int >>();
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].Count; 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);
}
}
}
static void Main() {
for ( int i = 0; i < 100000; i++)
{
edges.Add( new List< int >());
}
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);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)