Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions