POJ-1830
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6294 | Accepted: 2393 |
Description
Input
每组测试数据的格式如下:
第一行 一个数N(0 < N < 29)
第二行 N个0或者1的数,表示开始时N个开关状态。
第三行 N个0或者1的数,表示操作结束后N个开关的状态。
接下来 每行两个数I J,表示如果操作第 I 个开关,第J个开关的状态也会变化。每组数据以 0 0 结束。
Output
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的更多相关文章
- 【POJ 1830】 开关问题 (高斯消元)
开关问题 Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...
- POJ 1830 开关问题(高斯消元)题解
思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...
- 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:// ...
- POJ 1830 开关问题(高斯消元求解的情况)
开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8714 Accepted: 3424 Description ...
- 数学 --- 高斯消元 POJ 1830
开关问题 Problem's Link: http://poj.org/problem?id=1830 Mean: 略 analyse: 增广矩阵:con[i][j]:若操作j,i的状态改变则con[ ...
- POJ 1830 开关问题 【01矩阵 高斯消元】
任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- POJ 1830 开关问题 高斯消元,自由变量个数
http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...
- 【POJ 1830】 开关问题
[题目链接] http://poj.org/problem?id=1830 [算法] 列出异或方程组,用高斯消元求解 [代码] #include <algorithm> #include ...
- POJ 1830 开关问题 (高斯消元)
题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...
- poj 1830 开关问题
开关问题 题意:给n(0 < n < 29)开关的初始和最终状态(01表示),以及开关之间的关联关系(关联关系是单向的输入a b表示a->b),问有几种方式得到最终的状态.否则输出字 ...
随机推荐
- BZOJ1208:[HNOI2004]宠物收养所——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...
- 2016多校联合训练1 D题GCD (ST表+二分)
暑假颓废了好久啊...重新开始写博客 题目大意:给定10w个数,10w个询问.每次询问一个区间[l,r],求出gcd(a[l],a[l+1],...,a[r])以及有多少个区间[l',r']满足gcd ...
- 【初级算法】6. 两个数组的交集 II
题目如下: 给定两个数组,写一个方法来计算它们的交集. 例如: 给定 nums1 = [, , , ], nums2 = [, ], 返回 [, ]. 注意: 输出结果中每个元素出现的次数,应与元素在 ...
- 【DP】【Uva437】UVA437 The Tower of Babylon
传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...
- BUG:Open quote is expected for attribute "{1}" associated with an element type "id".
BUG原因:Mybatis的xml文件中id缺少双引号: 正确的应该是:
- caffe中的Accuracy+softmaxWithLoss
转:http://blog.csdn.net/tina_ttl/article/details/51556984 今天才偶然发现,caffe在计算Accuravy时,利用的是最后一个全链接层的输出(不 ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [C#] 小记 new 和 override 关键字
C#要想实现函数的override,要求和C++一样,父类的函数必须用virtual关键字注明,随后在子类中用override关键字表明重写的函数. 子类同名函数定义时,如果什么都不写,或者使用new ...
- js for等循环 跳出多层循环
js for 循环 跳出多层循环 ,,,,,,,]; // 8个数 ,,,,,,,]; //8个数 testFor(); console.log(') function testFor() { ;k& ...
- mysql 高级应用
1.MySQL like 模糊查询 例如:select * from emp where name like '张%'; 2.1MySQL UNION 操作符 SELECT country FROM ...