Question: answer the question in java language answer the question in java language answer the question in java language Problem 4 (Binary/N-ary Tree) After a hectic

answer the question in java language
answer the question in java language
answer the question in java language

answer the question in java language answer the question in java language

Problem 4 (Binary/N-ary Tree) After a hectic week at work, Mancunian and Liverbird decide to go on a fun weekend camping trip. As they were passing through a forest, they stumbled upon a unique tree of N nodes. Vertices are numbered from 1 to N. Each node of the tree is assigned a colour (out of C possible colours). Being bored, they decide to work together (for a change) and test their reasoning skills. The tree is rooted at vertex 1. For each node, they want to find its closest ancestor having the same colour. Input format The first line contains two integers N and c denoting the number of vertices in the tree and the number of possible colours. The second line contains N1 integers. The ict integer denotes the parent of the 1+1th vertex. The third line contains n integers, denoting the colours of the vertices. Each colour lies between 1 and c inclusive. Output format Print N space-separated integers. The i th integer is the vertex number of lowest ancestor of the ith node which has the same colour. If there is no such ancestor, print 1 for that node. Constraints - 15NS100,000 Sample Input 514133 1SampleOutput4212 11113

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!