Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Bob Jones lives with his family His family has members with member ID 0 t 0 Nit Bob Jones is the head of the family
Bob Jones lives with his family His family has members with member ID t Nit Bob Jones is the head of the family with member ID His family has K number of earning members. Every month each earning member receives their salary in a particular order and then gives it to their parent, the parent in turn gives the salary to their parents, and so on The salary will be given to the highest member of the family hierarchy. If member A is the parent of member B and if A had already received the salary, then B will keep the salary.
For example, in the given family hierarchy ABC B is the parent of A and Cis the parent of B so if A receives the salary then it first goes to B and then it goes to C but in case if Calready had received the salary then B will keep the salary of A and if in case B also received the salary already then. A will keep the salary
You are given two integers Nand K where N is the total number of members in the family and K is the total number of earning members in the family. You are given an array parents representing the parent's member ID where the index represents the child's member ID As Bob Jones is the head of the family, he will have no parent that will be represented by and therefore the parent array will always start with You are given an array salaryorder presenting the member IDs of all the earning members of the family inme order they receive the salary that is index O receives the salary first and then index and so on
Return an array containing the member ID of all members that keeps tge salary in the order they receive it
Function description:
Complete the Solve function provided that takes tge following arguments and retums the answer
N Represents the total number of family members
K Represents the total number of earning members
parents: Represents the array of parents ie parentsi represent the parent of an member
salaryorder. Represents all the earning members of the family in the order they receive the salary
Input format for custom testing
Note: Use this input format if you are testing against custom input or writing Code in a language where we don't provide boilerplate code.
The first line contains integer N denoting the total number of family members
The second line contains integer K denoting the total number of earning members
The third line contains N spaceseparated integers denoting the array of parents of each member.
The fourth line contains K spaceseparated integers denoting the array of earning members of the family in the order they receive the salary.
Output format:
Print the array of all the members that keep the salary in the order they receive it
Sample input:
Sample output:
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