【高斯消元】【poj 1830】开关问题

来源:互联网 发布:软件项目成本核算 编辑:程序博客网 时间:2024/04/30 11:57

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


最近智商出了点问题,被这道题卡了,不和上一道一样么?


//#define _TEST _TEST#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <cmath>#include <algorithm>using namespace std;/************************************************Code By willinglive    Blog:http://willinglive.cf************************************************/#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)#define MS(arr,x) memset(arr,x,sizeof(arr))#define LL long long#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)inline const int read(){int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}/////////////////////////////////////////////////int n;int s[40],e[40];int a[40][40];/////////////////////////////////////////////////int gauss(){int i,j;for(i=1;i<=n&&j<=n;j++){int k=i;for(;k<=n;k++) if(a[k][i]) break;if(a[k][j]){if(i^k) rep(j,i,n) swap(a[i][j],a[k][j]);rep(k,i+1,n) if(a[k][i])    rep(l,1,n+1) a[k][l]^=a[i][l];i++;}}for(j=i;j<=n;j++) if(a[j][n+1]) return -1;return 1<<(n-i+1);}/////////////////////////////////////////////////void input(){MS(a,0);    n=read();    rep(i,1,n) s[i]=read();    rep(i,1,n) e[i]=read();    rep(i,1,n) a[i][n+1]=s[i]^e[i],a[i][i]=1;    int i,j;    while(i=read(),j=read(),i+j) a[j][i]=1;}void solve(){    int res=gauss();    if(res==-1) puts("Oh,it's impossible~!!");    else printf("%d\n",res);}/////////////////////////////////////////////////int main(){    #ifndef _TEST    freopen("std.in","r",stdin); freopen("std.out","w",stdout);    #endif    per(i,read(),1)    input(),solve();    return 0;}


0 0