Bitwise Tree
Practice
4.5 (4 votes)
Dynamic programming
Algorithms
Graphs
Depth first search
Problem
61% Success 205 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree consisting of \(N\) nodes, where the \(i^{th}\) node has a value \(A_i\) assigned to it.

You can do the following operation any number of times. In an operation, you can :

  • Select a node \(i \;(1 \le i \le N)\) and set \(A_i = \left \lfloor{\frac{A_i}{2}}\right \rfloor \).

Find the minimum number of operations to be performed so that the bitwise AND of the values present in any two adjacent nodes of the tree is zero.

  Input format

  • The first line of input contains an integer \(N\) denoting the number of nodes in the tree.
  • The second line contains \(N\) integers \(A_1, A_2,\dots, A_N\), denoting the values assigned to the nodes.
  • \(N-1\) lines follow. The \(i^{th}\) line contains two integers \(X_i, Y_i\) denoting a edge between the nodes \(X_i,Y_i\).It is guaranteed that the nodes and edges form a tree.

Output format
For each query, print the minimum number of operations to be performed so that the bitwise AND of the values present in any two adjacent nodes of the tree is zero.

Constraints

\(2 \leq N \leq 10^5\\ 1\le A_i \le 10^3\\ 1 \le X_i \lt Y_i\le N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
AlgorithmsGraphsHard
Points:50
8 votes
Tags:
Depth First SearchTreesMathematicsGraphsDynamic ProgrammingAlgorithms
Points:50
7 votes
Tags:
Maximum Bipartite MatchingAlgorithmsDepth First SearchGraphs