Question: 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

 Problem 4 (Binary/N-ary Tree) After a hectic week at work, Mancunian

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 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 denoting the number of vertices in the tree and the number of possible colours. The second line contains N-1 integers. The ith integer denotes the parent of the i+1th vertex. The third line contains Nintegers, denoting the colours of the vertices. Each colour lies between 1 and C inclusive. Output format Print n space-separated integers. The ith integer is the vertex number of lowest ancestor of the ith node which has the same colour. If there is no such ancestor, print for that node. Constraints 15NS100,000 15c$100,000 Sample Input 54 1133 14212 Sample Output -1 -1 -1 13

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!