Xor over tree

4.2

5 votes
C++, Graph Representation, Algorithms, Graphs
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Given a set of N nodes, where each node has an integer A[i] associated with it. 

Construct a rooted tree using N nodes which is rooted at node 1, such that the value of the given function Z is maximized.

Z=i=Ni=1[j=Nj=i+1[F(i,j)]] where F(i,j)=(S(i,j)  A[LCA(i,j)])+(S(i,j) × A[LCA(i,j)])

  •  denotes the bitwise xor operator.
  • S(i,j) denotes the number of edges on the simple path between nodes i and j.
  • LCA(i,j) denotes the lowest common ancestor of nodes i and j.

Input format

  • First line contains an integer denoting number of nodes in the tree.
  • Next line contains N space separated integers denoting the value of each node i.e. A[i]

Output format

  • Print N - 1 lines where each line contains
    • Two space separated integers a b denoting an edge between node a and b in the tree. 

Constraints

2N5001A[i]109

Verdit and Scoring

  • Z is the score for each test case.
  • If the output is not a tree or contains any invalid entry, then you will receive a Wrong Answer verdict.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the given test case:

  • F(1,2) = 1 ^ A[1] + 1 * A[1] = 1 ^ 4 + 1 * 4 = 9
  • F(1,3) = 2 ^ A[1] + 2 * A[1] = 2 ^ 4 + 2 * 4 = 14
  • F(2,3) = 1 ^ A[2] + 1 * A[2] = 1 ^ 1 + 1 * 1 = 1
  • Z = 9 + 14 + 1 = 24

Hence, score is equal to 24.

Editor Image

?