Question: Please code in java Stella and colourful tree Problem Statement: While returning from Byteland, Stella got one tree with N nodes from her friend over

Please code in java

Stella and colourful tree Problem Statement:

While returning from Byteland, Stella got one tree with N nodes from her friend over there.

All nodes in this tree are colourless and Stella decided to fill colours to make it colourful.

Stella wants it to look beautiful and decided to color it in such a way that any 2 nodes u and v with shortest distance between u and v <= 2 can not be of same colour. She is wondering how many different colors she needs if she fills it optimally.

Input Format: The first line contains single integer n (3 n 100) the number of nodes in the tree.

Each of the next (n - 1) lines contains two integers x and y (1 x, y n) the indices of two nodes directly connected by an edge.

It is guaranteed that any node is reachable from any other using the edges.

Output Format: In the first line print single integer k the minimum number of colors Stella has to use. Sample Input 1:

3

2 3

1 3

Sample Output 1: 3

Explanation Output 1: Tree is like : 1->3->2

We can colour as follows:

1: Colour a 3: colour b 2: Colour c Total 3 colours

Sample Input 2

: 5

2 1

3 2

4 3

5 4

Sample Output 2: 3

Explanation Output 2: Tree is like : 1->2->3->4->5

We can colour as follows:

1: Colour a

2: colour b

3: Colour c

4: Colour a

5: Colour b

Total 3 colours

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!