XOR subsequences

3.2

4 votes
Set, Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

You are given an array A consisting of N integers. Your task is to find the longest subsequence S, such that XOR of any two elements in S is non-zero.

You are required to print the size of S(L) and the array of indices that are chosen for S. In case of multiple such arrays, choose the lexicographically smallest array.

Example: Let array A be [7, 9, 7], the maximum length of subsequence can be 2. There are two ways to choose a subsequence, [7, 9] and [9, 7]. The indices selected in the first case are [1, 2] while in the second they are [2, 3] so we need to choose [1, 2] as it is minimal.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of elements in array A.
  • The second line of each test case contains N space-separated integers of array A.

Output format

For each test case:

  • The first line must contain the maximum size (L) of S that can be chosen as a subsequence.
  • The second line must contain space-separated integers denoting the lexicographically minimal array of indices that can be chosen.

Constraints

  • 1T20000
  • 1N200000
  • 1Ai109
  • Sum of N over all test cases will not exceed 200000.
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Some ways of choosing with length 3:

[1,2,4], [1,4,5] [2,3,5] and [3,4,5]. Choose the minimal out of them.

Editor Image

?