Let's Play the Words? (Codeforces 1259D)
Tags: Math, implementation

Polycarp has $$$n$$$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100".Polycarp wants to offer his set of $$$n$$$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001".Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011".Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that:  the final set of $$$n$$$ words still contains different words (i.e. all words are unique);  there is a way to put all words of the final set of words in the order so that the final sequence of $$$n$$$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow.The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 2\cdot10^5$$$) — the number of words in the Polycarp's set. Next $$$n$$$ lines contain these words. All of $$$n$$$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $$$4\cdot10^6$$$. All words are different.Guaranteed, that the sum of $$$n$$$ for all test cases in the input doesn't exceed $$$2\cdot10^5$$$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $$$4\cdot10^6$$$.

Output: Print answer for all of $$$t$$$ test cases in the order they appear.If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $$$k$$$ ($$$0 \le k \le n$$$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $$$k$$$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $$$1$$$ to $$$n$$$ in the order they appear. If $$$k=0$$$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them.


Sample Inputs:
4
4
0001
1000
0011
0111
3
010
101
0
2
00000
00001
4
01
001
0001
00001

Sample Outputs:
1
3 
-1
0

2
1 2

Solution (Python):
t = int(input())
for i in range(t):
    n = int(input())
    (i0, i1) = ([], [])
    (l0, l1) = ([], [])
    (h0, h1) = (False, False)
    for i in range(n):
        t = input()
        if t[0] == '0' and t[-1] == '1':
            i0.append(i)
            l0.append(t)
        elif t[0] == '1' and t[-1] == '0':
            i1.append(i)
            l1.append(t)
        elif t[0] == t[-1] == '1':
            h1 = True
        elif t[0] == t[-1] == '0':
            h0 = True
    (c0, c1) = (len(l0), len(l1))
    (req, sl) = (0, [])
    s0 = set(l0)
    s1 = set(l1)
    if c0 > 0 or c1 > 0:
        if c0 - c1 > 1:
            req = (c0 - c1) // 2
            sel = 0
            sl = []
            for tt in range(len(l0)):
                t = l0[tt]
                if not t[::-1] in s1:
                    req -= 1
                    sl.append(i0[tt] + 1)
                if req == 0:
                    break
        elif c1 - c0 > 1:
            req = (c1 - c0) // 2
            sel = 0
            sl = []
            for tt in range(len(l1)):
                t = l1[tt]
                if not t[::-1] in s0:
                    req -= 1
                    sl.append(i1[tt] + 1)
                if req == 0:
                    break
        if req > 0:
            print(-1)
        else:
            print(len(sl))
            print(*sl)
    elif h0 and h1:
        print(-1)
    else:
        print(0)
        print(*[])

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    mass = []
    zo = 0
    oz = 0
    zz = 0
    oo = 0
    ozs = []
    zos = []
    ozss = set()
    zoss = set()
    for j in range(n):
        k = input()
        mass.append(k)
        if k[0] == '0' and k[-1] == '1':
            zoss.add(k)
            zos.append(j + 1)
            zo += 1
        elif k[0] == '1' and k[-1] == '0':
            ozss.add(k)
            ozs.append(j + 1)
            oz += 1
        elif k[0] == '0' and k[-1] == '0':
            zz += 1
        else:
            oo += 1
    if zz and oo and (not oz) and (not zo):
        print(-1)
        continue
    elif zo > oz:
        print((zo - oz) // 2)
        ans = []
        need = (zo - oz) // 2
        i = 0
        while need:
            zzz = mass[zos[i] - 1][len(mass[zos[i] - 1]) - 1::-1]
            if zzz not in ozss:
                ans.append(zos[i])
                need -= 1
            i += 1
        print(*ans)
    else:
        print((oz - zo) // 2)
        ans = []
        need = (oz - zo) // 2
        i = 0
        while need:
            zzz = mass[ozs[i] - 1][len(mass[ozs[i] - 1]) - 1::-1]
            if zzz not in zoss:
                ans.append(ozs[i])
                need -= 1
            i += 1
        print(*ans)

Solution (Python):
k = int(input())
for i in range(k):
    is_t = set()
    a = dict()
    a['00'] = []
    a['11'] = []
    a['01'] = []
    a['10'] = []
    n = int(input())
    s = []
    for i in range(n):
        b = input()
        a[b[0] + b[-1]].append(i)
        s.append(b)
        is_t.add(b)
    c = len(a['10'])
    d = len(a['01'])
    if c + d == 0:
        if len(a['00']) == 0 or len(a['11']) == 0:
            print(0)
        else:
            print(-1)
    elif c > d:
        ans = []
        i = 0
        m = (d + c) // 2
        while d != m and i < len(a['10']):
            s1 = s[a['10'][i]]
            if s1[::-1] not in is_t:
                d += 1
                ans.append(a['10'][i] + 1)
            i += 1
        if d != m:
            print(-1)
        else:
            print(len(ans))
            print(*ans)
    else:
        ans = []
        i = 0
        m = (d + c) // 2
        while c != m and i < len(a['01']):
            s1 = s[a['01'][i]]
            if s1[::-1] not in is_t:
                c += 1
                ans.append(a['01'][i] + 1)
            i += 1
        if c != m:
            print(-1)
        else:
            print(len(ans))
            print(*ans)