CF1774D.Same Count One
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
ChthollyNotaSeniorious received a special gift from AquaMoon: n binary arrays of length m . AquaMoon tells him that in one operation, he can choose any two arrays and any position pos from 1 to m , and swap the elements at positions pos in these arrays.
He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of 1 s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it!
If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output −1 .
输入格式
The first line of the input contains a single integer t ( 1≤t≤2⋅104 ) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n and m ( 2≤n≤105 , 2≤m≤105 ).
The i -th of the following n lines contains m integers ai,1,ai,2,…,ai,m (0≤ai,j≤1) — the elements of the i -th array.
It is guaranteed that the sum of n⋅m over all test cases does not exceed 106 .
输出格式
For each test case, if the objective is not achievable, output −1 .
Otherwise, in the first line output k (0≤k≤mn) — the minimum number of operations required.
The i -th of the following k lines should contain 3 integers, xi,yi,zi (1≤xi,yi≤n,1≤zi≤m) , which describe an operation that swap axi,zi,ayi,zi : swap the zi -th number of the xi -th and yi -th arrays.
输入输出样例
输入#1
3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1
输出#1
1 2 1 1 1 4 2 2 -1
说明/提示
In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become [0,1,1,0],[1,0,1,0],[1,0,0,1] , each of them contains exactly two 1 s.