Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HandsOn 2 : Nodes Reordering You are given a tree which consists of N nodes and an array A where A [ U ] represents

HandsOn
2: Nodes Reordering
You are given a tree which consists of N nodes and an array A where A[U] represents the value of node U.
You are also given an array P where the parent of node U is given by .
We say that the path from U to V is considered good if for each pair of adjacent nodes i,j, the values written on these nodes satisfy the following condition:
A[i]A[j] is smaller than both A[i] and A[j](where ^ is the bitwise xor operator).
Find the maximum length of a good path that you can get.
Note:
It is given that P[1]=0.
Input Format
The first line contains an integer, N, denoting the number of elements in P.
Each line i of the N subsequent lines (where 1=NN105
0P[i]N.1A[i]1050i0i
Sample Test Cases
Case 1
Input:
Input:
5
0
1
2
3
4
1
1
1
1
1
Output:
5
Explanation:
The good path consists of nodes 1,2,3,4,5. They satisfy the condition as the pair of value of nodes present in good path has a XOR equal to 0, which is smaller than the values of the nodes.
Hence, the answer for this case is equal to 5.
Case 2
Input:
5
0
1
2
3
4
2
2
4
6
2
Output:
3
Explanation:
Given ,2,4,6,2.
The good pafs consists of nodes 3,4,5. They satisfy the condition as the pair of value of nodes present in good path has a XOR which is maller than the values of the nodes.
Hence, the answer for this case is equal to 3
Case 3
Input:
4
o
1
1
1
1
1
1
1000
Output:
3
Explanation:
]
[1,1,1000
The good path consists of nodes 1,2,3. They satisfy the condition as the pair of value of nodes present in good path has a XOR of which is smaller than the values of the nodes.
complete the function .public static int get_ans(int N, List P, List A)
image text in transcribed

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

More Books

Students also viewed these Databases questions