Solutin:
将每个开关使用的情况当成未知数,如果开关i能影响到开关j,那么系数矩阵A[j][i]的系数为1。
每个开关增广矩阵的值是开关k的初状态异或开关k的目标状态,这个应该很容易想到。
方程都列好了,直接消元就好了。
code
/*
解异或方程组
*/
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = ; int prim[MAXN];
int A[MAXN][MAXN]; int Gauss (int n, int m) {
int col = , row = , tem, k = ;
for (; col < m && row < n; ++col) {
for (tem = row; tem < n && A[tem][col] == ; ++tem);
if (tem == n) continue;
if (tem != row) {
for (int i = ; i <= n; ++i)
swap (A[row][i], A[tem][i]);
}
for (int i = row + ; i <= n; ++i) {
if (A[i][col])
for (int j = col; j <= m; ++j)
A[i][j] ^= A[row][j];
}
row++;
}
for (int i = row; i < n; ++i)
if (A[i][n] != ) return -;
return m - row;
}
int n, m, cs, S, T;
int main() {
ios::sync_with_stdio ();
cin >> cs;
while (cs--) {
cin >> n;
S = T = ;
for (int i = , x; i < n; ++i) {
cin >> x;
if (x) S |= ( << i);
}
for (int i = , x; i < n; ++i) {
cin >> x;
if (x) T |= ( << i);
}
T ^= S;
memset (A, , sizeof A);
for (int i = ; i < n; ++i){
if (T & ( << i) ) A[i][n] = ;
A[i][i]=;
}
int x, y;
while (cin >> x >> y, x && y) A[y - ][x - ] = ;
int p = Gauss (n, n);
if (p < ) cout << "Oh,it's impossible~!!" << endl;
else
cout << ( << p) << endl;
}
}