ANDfinity (Codeforces 1689E)
Tags: Others, dsu

Bit Lightyear, to the ANDfinity and beyond!After graduating from computer sciences, Vlad has been awarded an array $$$a_1,a_2,\ldots,a_n$$$ of $$$n$$$ non-negative integers. As it is natural, he wanted to construct a graph consisting of $$$n$$$ vertices, numbered $$$1, 2,\ldots, n$$$. He decided to add an edge between $$$i$$$ and $$$j$$$ if and only if $$$a_i \& a_j > 0$$$, where $$$\&$$$ denotes the bitwise AND operation.Vlad also wants the graph to be connected, which might not be the case initially. In order to satisfy that, he can do the following two types of operations on the array:  Choose some element $$$a_i$$$ and increment it by $$$1$$$.  Choose some element $$$a_i$$$ and decrement it by $$$1$$$ (possible only if $$$a_i > 0$$$). It can be proven that there exists a finite sequence of operations such that the graph will be connected. So, can you please help Vlad find the minimum possible number of operations to do that and also provide the way how to do that?

Input: There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. This is followed by the test cases description.The first line of each test case contains an integer $$$n$$$ ($$$2\leq n \leq 2000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\leq a_i < 2^{30}$$$) — the elements of the array.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

Output: For each test case, print a single integer $$$m$$$ in the first line — the minimum number of operations. In the second line print the array after a valid sequence of operations that have been done such that the graph from the task becomes connected. If there are multiple solutions, output any.

Note: In the first test case, the graph is already connected.In the second test case, we can increment $$$0$$$ twice and end up with the array $$$[2,2]$$$. Since $$$2 \& 2 = 2 > 0$$$, the graph is connected. It can be shown that one operation is not enough.In the third test case, we can decrement $$$12$$$ once and we end up with an array $$$[3,11]$$$. $$$3 \& 11 = 3 > 0$$$ hence the graph is connected. One operation has to be done since the graph is not connected at the beginning.

Sample Inputs:
4
5
1 2 3 4 5
2
0 2
2
3 12
4
3 0 0 0

Sample Outputs:
0
1 2 3 4 5
2
2 2
1
3 11
3
3 1 1 1

Solution (Python):
from collections import deque

def connected(n, arr):
    g = [[] for i in range(n)]
    for k in range(30):
        ind = []
        for (i, a) in enumerate(arr):
            if a & 1 << k:
                ind.append(i)
        if len(ind) > 1:
            a = ind.pop()
            for i in ind:
                g[i].append(a)
                g[a].append(i)
    v = [0] * n
    v[0] = 1
    q = deque()
    q.append(0)
    while q:
        x = q.popleft()
        for y in g[x]:
            if v[y] == 0:
                v[y] = 1
                q.append(y)
    return 0 not in v

def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    ans = 0
    for (i, a) in enumerate(arr):
        if a == 0:
            ans += 1
            arr[i] = 1
    if connected(n, arr):
        print(ans)
        print(*arr)
        return
    for i in range(n):
        for j in (-1, 1):
            arr1 = arr[:]
            arr1[i] += j
            if connected(n, arr1):
                print(ans + 1)
                print(*arr1)
                return
    m = max((a & -a for a in arr))
    L = []
    for (i, a) in enumerate(arr):
        if a & -a == m:
            L.append(i)
    arr[L[0]] -= 1
    arr[L[1]] += 1
    print(ans + 2)
    print(*arr)
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
    solve()

Solution (Python):
from collections import deque

def connected(n, arr):
    g = [[] for i in range(n)]
    for k in range(30):
        ind = []
        for (i, a) in enumerate(arr):
            if a & 1 << k:
                ind.append(i)
        if len(ind) > 1:
            a = ind.pop()
            for i in ind:
                g[i].append(a)
                g[a].append(i)
    v = [0] * n
    v[0] = 1
    q = deque()
    q.append(0)
    while q:
        x = q.popleft()
        for y in g[x]:
            if v[y] == 0:
                v[y] = 1
                q.append(y)
    return 0 not in v

def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    ans = 0
    for (i, a) in enumerate(arr):
        if a == 0:
            ans += 1
            arr[i] = 1
    if connected(n, arr):
        print(ans)
        print(*arr)
        return
    for i in range(n):
        for j in (-1, 1):
            arr1 = arr[:]
            arr1[i] += j
            if connected(n, arr1):
                print(ans + 1)
                print(*arr1)
                return
    m = max((a & -a for a in arr))
    L = []
    for (i, a) in enumerate(arr):
        if a & -a == m:
            L.append(i)
    arr[L[0]] -= 1
    arr[L[1]] += 1
    print(ans + 2)
    print(*arr)
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
    solve()

Solution (Python):
from collections import deque

def connected(n, arr):
    g = [[] for i in range(n)]
    for k in range(30):
        ind = []
        for (i, a) in enumerate(arr):
            if a & 1 << k:
                ind.append(i)
        if len(ind) > 1:
            a = ind.pop()
            for i in ind:
                g[i].append(a)
                g[a].append(i)
    v = [0] * n
    v[0] = 1
    q = deque()
    q.append(0)
    while q:
        x = q.popleft()
        for y in g[x]:
            if v[y] == 0:
                v[y] = 1
                q.append(y)
    return 0 not in v

def solve():
    n = int(input())
    arr = list(map(int, input().split()))
    ans = 0
    for (i, a) in enumerate(arr):
        if a == 0:
            ans += 1
            arr[i] = 1
    if connected(n, arr):
        print(ans)
        print(*arr)
        return
    for i in range(n):
        for j in (-1, 1):
            arr1 = arr[:]
            arr1[i] += j
            if connected(n, arr1):
                print(ans + 1)
                print(*arr1)
                return
    m = max((a & -a for a in arr))
    L = []
    for (i, a) in enumerate(arr):
        if a & -a == m:
            L.append(i)
    arr[L[0]] -= 1
    arr[L[1]] += 1
    print(ans + 2)
    print(*arr)
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
    solve()