Given a tree with N nodes, a source node S and a destination node D. Each node has a colour either: Red. Green or Blue. Staying at the node for 1 second rewards us with a coloured ball of the colour of the node. Travelling across an edge takes 1 second You have to reach the destination node starting from the source node in minimum time ensuring: The number of balls of colour is equal and non zero.
Note: In order to gain a ball you have to spend 1 second on the node otherwise no ball is gained There always exists at least one node of each colour The nodes are 1 indexed.
Input Format: The first line contains (the number of nodes) followed by next N lines of the format I. Colour representing the colour of the i'th node. Next N-1 lines contains a. b representing an edge of the tree between node a and b. The next line contains S the source node and the following line contains D the destination node.
Output Format: Output a singe integer. denoting the min time required to reach the destination node satisfying the conditions.
Constraints:
3 <= N <= 2*10^5
3 <= N <= 10'3 for 50% marks
Sample Input:
4
1 Red
2 Green
3 Blue
4 Red
1 2
1 3
3 4
1
4
Sample Output: 7
Could some one suggest how to slove this problem.