306956: CF1277D. Let's Play the Words?

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Let's Play the Words?

题意翻译

有 $t$ 组数据。 给出 $n$ 个只由 $0$ 和 $1$ 组成的非空二进制串 $s[n]$ ,保证所有二进制串不同。 你有一个操作,可以使一个二进制序列翻转,询问最少的操作次数,使得可以存在一个排列,使 $n$ 个二进制串可以首尾相接,且不存在两个相同的二进制串。 形式化的,操作后的序列 $s$ ,所有二进制串都不相同,且存在一个排列 $p_i$ ,使 $\forall 1\le i<n,\ s[p_i]\text{的末尾}=s[p_{i+1}]\text{的首位}$ 。 你需要输出最少的操作次数,并按从小到大输出需要反转的二进制串编号。 如果不存在方案则输出 $-1$ 。 数据范围:$\ 1\le t\le10^4,\ 1\le n\le2\times10^5,\ \sum|s|\le 4\times10^6$ 。 translated by @Forever_Pursuit

题目描述

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.

输入输出格式

输入格式


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 $ .

输出格式


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.

输入输出样例

输入样例 #1

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

输出样例 #1

1
3 
-1
0

2
1 2 

Input

题意翻译

有 $t$ 组数据。 给出 $n$ 个只由 $0$ 和 $1$ 组成的非空二进制串 $s[n]$ ,保证所有二进制串不同。 你有一个操作,可以使一个二进制序列翻转,询问最少的操作次数,使得可以存在一个排列,使 $n$ 个二进制串可以首尾相接,且不存在两个相同的二进制串。 形式化的,操作后的序列 $s$ ,所有二进制串都不相同,且存在一个排列 $p_i$ ,使 $\forall 1\le i<n,\ s[p_i]\text{的末尾}=s[p_{i+1}]\text{的首位}$ 。 你需要输出最少的操作次数,并按从小到大输出需要反转的二进制串编号。 如果不存在方案则输出 $-1$ 。 数据范围:$\ 1\le t\le10^4,\ 1\le n\le2\times10^5,\ \sum|s|\le 4\times10^6$ 。 translated by @Forever_Pursuit

加入题单

上一题 下一题 算法标签: