Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 languageProblem 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started