Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6294   Accepted: 2393

Description

有 N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关 的状态如果原来为开就变为关,如果为关就变为开。你的目标是经过若干次开关操作后使得最后N个开关达到一个特定的状态。对于任意一个开关,最多只能进行一 次开关操作。你的任务是,计算有多少种可以达到指定状态的方法。(不计开关操作的顺序)

Input

输入第一行有一个数K,表示以下有K组测试数据。
每组测试数据的格式如下:

第一行 一个数N(0 < N < 29)

第二行 N个0或者1的数,表示开始时N个开关状态。

第三行 N个0或者1的数,表示操作结束后N个开关的状态。

接下来 每行两个数I J,表示如果操作第 I 个开关,第J个开关的状态也会变化。每组数据以 0 0 结束。

Output

如果有可行方法,输出总数,否则输出“Oh,it's impossible~!!” 不包括引号

Sample Input

2
3
0 0 0
1 1 1
1 2
1 3
2 1
2 3
3 1
3 2
0 0
3
0 0 0
1 0 1
1 2
2 1
0 0

Sample Output

4
Oh,it's impossible~!!

Hint

第一组数据的说明:
一共以下四种方法:

操作开关1

操作开关2

操作开关3

操作开关1、2、3 (不记顺序)

/**
题意:给一些开关,开某一个开关之后有的开关也会变化
做法:高斯消元 线性代数
**/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define maxn 50
using namespace std;
int mmap[maxn][maxn];
int start[maxn];
int eed[maxn];
int guess(int equ,int val)
{
int k=,col = ;
int max_r = ;
for(k=; k<equ&&col<val; k++,col++)
{
max_r = k;
for(int i=k+; i<equ; i++)
{
if(abs(mmap[i][col]) > abs(mmap[max_r][col]))
{
max_r = i;
}
}
if(max_r != k)
{
for(int i=k; i<val+; i++)
{
swap(mmap[k][i],mmap[max_r][i]);
}
}
if(mmap[k][col] == )
{
k--;
continue;
}
for(int i=k+; i<equ; i++)
{
if(mmap[i][col] != )
{
for(int j=col; j<val+; j++)
{
mmap[i][j] ^= mmap[k][j];
}
}
}
}
///上三角
for(int i=k; i<equ; i++)
{
if(mmap[i][col]!=) return -;
}
return val-k;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
memset(start,,sizeof(start));
memset(eed,,sizeof(eed));
for(int i=; i<n; i++)
{
scanf("%d",&start[i]);
}
for(int i=; i<n; i++)
{
scanf("%d",&eed[i]);
}
int u,v;
memset(mmap,,sizeof(mmap));
while(scanf("%d %d",&u,&v))
{
if(u == && v == ) break;
u--;
v--;
mmap[v][u] = ;
}
for(int i=; i<n; i++)
{
mmap[i][i] = ;
}
for(int i=; i<n; i++)
{
mmap[i][n] = start[i]^eed[i];
}
int res = guess(n,n);
if(res == -) printf("Oh,it's impossible~!!\n");
else printf("%d\n",<<res);
}
return ;
}

POJ-1830的更多相关文章

  1. 【POJ 1830】 开关问题 (高斯消元)

    开关问题   Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...

  2. POJ 1830 开关问题(高斯消元)题解

    思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...

  3. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  4. POJ 1830 开关问题(高斯消元求解的情况)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8714   Accepted: 3424 Description ...

  5. 数学 --- 高斯消元 POJ 1830

    开关问题 Problem's Link: http://poj.org/problem?id=1830 Mean: 略 analyse: 增广矩阵:con[i][j]:若操作j,i的状态改变则con[ ...

  6. POJ 1830 开关问题 【01矩阵 高斯消元】

    任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...

  7. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

  8. 【POJ 1830】 开关问题

    [题目链接] http://poj.org/problem?id=1830 [算法] 列出异或方程组,用高斯消元求解 [代码] #include <algorithm> #include ...

  9. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  10. poj 1830 开关问题

    开关问题 题意:给n(0 < n < 29)开关的初始和最终状态(01表示),以及开关之间的关联关系(关联关系是单向的输入a b表示a->b),问有几种方式得到最终的状态.否则输出字 ...

随机推荐

  1. BZOJ1208:[HNOI2004]宠物收养所——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...

  2. 2016多校联合训练1 D题GCD (ST表+二分)

    暑假颓废了好久啊...重新开始写博客 题目大意:给定10w个数,10w个询问.每次询问一个区间[l,r],求出gcd(a[l],a[l+1],...,a[r])以及有多少个区间[l',r']满足gcd ...

  3. 【初级算法】6. 两个数组的交集 II

    题目如下: 给定两个数组,写一个方法来计算它们的交集. 例如: 给定 nums1 = [, , , ], nums2 = [, ], 返回 [, ]. 注意: 输出结果中每个元素出现的次数,应与元素在 ...

  4. 【DP】【Uva437】UVA437 The Tower of Babylon

    传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...

  5. BUG:Open quote is expected for attribute "{1}" associated with an element type "id".

    BUG原因:Mybatis的xml文件中id缺少双引号: 正确的应该是:

  6. caffe中的Accuracy+softmaxWithLoss

    转:http://blog.csdn.net/tina_ttl/article/details/51556984 今天才偶然发现,caffe在计算Accuravy时,利用的是最后一个全链接层的输出(不 ...

  7. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  8. [C#] 小记 new 和 override 关键字

    C#要想实现函数的override,要求和C++一样,父类的函数必须用virtual关键字注明,随后在子类中用override关键字表明重写的函数. 子类同名函数定义时,如果什么都不写,或者使用new ...

  9. js for等循环 跳出多层循环

    js for 循环 跳出多层循环 ,,,,,,,]; // 8个数 ,,,,,,,]; //8个数 testFor(); console.log(') function testFor() { ;k& ...

  10. mysql 高级应用

    1.MySQL like 模糊查询 例如:select * from emp where name like '张%'; 2.1MySQL UNION 操作符 SELECT country FROM ...