B. Dima and a Bad XOR
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Student Dima from Kremland has a matrix aa of size n×mn×m filled with non-negative integers.
He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!
Formally, he wants to choose an integers sequence c1,c2,…,cnc1,c2,…,cn (1≤cj≤m1≤cj≤m) so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0a1,c1⊕a2,c2⊕…⊕an,cn>0holds, where ai,jai,j is the matrix element from the ii-th row and the jj-th column.
Here x⊕yx⊕y denotes the bitwise XOR operation of integers xx and yy.
Input
The first line contains two integers nn and mm (1≤n,m≤5001≤n,m≤500) — the number of rows and the number of columns in the matrix aa.
Each of the next nn lines contains mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix aa, i.e. ai,jai,j (0≤ai,j≤10230≤ai,j≤1023).
Output
If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE".
Otherwise print "TAK" in the first line, in the next line print nn integers c1,c2,…cnc1,c2,…cn (1≤cj≤m1≤cj≤m), so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.
If there is more than one possible answer, you may output any.
Examples
input
Copy
3 2
0 0
0 0
0 0
output
Copy
NIE
input
Copy
2 3
7 7 7
7 7 10
output
Copy
TAK
1 3
Note
In the first example, all the numbers in the matrix are 00, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero.
In the second example, the selected numbers are 77 (the first number in the first line) and 1010 (the third number in the second line), 7⊕10=137⊕10=13, 1313 is more than 00, so the answer is found.
思路:搜索+剪枝 (我们只需要记录之前每行出现过的异或合即可)
#include<bits/stdc++.h>
using namespace std;
int tu[505][505];
int vis[505][1025];
int ans[505];
int n,m,flag;
void dfs(int row,int cur){
//cout<<cur<<" "<<row<<endl;
if(vis[row][cur]) return;
vis[row][cur] = 1;
if(row>n){
if(cur==0) return;
flag = 1;
return ;
}
for(int i=1;i<=m;i++){
if(row==1){
ans[row] = i;
dfs(row+1,tu[row][i]);
if(flag) return;
}else{
ans[row] = i;
dfs(row+1,cur^tu[row][i]);
if(flag) return;
}
}
return ;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
flag = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>tu[i][j];
}
}
dfs(1,tu[1][1]);
if(!flag) cout<<"NIE"<<endl;
else{
cout<<"TAK"<<endl;
for(int i=1;i<n;i++){
cout<<ans[i]<<" ";
}
cout<<ans[n]<<endl;
}
return 0;
}