AcWing 208. 开关问题
原题链接
中等
#include <bits/stdc++.h>
using namespace std;
const int N = 40;
int a[N][N];
int sc;
void out()
{
for (int i = 1; i <= sc; i ++ )
{
for (int j = 1; j <= sc + 1; j ++ )
cout << a[i][j] << ' ';
puts("");
}
puts("");
}
int gauss()
{
int column, row;
for (column = row = 1; column <= sc; column ++ )
{
int temporary = row;
for (int i = row; i <= sc; i ++ )
if (a[i][column])
{
temporary = i;
break;
}
if (!a[temporary][column]) continue;
for (int i = column; i <= sc + 1; i ++ )swap(a[row][i], a[temporary][i]);
// out();
for (int i = row + 1; i <= sc; i ++ )
if (a[i][column])
for (int j = column; j <= sc + 1; j ++ )
a[i][j] ^= a[row][j];
// out();
row ++ ;
}
// cout << row << endl;
for (int i = sc - 1; i >= 0; i -- )
for (int j = i + 1; j <= sc; j ++ )
a[i][sc] ^= a[i][j] & a[j][sc];
// out();
int res = 1;
if (row < sc + 1)
{
for (int i = row; i <= sc; i ++ )
{
if (a[i][sc + 1]) return -1;
res *= 2;
}
}
return res;
}
int main()
{
int n;
cin >> n;
while (n -- )
{
memset(a, 0, sizeof a); // 数组初始化
cin >> sc;
for (int i = 1; i <= sc ; i ++ ) cin >> a[i][sc + 1]; // 一开始读入初始状态
for (int i = 1; i <= sc; i ++ ) // 读入操作结束后的开关状态,并做基本处理
{
a[i][i] = 1;
int temporary;
cin >> temporary;
a[i][sc + 1] ^= temporary;
}
int x, y;
while (scanf("%d %d", &x, &y), x || y) a[y][x] = 1;
// out();
int res = gauss();
if (res == -1) puts("Oh,it's impossible~!!");
else printf("%d\n", res);
}
return 0;
}