Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions