Minimum steps to color the tree with given colors
Given a tree with N nodes which initially have no color and an array color[] of size N which represent the color of each node after the coloring process takes place. The task is to color the tree into the given colors using the smallest possible number of steps. On each step, one can choose a vertex v and a color x, and then color all vertices in the sub-tree of v (including v itself) with color x. Note that root is vertex number 1.
Examples:
Input: color[] = { 1, 1, 2, 1, 3, 1}
Output: 4
Color the sub-tree rooted at node 1 with color 1.
Then all the vertices have colors 1.
Now, color the sub-tree rooted at 3 with color 2.
Finally, color the sub-trees rooted at 5 and 6 with colors 3 and 1 respectively.
Input: color[] = { 1, 2, 3, 2, 2, 3}
Output: 3
Approach: Call a DFS function at vertex 1 and initially keep answer as zero. Increment the answer whenever there is a difference in colors of child and parent nodes.
See the below code for better understanding.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
vector< int > gr[100005];
void Add_Edge( int u, int v)
{
gr[u].push_back(v);
gr[v].push_back(u);
}
void dfs( int child, int par, int color[])
{
if (color[child] != color[par])
ans++;
for ( auto it : gr[child]) {
if (it == par)
continue ;
dfs(it, child, color);
}
}
int main()
{
int color[] = { 0, 1, 2, 3, 2, 2, 3 };
Add_Edge(1, 2);
Add_Edge(1, 3);
Add_Edge(2, 4);
Add_Edge(2, 5);
Add_Edge(3, 6);
dfs(1, 0, color);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int ans = 0 ;
static Vector<Vector<Integer>> gr = new Vector<Vector<Integer>>();
static void Add_Edge( int u, int v)
{
gr.get(u).add(v);
gr.get(v).add(u);
}
static void dfs( int child, int par, int color[])
{
if (color[child] != color[par])
ans++;
for ( int i = 0 ; i < gr.get(child).size(); i++)
{
if (gr.get(child).get(i) == par)
continue ;
dfs(gr.get(child).get(i), child, color);
}
}
public static void main(String args[])
{
for ( int i = 0 ; i <= 10 ; i++)
gr.add( new Vector<Integer>());
int color[] = { 0 , 1 , 2 , 3 , 2 , 2 , 3 };
Add_Edge( 1 , 2 );
Add_Edge( 1 , 3 );
Add_Edge( 2 , 4 );
Add_Edge( 2 , 5 );
Add_Edge( 3 , 6 );
dfs( 1 , 0 , color);
System.out.println( ans);
}
}
|
Python3
ans = 0
gr = [[] for i in range ( 100005 )]
def Add_Edge(u, v):
gr[u].append(v)
gr[v].append(u)
def dfs(child, par, color):
global ans
if (color[child] ! = color[par]):
ans + = 1
for it in gr[child]:
if (it = = par):
continue
dfs(it, child, color)
color = [ 0 , 1 , 2 , 3 , 2 , 2 , 3 ]
Add_Edge( 1 , 2 )
Add_Edge( 1 , 3 )
Add_Edge( 2 , 4 )
Add_Edge( 2 , 5 )
Add_Edge( 3 , 6 )
dfs( 1 , 0 , color)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ans = 0;
static List<List< int >> gr = new List<List< int >>();
static void Add_Edge( int u, int v)
{
gr[u].Add(v);
gr[v].Add(u);
}
static void dfs( int child, int par, int []color)
{
if (color[child] != color[par])
ans++;
for ( int i = 0; i < gr[child].Count; i++)
{
if (gr[child][i] == par)
continue ;
dfs(gr[child][i], child, color);
}
}
public static void Main(String []args)
{
for ( int i = 0; i <= 10; i++)
gr.Add( new List< int >());
int []color = { 0, 1, 2, 3, 2, 2, 3 };
Add_Edge(1, 2);
Add_Edge(1, 3);
Add_Edge(2, 4);
Add_Edge(2, 5);
Add_Edge(3, 6);
dfs(1, 0, color);
Console.WriteLine( ans);
}
}
|
Javascript
<script>
let ans = 0;
let gr = [];
function Add_Edge(u,v)
{
gr[u].push(v);
gr[v].push(u);
}
function dfs(child,par,color)
{
if (color[child] != color[par])
ans++;
for (let i = 0; i < gr[child].length; i++)
{
if (gr[child][i] == par)
continue ;
dfs(gr[child][i], child, color);
}
}
for (let i = 0; i <= 10; i++)
gr.push([]);
let color = [ 0, 1, 2, 3, 2, 2, 3 ];
Add_Edge(1, 2);
Add_Edge(1, 3);
Add_Edge(2, 4);
Add_Edge(2, 5);
Add_Edge(3, 6);
dfs(1, 0, color);
document.write( ans);
</script>
|
Last Updated :
03 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...