You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.7 KiB

2 years ago
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
using namespace std;
const int N = 30;
const double eps = 10e-8;
int n;
int start[N]; //开始状态
int stop[N]; //结束状态
//高斯消元模板
double a[N][N]; //增广矩阵
int gauss() {
int c, r; //当前列,行
// 1、枚举系数每一列
for (c = 0, r = 0; c < n; c++) {
// 2、找出系数最大的行
int t = r;
for (int i = r; i < n; i++) //行
if (abs(a[i][c]) > abs(a[t][c])) t = i;
// 3、最大系数为0直接下一列
if (abs(a[t][c]) < eps) continue;
// 4、交换
if (r != t) // POJ中如果二维数组直接swap(a[t],a[r])会报编译错误,没办法,只好用了循环
for (int i = 0; i < N; i++) swap(a[t][i], a[r][i]);
// 5、倒序, 每项除a[r][c],化系数为1,处理的是方程左右两端需要带着a[r][n]
for (int i = n; i >= c; i--) a[r][i] /= a[r][c];
// 6、用当前行将下面所有的列消成0
for (int i = r + 1; i < n; i++)
for (int j = n; j >= c; j--)
a[i][j] -= a[r][j] * a[i][c];
// 7、下一行
r++;
}
if (r < n) {
for (int i = r; i < n; i++)
if (abs(a[i][n]) > eps)
return -1; //无解
return n - r; //自由元个数
}
//倒三角,将已知解代入
for (int i = n - 2; i >= 0; i--)
for (int j = i + 1; j < n; j++)
a[i][n] -= a[i][j] * a[j][n];
//唯一解0
return 0;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
memset(a, 0, sizeof(a));
memset(start, 0, sizeof(start)); //初始化开始状态
memset(stop, 0, sizeof(stop)); //初始化终止状态
//输入起始状态(下标从0开始)
for (int i = 0; i < n; i++) cin >> start[i];
//输入终止状态(下标从0开始)
for (int i = 0; i < n; i++) cin >> stop[i];
//输入增广矩阵
int x, y;
while (cin >> x >> y && x != 0 && y != 0)
a[y - 1][x - 1] = 1; //反着存入y-1受x-1影响
for (int i = 0; i < n; i++) {
a[i][n] = start[i] ^ stop[i]; //状态变化 start^stop
a[i][i] = 1; //自己影响自己
}
//高斯消元模板
int t = gauss();
if (t == -1)
cout << "Oh,it's impossible~!!" << endl;
else
cout << (1 << t) << endl;
}
return 0;
}